#include <bits/stdc++.h>
using namespace std;
const int mo = 998244353;
int fact[400005];
int inv[400005];
const int N = 1e5 + 5;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> tag(n + 1);
    vector<int> score(n + 1);
    for (int i = 1; i <= n; i++) {
      cin >> tag[i];
    }
    for (int i = 1; i <= n; i++) {
      cin >> score[i];
    }
    vector<int> dp(n + 1);
    for (int big = 1; big <= n; big++) {
      for (int x = big - 1; x >= 1; x--) {
        if (tag[big] != tag[x]) {
          int bign = max(dp[big], dp[x] + abs(score[x] - score[big]));
          int xn = max(dp[x], dp[big] + abs(score[x] - score[big]));
          dp[big] = bign;
          dp[x] = xn;
        }
      }
    }
    cout << *max_element((dp).begin(), (dp).end()) << "\n";
  }
}
