#include <bits/stdc++.h>
using namespace std;
const int maxn = 5005;
int n, tag[maxn], s[maxn], dp[maxn];
void readf() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> tag[i];
  for (int i = 1; i <= n; ++i) cin >> s[i];
}
void solve() {
  int ans = 0;
  for (int i = 1; i <= n; ++i) dp[i] = 0;
  for (int i = 2; i <= n; ++i)
    for (int j = i - 1; j > 0; --j)
      if (tag[i] != tag[j]) {
        int dpi = dp[i];
        int dpj = dp[j];
        dp[i] = max(dp[i], dpj + abs(s[i] - s[j]));
        dp[j] = max(dp[j], dpi + abs(s[i] - s[j]));
        ans = max({ans, dp[i], dp[j]});
      }
  cout << ans << '\n';
}
int main() {
  srand(time(NULL));
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    readf();
    solve();
  }
}
