#include <bits/stdc++.h>
using namespace std;
void single_case() {
  long long N;
  cin >> N;
  vector<long long> tags(N + 1, 0);
  vector<long long> scos(N + 1, 0);
  for (long long i = 1; i <= N; i++) {
    cin >> tags[i];
  }
  for (long long i = 1; i <= N; i++) {
    cin >> scos[i];
  }
  vector<long long> rdp1(N + 1, 0);
  vector<long long> rdp2(N + 1, 0);
  vector<long long> dpmax(N + 1, 0);
  long long maxval = 0;
  for (long long j = 2; j <= N; j++) {
    auto& dp1 = j % 2 == 1 ? rdp1 : rdp2;
    auto& dp2 = j % 2 == 0 ? rdp1 : rdp2;
    for (auto& c : dp2) c = 0;
    for (long long jp = j - 1; jp >= 1; jp--) {
      if (tags[jp] == tags[j]) {
        dp2[jp] = 0;
      } else {
        dp2[jp] = dpmax[jp] + abs(scos[jp] - scos[j]);
      }
    }
    long long max_latter = 0;
    for (long long i = j - 2; i >= 1; i--) {
      max_latter = max(max_latter, dp1[i + 1]);
      if (tags[i] == tags[j] || tags[i] == tags[j - 1]) continue;
      long long new_former =
          abs(scos[j - 1] - scos[i]) + abs(scos[i] - scos[j]);
      dp2[i] = max(dp2[i], new_former + max_latter);
    }
    for (auto c : dp2) dpmax[j] = max(dpmax[j], c);
    long long maxadd = 0;
    for (long long i = 1; i <= j; i++) {
      if (tags[i - 1] != tags[j] && i > 1)
        maxadd = max(maxadd, abs(scos[i - 1] - scos[j]));
      long long origin = dp2[i];
      long long rst = origin + maxadd;
      maxval = max(rst, maxval);
    }
  }
  auto& dp2 = N % 2 == 0 ? rdp1 : rdp2;
  long long maxadd = 0;
  for (long long i = 1; i <= N; i++) {
    if (tags[i - 1] != tags[N] && i > 1)
      maxadd = max(maxadd, abs(scos[i - 1] - scos[N]));
    long long origin = dp2[i];
    long long rst = origin + maxadd;
    maxval = max(rst, maxval);
  }
  cout << maxval << endl;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long T;
  cin >> T;
  while (T--) single_case();
}
