#include <bits/stdc++.h>
using namespace std;
int tag[5005], s[5005];
long long int mx1[5005];
long long int tmp[5005];
int main(void) {
  int t;
  scanf("%d", &t);
  while (t--) {
    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", &s[i]);
    int now = 0;
    for (int i = 0; i <= n; i++) {
      mx1[i] = 0;
    }
    mx1[0] = -1e18;
    long long int ans = 0;
    for (int i = 1; i <= n; i++) {
      long long int mx2 = -1e18;
      for (int j = 0; j <= i; j++) tmp[j] = -1e18;
      for (int j = i - 1; j >= 1; j--) {
        if (tag[i] != tag[j]) {
          long long int j_i = mx1[j] + abs(s[i] - s[j]);
          long long int i_j = mx2 + abs(s[i] - s[j]);
          mx2 = max(mx2, j_i);
          ans = max(ans, max(i_j, j_i));
          tmp[j] = max(tmp[j], i_j);
          tmp[i] = max(tmp[i], j_i);
        }
      }
      for (int j = i; j >= 1; j--) {
        mx1[i] = max(mx1[i], tmp[i]);
        mx1[j] = max(mx1[j], tmp[j]);
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
