#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
int tag[N], s[N];
int f[N];
int main() {
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) {
    int n, ans = 0;
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> tag[i];
    for (int i = 1; i <= n; ++i) cin >> s[i];
    memset(f, 0, sizeof(f));
    for (int j = 2; j <= n; ++j)
      for (int i = j - 1; i; --i) {
        if (tag[i] == tag[j]) continue;
        int ti = f[i], tj = f[j];
        f[j] = max(f[j], ti + abs(s[i] - s[j]));
        f[i] = max(f[i], tj + abs(s[i] - s[j]));
      }
    for (int i = 1; i <= n; ++i) ans = max(ans, f[i]);
    cout << ans << endl;
  }
  return 0;
}
