#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
template <class T>
bool ckmin(T &a, T b) {
  return a > b ? a = b, true : false;
}
template <class T>
bool ckmax(T &a, T b) {
  return a < b ? a = b, true : false;
}
void solve() {
  int n;
  cin >> n;
  vi dp(n), t(n), s(n);
  for (auto &x : t) cin >> x;
  for (auto &x : s) cin >> x;
  for (int i = 0; i < n; ++i) {
    for (int j = i - 1; j >= 0; --j) {
      if (j < 0 || t[i] == t[j]) continue;
      int a = dp[i], b = dp[j];
      ckmax(dp[i], b + abs(s[i] - s[j]));
      ckmax(dp[j], a + abs(s[i] - s[j]));
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) ckmax(ans, dp[i]);
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
