#include <bits/stdc++.h>
using namespace std;
int s[5005], tag[5005], aut[5005];
long long dp[5005];
int main() {
  int t, n;
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    memset(dp, 0, sizeof(dp));
    memset(aut, 0, sizeof(aut));
    for (int i = 1; i <= n; i++) scanf("%d", tag + i);
    for (int i = 1; i <= n; i++) scanf("%d", s + i);
    dp[1] = 0;
    long long ans = 0;
    for (int i = 2; i <= n; i++) {
      for (int j = i - 1; j > 0; j--) {
        if (tag[i] != tag[j]) {
          long long res = dp[j] + 1ll * abs(s[i] - s[j]);
          if (res > dp[i]) {
            dp[i] = res;
            aut[i] = j;
          }
        }
      }
      for (int j = aut[i] - 1; j > 0; j--) {
        if (tag[i] != tag[j]) {
          long long res = dp[i] + 1ll * abs(s[i] - s[j]);
          if (res > dp[j]) {
            dp[j] = res;
            aut[j] = i;
          }
        }
      }
    }
    for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
    printf("%lld\n", ans);
  }
  return 0;
}
