#include <bits/stdc++.h>
using namespace std;
void do_test() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<int> b(n);
  for (int i = 0; i < n; i++) cin >> b[i];
  vector<long long> dp(n);
  for (int i = 1; i < n; i++) {
    for (int j = i - 1; j >= 0; j--) {
      if (a[i] == a[j]) continue;
      int maxi_1 = max(dp[i], dp[j] + abs(b[i] - b[j]));
      int maxi_2 = max(dp[j], dp[i] + abs(b[i] - b[j]));
      dp[i] = maxi_1;
      dp[j] = maxi_2;
    }
  }
  int maxi = *max_element(dp.begin(), dp.end());
  cout << maxi << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) do_test();
}
