#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char c = getchar();
  long long s = 0;
  long long x = 1;
  while (c < '0' || c > '9') {
    if (c == '-') x = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    s = s * 10 + c - '0';
    c = getchar();
  }
  return s * x;
}
long long T, n, m;
const long long N = 2e5 + 55;
long long tag[N], s[N], f[2][N];
signed main() {
  T = read();
  while (T--) {
    n = read();
    for (long long i = 1; i <= n; i++) {
      tag[i] = read();
    }
    for (long long i = 1; i <= n; i++) {
      s[i] = read();
    }
    tag[0] = tag[1];
    long long head = n + 1;
    for (long long i = 1; i <= n; i++) {
      if (tag[i] != tag[i + 1]) {
        head = i + 1;
        break;
      }
    }
    memset(f[head & 1], 0, sizeof(f[head & 1]));
    for (long long i = 1; i < head; i++) {
      f[head & 1][i] = abs(s[head] - s[i]);
    }
    long long maxx = 0;
    for (long long i = head; i <= n; i++) {
      memset(f[(i + 1) & 1], 0, sizeof(f[(i + 1) & 1]));
      for (long long j = i; j >= 1; j--) {
        f[i & 1][j] = max(f[i & 1][j + 1], f[i & 1][j]);
        maxx = max(maxx, f[i & 1][j]);
      }
      for (long long j = 1; j < i; j++) {
        if (tag[i] != tag[j] and tag[j] != tag[i + 1])
          f[(i + 1) & 1][j] =
              max(f[(i + 1) & 1][j],
                  f[i & 1][j + 1] + abs(s[i] - s[j]) + abs(s[i + 1] - s[j]));
      }
      if (tag[i] != tag[i + 1])
        f[(i + 1) & 1][i] =
            max(f[(i + 1) & 1][i], f[i & 1][1] + abs(s[i] - s[i + 1]));
      for (long long j = 1; j <= i; j++) {
        if (f[i & 1][j + 1] and tag[i] != tag[j])
          maxx = max(maxx, f[i & 1][j + 1] + abs(s[i] - s[j]));
      }
    }
    cout << maxx << endl;
  }
  return 0;
}
