#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    vector<int> T(n, 0), S(n, 0);
    for (int i = 0; i < n; i++) {
      cin >> T[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> S[i];
    }
    vector<int> m1(n, 0), m2(n, 0);
    for (int i = 0; i < n; i++) {
      m2.assign(n, 0);
      for (int j = i - 1; j > -1; j--) {
        if (T[i] != T[j]) {
          m1[i] = max(m1[j] + abs(S[i] - S[j]), m1[i]);
        }
        m2[j] = max(m1[i], m2[j + 1]);
      }
      for (int j = 0; j < i; j++) {
        if (T[i] != T[j]) {
          m1[j] = max(m1[j], m2[j + 1] + abs(S[i] - S[j]));
        }
      }
    }
    int m = 0;
    for (int i = 0; i < n; i++) {
      m = max(m, m1[i]);
    }
    cout << m << endl;
  }
  return 0;
}
