#include <bits/stdc++.h>
using namespace std;
using longs = long long;
using uint = unsigned;
using ulongs = unsigned long long;
using longd = long double;
using lll = __int128;
template <class printable>
void trace(const char *name, printable &&value) {
  cerr << name << " = " << value << endl;
}
template <class printable, class... args>
void trace(const char *names, printable &&value, args &&...list) {
  const char *separate = strchr(names + 1, ',');
  cerr.write(names, separate - names) << " = " << value << ',';
  trace(separate + 1, list...);
}
template <class T>
void print(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  static char str[40];
  int cnt = 0;
  while (x > 9) {
    str[cnt++] = (x % 10) + 48;
    x /= 10;
  }
  str[cnt++] = x + 48;
  while (cnt--) putchar(str[cnt]);
}
void print(const char *s) { fputs(s, stdout); }
void print(char *s) { fputs(s, stdout); }
void print(string &s) { print(s.c_str()); }
void print(char ch) { putchar(ch); }
template <class T, class... Ts>
void print(T x, Ts... xs) {
  print(x), print(xs...);
}
void println() { puts(""); }
template <class T>
void println(T x) {
  print(x), putchar('\n');
}
static class Scanner {
  template <class T>
  inline T read() {
    T x = 0;
    int f = 0, ch = getchar();
    while (!isdigit(ch)) ch == '-' && (f = !f), ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
    return f ? -x : x;
  }
  static inline bool isBlank(int x) { return x == ' ' || x == '\n'; }

 public:
  template <class T>
  void operator()(T &x) {
    x = read<T>();
  }
  template <class T, class... Ts>
  void operator()(T &x, Ts &...y) {
    (*this)(x), (*this)(y...);
  }
  template <class T, class... Ts>
  void operator()(T &&x, Ts &&...y) {
    (*this)(x), (*this)(y...);
  }
  void operator()(char *x) { scanf("%s", x); }
  void operator()(char &x) {
    do x = (char)getchar();
    while (isBlank(x));
  }
  int nextInt() { return read<int>(); }
  longs nextLongs() { return read<longs>(); }
  lll nextInt128() { return read<lll>(); }
  char nextChar() {
    char x;
    (*this)(x);
    return x;
  }
} scanner;
struct fast_read {
  template <class name>
  fast_read operator,(name &x) {
    scanner(x);
    return fast_read{};
  }
} fastRead;
const int N = 5050;
int s[N], tag[N];
longs dp[N];
signed main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  int T = scanner.nextInt();
  while (T--) {
    int n = scanner.nextInt();
    for (int i = 1; i <= n; ++i) tag[i] = scanner.nextInt();
    for (int i = 1; i <= n; ++i) s[i] = scanner.nextInt();
    for (int i = 1; i <= n; ++i)
      for (int j = i - 1; j; --j)
        if (tag[i] != tag[j]) {
          auto ii = dp[i], jj = dp[j];
          auto add = abs(s[i] - s[j]);
          ((dp[i]) = max({(dp[i]), jj + add}));
          ((dp[j]) = max({(dp[j]), ii + add}));
        }
    println(*max_element(dp + 1, dp + 1 + n));
  }
  return 0;
}
