#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long to, cost;
  edge() {}
  edge(long long a, long long b) { to = a, cost = b; }
};
const long long mod = 1000000007;
bool exceed(long long x, long long y, long long m) { return x >= m / y + 1; }
void mark() { cout << "*" << endl; }
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long modpow(long long a, long long n) {
  if (n == 0) return 1;
  if (n % 2)
    return ((a % mod) * (modpow(a, n - 1) % mod)) % mod;
  else
    return modpow((a * a) % mod, n / 2) % mod;
}
template <typename T>
void outa(T a[], long long s, long long t) {
  for (long long i = s; i <= t; i++) {
    cout << a[i];
    if (i < t) cout << " ";
  }
  cout << endl;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
  for (int i = 0; i < vec.size(); i++) {
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U>& pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& pair_var) {
  os << "(" << pair_var.first << ", " << pair_var.second << ")";
  return os;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, map<T, U>& map_var) {
  for (typename map<T, U>::iterator itr = map_var.begin(); itr != map_var.end();
       itr++) {
    os << "(" << itr->first << ", " << itr->second << ")";
    itr++;
    if (itr != map_var.end()) os << ",";
    itr--;
  }
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T>& set_var) {
  for (typename set<T>::iterator itr = set_var.begin(); itr != set_var.end();
       itr++) {
    os << *itr;
    ++itr;
    if (itr != set_var.end()) os << " ";
    itr--;
  }
  return os;
}
void dump_func() { cout << endl; }
template <class Head, class... Tail>
void dump_func(Head&& head, Tail&&... tail) {
  cout << head;
  if (sizeof...(Tail) > 0) cout << " ";
  dump_func(std::move(tail)...);
}
long long T;
long long n;
long long t[5005], s[5005];
long long dp[2][5005];
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> T;
  while (T--) {
    cin >> n;
    for (long long(i) = (1); (i) <= (n); (i)++) cin >> t[i];
    for (long long(i) = (1); (i) <= (n); (i)++) cin >> s[i];
    if (n == 1) {
      dump_func(0);
      continue;
    }
    for (long long(i) = (0); (i) <= (1); (i)++)
      for (long long(j) = (1); (j) <= (n); (j)++) dp[i][j] = -1e18;
    for (long long(i) = (2); (i) <= (n); (i)++) {
      for (long long(j) = (1); (j) <= (n); (j)++) dp[i % 2][j] = -1e18;
      long long mx = -1e18;
      for (int j = i - 2; j >= 1; j--) {
        if (t[i] != t[j])
          (dp[i % 2][j]) = max((dp[i % 2][j]), (abs(s[i] - s[j])));
        if (t[i] != t[j] && t[i - 1] != t[j])
          (dp[i % 2][j]) = max((dp[i % 2][j]),
                               (mx + abs(s[i - 1] - s[j]) + abs(s[j] - s[i])));
        (mx) = max((mx), (dp[(i - 1) % 2][j]));
      }
      if (i >= 3 && t[i] != t[1] && t[1] != t[2])
        (dp[i % 2][1]) =
            max((dp[i % 2][1]), (abs(s[i] - s[1]) + abs(s[1] - s[2])));
      if (t[i] != t[i - 1]) {
        (mx) = max((mx), (0LL));
        (dp[i % 2][i - 1]) =
            max((dp[i % 2][i - 1]), (mx + abs(s[i] - s[i - 1])));
      }
    }
    long long ans = 0;
    for (long long(j) = (1); (j) <= (n - 1); (j)++) {
      (ans) = max((ans), (dp[n % 2][j]));
      for (long long(k) = (1); (k) <= (j - 1); (k)++)
        if (t[n] != t[k]) (ans) = max((ans), (dp[n % 2][j] + abs(s[n] - s[k])));
    }
    dump_func(ans);
  }
  return 0;
}
