#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n;
  cin >> n;
  vector<long long int> tag(n + 1);
  for (long long int j = 1; j <= n; j++) cin >> tag[j];
  vector<long long int> sc(n + 1);
  for (long long int j = 1; j <= n; j++) cin >> sc[j];
  vector<long long int> dp(n + 1, 0);
  dp[1] = 0;
  for (long long int j = 2; j <= n; j++) {
    long long int temp = 0;
    for (long long int i = 1; i < j; i++) {
      if (tag[i] != tag[j]) {
        temp = max(temp, dp[i] + abs(sc[i] - sc[j]));
      }
    }
    vector<long long int> event(j, 0);
    for (long long int i = j - 1; i >= 1; i--) {
      if (tag[i] != tag[j]) {
        event[i - 1] = dp[i] + abs(sc[i] - sc[j]);
      }
    }
    long long int first = 2;
    for (long long int i = j - 1; i >= 1; i--) {
      if (tag[i] != tag[j]) {
        break;
      }
      first++;
    }
    long long int curr_mx = 0;
    if (j - first + 1 >= 1) curr_mx = event[j - first + 1];
    for (long long int i = j - first; i >= 1; i--) {
      curr_mx = max(curr_mx, event[i]);
      if (tag[i] != tag[j]) {
        dp[i] = max(dp[i], curr_mx + abs(sc[i] - sc[j]));
      }
    }
    dp[j] = temp;
  }
  long long int res = 0;
  for (long long int j = 1; j <= n; j++) res = max(res, dp[j]);
  cout << res << '\n';
  return;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
