#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << "[" << H << "]";
  debug_out(T...);
}
clock_t startTime;
double getCurrentTime() {
  return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.00001;
const int MOD = 1e9 + 7;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e7 + 3e2;
const int M = 5555;
long long dp[M];
int tg[M], a[M];
int mx[M];
int i1[M], i2[M];
void solve(int TC) {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> tg[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = 0;
    for (int j = 1; j < i; j++) {
      if (tg[i] != tg[j]) {
        if (dp[i] < dp[j] + abs(a[i] - a[j])) {
          dp[i] = max(dp[i], dp[j] + abs(a[i] - a[j]));
          i1[i] = j;
        } else {
          if (dp[i] == dp[j] + abs(a[i] - a[j])) {
            i1[i] = max(i1[i], j);
          }
        }
      }
    }
    ans = max(ans, dp[i]);
  }
  if (ans == 0) {
    cout << 0 << endl;
    return;
  }
  fill(mx + 1, mx + n + 1, 0);
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j < i; j++) {
      if (tg[i] != tg[j]) {
        if (i < n) {
          if (dp[i] != dp[j] + abs(a[i] - a[j]) ||
              dp[i] == dp[j] + abs(a[i] - a[j]) && i1[i] != j)
            ans = max(ans, dp[i] + abs(a[i] - a[j]) + mx[j]);
        }
        mx[j] = max(mx[j], abs(a[i] - a[j]));
      }
    }
  }
  for (int i = 1; i < n; i++) {
    if (tg[i] != tg[n]) {
      if (dp[n] != dp[i] + abs(a[i] - a[n]) ||
          dp[n] == dp[i] + abs(a[i] - a[n]) && i1[n] != i)
        ans = max(ans, dp[n] + abs(a[i] - a[n]));
    }
  }
  cout << ans << endl;
}
int main() {
  startTime = clock();
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios_base::sync_with_stdio(false);
  bool llololcal = false;
  int TC = 1;
  cin >> TC;
  for (int test = 1; test <= TC; test++) {
    42;
    solve(test);
  }
  if (llololcal)
    cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;
  return 0;
}
