#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
long long INF = 5e18;
int n, tag[MAXN], s[MAXN], T;
long long dp[MAXN], temp[MAXN], ans;
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &tag[i]);
    }
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &s[i]);
    }
    for (int i = 1; i <= n; ++i) {
      dp[i] = -INF;
    }
    dp[1] = 0;
    for (int i = 2; i <= n; ++i) {
      temp[0] = 0;
      for (int j = 1; j <= i; ++j) {
        temp[j] = 0;
      }
      for (int j = 1; i - j > 0; ++j) {
        if (tag[i - j] != tag[i]) {
          temp[j] = max(temp[j - 1], dp[i - j] + abs(s[i - j] - s[i]));
          dp[i - j] = max(dp[i - j], temp[j - 1] + abs(s[i - j] - s[i]));
        } else {
          temp[j] = temp[j - 1];
        }
      }
      dp[i] = temp[i - 1];
      ans = 0;
      for (int j = 1; j <= i; ++j) {
        ans = max(ans, dp[j]);
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
