#include <bits/stdc++.h>
using namespace std;
ostream cnull(nullptr);
static const int __39 = []() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  return 39;
}();
char _, __;
const int RANDOM =
    chrono::high_resolution_clock::now().time_since_epoch().count();
const uint64_t C = long long(4e18 * acos(0)) | 71;
struct chash {
  long long operator()(long long x) const {
    return __builtin_bswap64((x ^ RANDOM) * C);
  }
  long long operator()(pair<int, int> x) const {
    return operator()((long long(x.first) << 32) | x.second);
  }
};
long long solve() {
  int n;
  cin >> n;
  vector<int> tag(n);
  vector<int> score(n);
  for (int i = 0; i < (n); ++i) cin >> tag[i];
  for (int i = 0; i < (n); ++i) cin >> score[i];
  vector<long long> dp(n);
  for (int i = 0; i < (n); ++i) {
    for (int j = i - 1; j >= 0; j--) {
      if (tag[i] != tag[j]) {
        long long s = abs(score[i] - score[j]);
        long long x = dp[j] + s;
        long long y = dp[i] + s;
        dp[i] = x;
        dp[j] = y;
      }
    }
  }
  return *max_element(begin(dp), end(dp));
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    cout << solve() << "\n";
  }
  return 0;
}
