#include <bits/stdc++.h>
using INT = int;
using namespace std;
auto &_ = std::ignore;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
constexpr struct {
  template <class T>
  constexpr operator T() const {
    return numeric_limits<T>::max();
  }
} INF;
constexpr struct {
  template <class T>
  constexpr operator T() const {
    return numeric_limits<T>::min();
  }
} MINF;
template <class T>
auto _format(ostream &os, const char *c, const T &cv)
    -> decltype(os << cv, c + 1) {
  os << cv;
  while (*c != '}') c++;
  return c + 1;
}
template <size_t i, class T>
auto _format(ostream &os, const char *c, const T &cv)
    -> decltype(typename enable_if<i == tuple_size<T>::value>::type(1), c + 1) {
  return c;
}
template <size_t i = 0, class T>
auto _format(ostream &os, const char *c, const T &cv)
    -> decltype(typename enable_if<i != tuple_size<T>::value>::type(1), c + 1) {
  while (*c != '{') os << *c++;
  c = _format(os, c, get<i>(cv));
  return _format<i + 1>(os, c, cv);
}
template <class T>
auto _format(ostream &os, const char *c, const T &cv)
    -> decltype(begin(cv), c + 1) {
  const char *ct = c + 1;
  size_t ic = 0;
  for (auto &i : cv) {
    const char *cc = c + 1;
    while (*cc != '{') {
      if (*cc == 'i')
        os << ic, cc++;
      else
        os << *cc++;
    }
    cc = _format(os, cc, i);
    while (*cc != '}') os << *cc++;
    ct = cc;
    ic++;
  }
  return ct + 1;
}
string format(const char *c) { return string(c); }
template <class T, class... Args>
string format(const char *c, const T &a, Args &&...rest) {
  ostringstream os;
  while (*c != '{' && *c != '\0') os << *c++;
  if (*c == '{') c = _format(os, c, a);
  return os.str() + format(c, forward<Args>(rest)...);
}
template <class... Args>
ostream &print(Args &&...rest) {
  return cout << format(forward<Args>(rest)...);
}
template <class T>
void _read(istream &is, T &a) {
  is >> a;
}
void _read(istream &is, ll &a) {
  a = 0;
  bool f = false;
  char c;
  while (!isdigit(c = is.get()) && is.good()) f = c == '-';
  if (!is.good()) return;
  do a = (a << 3) + (a << 1) + c - '0';
  while (isdigit(c = is.get()));
  if (f) a = -a;
}
void read() {}
template <class T, class... Args>
void read(T &a, Args &...args) {
  _read(cin, a);
  read(args...);
}
template <class T>
void reada(vector<T> &v, size_t n) {
  v.reserve(n);
  for (size_t i = 0; i < n; i++) {
    T a;
    cin >> a;
    v.emplace_back(move(a));
  }
}
template <class T1, class T2>
bool cmin(T1 &a, const T2 b) {
  return a > b ? a = b, 1 : 0;
}
template <class T1, class T2>
bool cmax(T1 &a, const T2 b) {
  return a < b ? a = b, 1 : 0;
}
template <class T1, class T2, class... T3>
bool cmin(T1 &a, const T2 b, const T3... rest) {
  return cmin(a, b) | cmin(a, rest...);
}
template <class T1, class T2, class... T3>
bool cmax(T1 &a, const T2 b, const T3... rest) {
  return cmax(a, b) | cmax(a, rest...);
}
bool MULTIDATA = true;
struct solution {
  int n;
  vi tag, s;
  struct ss {
    int i, j;
    bool operator<(const ss &a) const {
      if (a.i > i) return true;
      if (i > a.i) return false;
      if (a.j < j) return true;
      if (j < a.j) return false;
      return false;
    }
  };
  void scan() {
    read(n);
    reada(tag, n);
    reada(s, n);
  }
  int solve() {
    vector<int> G(n, 0);
    for (int i = 0; i < n; i++)
      for (int j = i - 1; j >= 0; j--)
        if (tag[i] != tag[j]) {
          int w = s[i] - s[j];
          w = abs(w);
          int a = G[j], b = G[i];
          cmax(G[i], a + w);
          cmax(G[j], b + w);
        }
    long long count = 0;
    for (int i = 0; i < n; i++) cmax(count, G[i]);
    cout << count << endl;
    return 0;
  }
};
INT main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int T = 1;
  if (MULTIDATA) read(T);
  while (T--) {
    auto a = unique_ptr<solution>(new solution());
    a->scan();
    a->solve();
    if (!cin.good()) break;
  }
  return 0;
}
