#include <bits/stdc++.h>
using namespace std;
int main() {
  int tc, n, vi, vj, diff, sol;
  cin >> tc;
  while (tc--) {
    cin >> n;
    vector<int> tags(n), scores(n);
    for (int i = 0; i < n; ++i) cin >> tags[i];
    for (int i = 0; i < n; ++i) cin >> scores[i];
    vector<long long> dp(n);
    for (int j = 1; j < n; ++j) {
      for (int i = j - 1; i >= 0; --i) {
        if (tags[i] == tags[j]) continue;
        vi = dp[i];
        vj = dp[j];
        diff = abs(scores[i] - scores[j]);
        dp[i] = max(vi, vj + diff);
        dp[j] = max(vj, vi + diff);
      }
    }
    sol = 0;
    for (int i = 0; i < n; ++i)
      if (dp[i] > sol) sol = dp[i];
    cout << sol << '\n';
  }
  return 0;
}
