#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;
int n;
long long f[N], a[N], ne[N], g[N];
int tag[N];
int main() {
  int __;
  cin >> __;
  while (__--) {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> tag[i];
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) f[i] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= i; j++) ne[j] = 0;
      long long maxn = 0;
      for (int j = 1; j < i; j++) {
        long long g = 0;
        if (tag[j] == tag[i]) {
          ne[j - 1] = 0;
          continue;
        }
        g = f[j] + abs(a[j] - a[i]);
        maxn = max(maxn, g);
        ne[j - 1] = g;
      }
      long long p = 0;
      for (int j = i - 2; j >= 1; j--) {
        p = max(p, ne[j]);
        if (tag[i] == tag[j]) continue;
        f[j] = max(f[j], p + abs(a[j] - a[i]));
      }
      f[i] = max(f[i], maxn);
    }
    long long res = 0;
    for (int i = 1; i <= n; i++) res = max(res, f[i]);
    cout << res << endl;
  }
}
