#include <bits/stdc++.h>
using namespace std;
const int MX = 5005;
int n, tag[MX];
long long res, dp[MX], s[MX];
void solve() {
  cin >> n;
  for (int i = 0; i < int(n); i++) cin >> tag[i];
  for (int i = 0; i < int(n); i++) cin >> s[i];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < int(n); i++) {
    for (int j = i - 1; j >= 0; j--) {
      if (tag[i] != tag[j]) {
        int aux = dp[j];
        dp[j] = max(dp[j], dp[i] + abs(s[i] - s[j]));
        dp[i] = max(dp[i], aux + abs(s[i] - s[j]));
      }
    }
  }
  res = 0;
  for (int i = 0; i < int(n); i++) res = max(res, dp[i]);
  cout << res << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
