#include <bits/stdc++.h>
using namespace std;
void print();
template <typename T, typename... Args>
void print(T x, Args... args);
void solve() {
  int n;
  cin >> n;
  vector<int> t(n), s(n);
  for (int i = 0; i < n; ++i) cin >> t[i];
  for (int i = 0; i < n; ++i) cin >> s[i];
  vector<int> dp(n);
  for (int i = 0; i < n; ++i)
    for (int j = i - 1; j >= 0; --j)
      if (t[i] != t[j]) {
        int oldi = dp[i];
        int oldj = dp[j];
        if (oldj + abs(s[i] - s[j]) > dp[i]) dp[i] = oldj + abs(s[i] - s[j]);
        if (oldi + abs(s[i] - s[j]) > dp[j]) dp[j] = oldi + abs(s[i] - s[j]);
      }
  print(*max_element(dp.begin(), dp.end()));
}
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  int t = 1;
  cin >> t;
  for (; t--;) solve();
  return 0;
}
void print() { cout << "\n"; }
template <typename T, typename... Args>
void print(T x, Args... args) {
  if (sizeof...(args)) {
    cout << x << ' ';
    print(args...);
  } else {
    cout << x << '\n';
  }
}
