#include <bits/stdc++.h>
using namespace std;
long long f[2][5000 + 5], F[5000 + 5];
int tag[5000 + 5], s[5000 + 5];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, i, j, last = -1;
    long long ans = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; i++) scanf("%d", &tag[i]);
    for (i = 1; i <= n; i++) scanf("%d", &s[i]);
    memset(f, 0, sizeof(f));
    for (i = 2; i <= n; i++) {
      memset(f[i & 1], 0, sizeof(f[i & 1]));
      memset(F, 0, sizeof(F));
      int maxn = 0;
      for (j = 1; j < i; j++)
        if (tag[i] != tag[j]) F[j] = f[(i & 1) ^ 1][j] + abs(s[i] - s[j]);
      for (j = 1; j < i; j++) f[i & 1][i] = max(f[i & 1][i], F[j]);
      for (j = i - 1; j >= 1; j--) F[j] = max(F[j + 1], F[j]);
      for (j = 1; j < i; j++)
        if (tag[i] != tag[j]) f[i & 1][j] = F[j + 1] + abs(s[i] - s[j]);
      for (j = 1; j <= i; j++) ans = max(ans, f[i & 1][j]);
    }
    printf("%lld\n", ans);
  }
}
