#include <bits/stdc++.h>
using namespace std;
const int N = 10010;
int tag[N];
int s[N];
int T, n;
long long ans;
long long dp[N];
int main() {
  scanf("%d", &T);
  while (T--) {
    ans = 0;
    memset(dp, 0, sizeof(dp));
    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 j = 2; j <= n; j++) {
      for (int i = j - 1; i; i--) {
        if (tag[i] == tag[j]) continue;
        long long tx = dp[i];
        long long ty = dp[j];
        long long dt = abs(s[i] - s[j]);
        dp[i] = max(dp[i], ty + dt);
        dp[j] = max(dp[j], tx + dt);
      }
    }
    for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
    printf("%d\n", ans);
  }
  return 0;
}
