#include <bits/stdc++.h>
using namespace std;
const int N = 0 ? 100 : 5007;
int n;
int tag[N];
int s[N];
int dp[N];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int _t;
  cin >> _t;
  for (int _tn = 0; _tn < _t; ++_tn) {
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> tag[i];
      --tag[i];
    }
    for (int i = 0; i < n; ++i) {
      cin >> s[i];
    }
    fill(dp, dp + n, 0);
    for (int i = 0; i < n; ++i) {
      int cur = 0;
      for (int j = i - 1; j >= 0; --j) {
        if (tag[i] != tag[j]) {
          int temp = dp[j] + abs(s[i] - s[j]);
          dp[j] = max(dp[j], cur + abs(s[i] - s[j]));
          cur = max(cur, temp);
        }
      }
      dp[i] = max(dp[i], cur);
    }
    cout << *max_element(dp, dp + n) << '\n';
  }
  return 0;
}
