#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long double EPS = 1e-9;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    long long tag[n];
    long long s[n];
    for (int i = (0); i < (n); i++) cin >> tag[i];
    for (int i = (0); i < (n); i++) cin >> s[i];
    long long prepro[n][n];
    long long pos[n][n];
    pos[0][0] = 0;
    prepro[0][0] = -INF;
    for (int i = (0); i < (n); i++) {
      for (int j = (0); j < (i); j++) {
        if (j == 0) {
          prepro[i][j] = abs(s[i] - s[j]);
          pos[i][j] = 0;
          continue;
        }
        if (tag[i] == tag[j]) {
          pos[i][j] = pos[i][j - 1];
          prepro[i][j] = prepro[i][j - 1];
          continue;
        }
        if (prepro[i][j - 1] > abs(s[i] - s[j]))
          pos[i][j] = pos[i][j - 1];
        else
          pos[i][j] = j;
        prepro[i][j] = max(prepro[i][j - 1], abs(s[i] - s[j]));
      }
    }
    long long dp[n];
    long long pre[n];
    memset(pre, 0, sizeof pre);
    ;
    memset(dp, 0, sizeof dp);
    ;
    for (int i = (0); i < (n); i++) {
      for (int j = (i + 1); j < (n); j++) {
        if (tag[i] != tag[j]) {
          if (dp[j] < dp[i] + abs(s[i] - s[j])) {
            dp[j] = dp[i] + abs(s[i] - s[j]);
            pre[j] = i;
          } else if (dp[j] == dp[i] + abs(s[i] - s[j])) {
            pre[j] = i;
          }
        }
        if (pre[i] == 0) continue;
        if (tag[pos[i][pre[i] - 1]] == tag[j]) continue;
        if (dp[j] <
            dp[i] + prepro[i][pre[i] - 1] + abs(s[pos[i][pre[i] - 1]] - s[j])) {
          dp[j] =
              dp[i] + prepro[i][pre[i] - 1] + abs(s[pos[i][pre[i] - 1]] - s[j]);
          pre[j] = pos[i][pre[i]];
        } else if (dp[j] == dp[i] + prepro[i][pre[i] - 1] +
                                abs(s[pos[i][pre[i] - 1]] - s[j])) {
          if (pre[j] < pos[i][pre[i] - 1]) pre[j] = pos[i][pre[i] - 1];
        }
      }
    }
    if (pre[n - 1] != 0) dp[n - 1] += prepro[n - 1][pre[n - 1] - 1];
    cout << dp[n - 1] << endl;
  }
}
