#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  scanf("%d", &n);
  vector<long long> tags(n);
  vector<long long> slst(n);
  for (int i = 0; i < n; i++) scanf("%llu", &tags[i]);
  for (int i = 0; i < n; i++) scanf("%llu", &slst[i]);
  vector<long long> dp(n);
  vector<long long> dp2(n);
  for (int i = 0; i < n; i++) {
    dp2[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dp[j] = dp2[j];
      dp2[j] = 0;
    }
    for (int j = i - 1; j >= 0; j--) {
      if (tags[i] != tags[j]) {
        dp2[j] = max(dp[j], max(dp2[j], dp2[i] + abs(slst[i] - slst[j])));
        dp2[i] = max(dp2[i], dp[j] + abs(slst[i] - slst[j]));
      }
    }
  }
  long long ans = 0;
  for (auto d : dp2) {
    ans = max(ans, d);
  }
  printf("%llu\n", ans);
}
int main(void) {
  int t;
  scanf("%d", &t);
  while (t--) solve();
}
