#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
int a[maxn], b[maxn], dp[maxn];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];
    for (int i = 0; i < n; i++) dp[i] = 0;
    for (int i = 1; i < n; i++) {
      for (int j = i - 1; j >= 0; j--) {
        if (a[i] == a[j]) continue;
        int dpi = dp[i], dpj = dp[j], p = abs(b[i] - b[j]);
        dp[i] = max(dpj + p, dp[i]);
        dp[j] = max(dpi + p, dp[j]);
      }
    }
    int mx = 0;
    for (int i = 0; i < n; i++) mx = max(mx, dp[i]);
    cout << mx << endl;
  }
  return 0;
}
