#include <bits/stdc++.h>
using namespace std;
const long long maxn = 5e3 + 5;
long long arr[maxn];
long long arr2[maxn];
long long ma1;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    long long a[n];
    long long b[n];
    for (auto &h : a) cin >> h;
    for (auto &h : b) cin >> h;
    ma1 = 0;
    for (long long i = 0; i <= n; ++i) {
      arr[i] = 0;
      arr2[i] = 0;
    }
    for (long long i = (n - 1); i >= 0; --i) {
      long long currma = 0;
      for (long long j = 0; j <= n; ++j) arr2[j] = 0;
      for (long long j = 0; j < i; ++j) {
        if (a[i] == a[j]) continue;
        long long h1 = max(currma, arr[i]) + abs(b[i] - b[j]);
        long long h2 = arr[j] + abs(b[i] - b[j]);
        currma = max(currma, h2);
        ma1 = max(ma1, h1);
        arr2[j] = max(arr[j], h1);
      }
      for (long long i = 0; i <= n; ++i) arr[i] = arr2[i];
    }
    cout << ma1 << '\n';
  }
  return 0;
}
