#include <bits/stdc++.h>
using namespace std;
template <typename T>
void r1(T &x) {
  x = 0;
  char c(getchar());
  int f(1);
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  x *= f;
}
const int maxn = 5e3 + 5;
const int maxm = maxn << 1;
typedef int room[maxn];
int n, m;
int s[maxn], tag[maxn], f[maxn];
signed main() {
  int i, j, t;
  r1(t);
  while (t--) {
    r1(n);
    for (i = 1; i <= n; ++i) r1(tag[i]);
    for (i = 1; i <= n; ++i) r1(s[i]);
    for (i = 1; i <= n; ++i) f[i] = 0;
    for (i = 1; i <= n; ++i) {
      for (j = i - 1; j; --j) {
        if (tag[i] == tag[j]) continue;
        int z = abs(s[i] - s[j]), fi = f[i], fj = f[j];
        f[i] = max(f[i], fj + z);
        f[j] = max(f[j], fi + z);
      }
    }
    int ans(0);
    for (i = 1; i <= n; ++i) ans = max(ans, f[i]);
    printf("%d\n", ans);
  }
  return 0;
}
