#include <bits/stdc++.h>
using namespace std;
const int MaxN = 1e7;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
int main() {
  int T = 1;
  cin >> T;
  while (T--) {
    int N;
    cin >> N;
    int tag[N + 1], score[N + 1];
    for (int i = 1; i <= N; i++) {
      cin >> tag[i];
    }
    for (int i = 1; i <= N; i++) {
      cin >> score[i];
    }
    vector<long long> dp(N + 1, 0);
    long long ma = 0;
    for (int j = 2; j <= N; j++) {
      for (int i = j - 1; i >= 1; i--) {
        if (tag[i] != tag[j]) {
          long long fi = dp[i], se = dp[j];
          long long toAdd = abs(score[i] - score[j]);
          dp[i] = (se + toAdd);
          dp[j] = (fi + toAdd);
          ma = max(ma, max(dp[i], dp[j]));
          ;
        }
      }
    }
    cout << ma << "\n";
  }
}
