#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
int tag[N], s[N];
long long dp[N];
long long tmp[N];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &tag[i]);
      dp[i] = 0;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%d", &s[i]);
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j < i; j++) {
        if (tag[j] != tag[i]) {
          dp[i] = max(dp[i], dp[j] + abs(s[i] - s[j]));
          tmp[j] = dp[j] + abs(s[i] - s[j]);
        }
      }
      long long mx = -1;
      for (int j = i - 1; j > 0; j--) {
        if (tag[j] != tag[i]) {
          if (mx != -1) dp[j] = max(dp[j], abs(s[i] - s[j]) + mx);
          mx = max(mx, tmp[j]);
        }
      }
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      ans = max(ans, dp[i]);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
