#include <bits/stdc++.h>
using namespace std;
const int MN = 5005, inf = 1000000005, mod = 1000000007;
const long long INF = 1000000000000000005LL;
long long prawo[MN];
int tag[MN], score[MN];
void solve() {
  long long res = 0LL;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &tag[i]);
  for (int i = 1; i <= n; ++i) scanf("%d", &score[i]);
  for (int i = n; i > 0; --i) {
    prawo[i] = 0LL;
    for (int j = i + 1; j <= n; ++j)
      if (tag[j] != tag[i])
        prawo[i] =
            max(prawo[i], (long long)abs(score[i] - score[j]) + prawo[j]);
    res = max(res, prawo[i]);
  }
  for (int i = 1; i <= n; ++i) {
    long long pocz = 0;
    for (int j = i + 2; j <= n; ++j) {
      if (tag[j - 1] != tag[i])
        pocz = max(pocz, (long long)abs(score[j - 1] - score[i]));
      if (tag[j] != tag[i])
        res = max(res, pocz + (long long)abs(score[j] - score[i]) + prawo[j]);
    }
  }
  printf("%lld\n", res);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
}
