#include <bits/stdc++.h>
using namespace std;
vector<int> tag;
vector<long long> s;
int n;
vector<long long> dp;
long long solve(pair<int, int> iq, int i) {
  if (dp[i] != -1) return dp[i];
  long long ans = 0;
  for (int j = 1; j <= n; j++) {
    if (tag[i] != tag[j]) {
      if (i > j) {
        bool cond1 = i > iq.first;
        bool cond2 = (i == iq.first && j < iq.second);
        if (cond1 || cond2) ans = max(ans, abs(s[i] - s[j]) + solve({i, j}, j));
      } else {
        bool cond1 = j > iq.first;
        bool cond2 = (j == iq.first && i < iq.second);
        if (cond1 || cond2) ans = max(ans, abs(s[i] - s[j]) + solve({j, i}, j));
      }
    }
  }
  dp[i] = ans;
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    cin >> n;
    dp = vector<long long>(n + 1, -1);
    tag = vector<int>(n + 1);
    s = vector<long long>(n + 1);
    for (int i = 1; i <= n; i++) {
      cin >> tag[i];
    }
    for (int i = 1; i <= n; i++) {
      cin >> s[i];
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      ans = max(ans, solve({0, 0}, i));
    }
    cout << ans << '\n';
  }
  return 0;
}
