#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
long long a[N];
int tag[N];
long long dp[N];
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> tag[i];
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    dp[i] = 0;
  }
  for (int i = 2; i <= n; ++i) {
    for (int j = i - 1; j >= 1; --j) {
      if (tag[i] == tag[j]) continue;
      int dpi = dp[i], dpj = dp[j];
      long long val = abs(a[i] - a[j]);
      dp[i] = max(dp[i], dpj + val);
      dp[j] = max(dp[j], dpi + val);
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) ans = max(ans, dp[i]);
  cout << ans << '\n';
}
int main() {
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
