#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000 + 5;
const int modu = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-5;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
void read(long long &x) {
  x = 0;
  int f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    f |= (ch == '-');
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x = f ? -x : x;
  return;
}
void read(int &x) {
  long long y;
  read(y);
  x = (int)y;
}
void read(char &ch) {
  char s[3];
  scanf("%s", s);
  ch = s[0];
}
void read(char *s) { scanf("%s", s); }
template <class T, class... U>
void read(T &x, U &...u) {
  read(x);
  read(u...);
}
int n, k, tag[maxn];
long long s[maxn], f[maxn];
int main() {
  int T;
  read(T);
  while (T--) {
    read(n);
    for (int i = 0; i < n; ++i) read(tag[i]);
    for (int i = 0; i < n; ++i) read(s[i]);
    long long ans = 0;
    memset(f, 0, sizeof(typeid(f[0])) * n);
    for (int i = 1; i < n; ++i) {
      for (int j = i - 1; j >= 0; --j)
        if (tag[i] != tag[j]) {
          long long dp1 = f[j] + abs(s[i] - s[j]);
          long long dp2 = f[i] + abs(s[i] - s[j]);
          f[i] = max(f[i], dp1);
          f[j] = max(f[j], dp2);
        }
    }
    for (int i = 0; i < n; ++i) ans = max(ans, f[i]);
    printf("%lld\n", ans);
  }
  return 0;
}
