#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
const int _ = []() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(10);
  return 0;
}();
template <typename value_t>
void resize(value_t& v, const value_t& val) {
  v = val;
}
template <typename vec_t, typename value_t, typename... arg_t>
void resize(std::vector<vec_t>& v, const value_t& val, int size, arg_t... arg) {
  v.resize(size);
  for (auto& c : v) resize(c, val, arg...);
}
template <typename A, typename B>
void chmin(A& a, const B& b) {
  a = min(a, static_cast<A>(b));
};
template <typename A, typename B>
void chmax(A& a, const B& b) {
  a = max(a, static_cast<A>(b));
};
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n;
    cin >> n;
    vector<int> t(n), s(n), v(n, 0);
    for (ll i = 0; i < n; ++i) cin >> t[i];
    for (ll i = 0; i < n; ++i) cin >> s[i];
    for (ll i = 1; i < n; ++i) {
      for (ll j = i - 1; j > -1; --j) {
        if (t[i] == t[j]) continue;
        int tmp = v[i];
        chmax(v[i], v[j] + abs(s[i] - s[j]));
        chmax(v[j], tmp + abs(s[i] - s[j]));
      }
    }
    cout << *max_element(v.begin(), v.end()) << endl;
  }
  return 0;
}
