#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int N = 2e5 + 10;
const int M = 4e5 + 10;
const int Mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int _inf = 0xcfcfcfcf;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int P = 13331;
const double eps = 1e-9;
const double PI = acos(-1.0);
int n, m, k;
int tag[N], s[N];
long long f[N];
int main() {
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    memset(f, 0, sizeof f);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> tag[i];
    for (int i = 1; i <= n; i++) cin >> s[i];
    for (int i = 1; i <= n; i++) {
      for (int j = i - 1; j >= 1; j--) {
        if (tag[i] == tag[j]) continue;
        int first = f[i], fj = f[j];
        int val = abs(s[i] - s[j]);
        f[i] = ((f[i]) < (fj + val) ? (fj + val) : (f[i]));
        f[j] = ((f[j]) < (first + val) ? (first + val) : (f[j]));
      }
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++) ans = ((ans) < (f[i]) ? (f[i]) : (ans));
    cout << ans << endl;
  }
  return 0;
}
