#include <bits/stdc++.h>
using namespace std;
int N;
long long D[5050];
int tag[5050];
int S[5050];
int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    scanf("%d", &N);
    for (int i = 1; i <= N; i++) scanf("%d", &tag[i]);
    for (int i = 1; i <= N; i++) scanf("%d", &S[i]);
    for (int i = 1; i <= N; i++) D[i] = 0;
    D[1] = 0;
    D[2] = (tag[1] == tag[2] ? 0 : abs(S[1] - S[2]));
    long long ans = D[2];
    for (int i = 3; i <= N; i++) {
      for (int j = 1; j < i; j++) {
        if (tag[i] == tag[j]) continue;
        D[i] = max(D[i], D[j] + abs(S[i] - S[j]));
      }
      long long t = D[i];
      long long mx = 0;
      for (int j = 1; j < i - 2; j++) {
        if (!(tag[j] == tag[i - 1] || tag[j] == tag[i])) {
          long long now = D[i - 1] + abs(S[i - 1] - S[j]) + abs(S[i] - S[j]);
          D[i] = max(D[i], now);
          ans = max(ans, now + mx);
        }
        if (tag[j] != tag[i]) mx = max(mx, abs((long long)S[i] - S[j]));
      }
      if (tag[i - 2] != tag[i]) mx = max(mx, abs((long long)S[i] - S[i - 2]));
      ans = max(ans, t + mx);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
