#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 int INF = 1e9 + 7;
const int mod = 1e9 + 7;
long long int powmod(long long int a, long long int b) {
  long long int res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int nxt() {
  int x;
  cin >> x;
  return x;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int tc = nxt();
  while (tc--) {
    int n = nxt();
    vector<int> t(n), s(n);
    generate((t).begin(), (t).end(), nxt);
    generate((s).begin(), (s).end(), nxt);
    int i = 1;
    while (i < n && t[i] == t[i - 1]) i++;
    if (i == n) {
      puts("0");
      continue;
    }
    vector<long long int> dp(n);
    for (int j = i - 1; j >= 0; j--) {
      dp[j] = abs(s[i] - s[j]);
      if (j + 1 < i) dp[j] = max(dp[j], dp[j + 1]);
    }
    for (int j = i + 1; j < n; j++) {
      vector<long long int> tmp(n);
      if (t[j] != t[j - 1]) {
        tmp[j - 1] = dp[0] + abs(s[j] - s[j - 1]);
      }
      for (int k = j - 2; k >= 0; k--) {
        if (t[k] != t[j] && t[k] != t[j - 1]) {
          long long int dist =
              dp[k + 1] + abs(s[k] - s[j]) + abs(s[k] - s[j - 1]);
          tmp[k] = dist;
        }
        tmp[k] = max(tmp[k], tmp[k + 1]);
      }
      dp = tmp;
    }
    long long int ans = dp[0];
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n))) {
      for (int j = 0; j < i; j++) {
        long long int dist = abs(s[n - 1] - s[j]);
        if (t[j] != t[n - 1]) ans = max(ans, dp[i] + dist);
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
