#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
long long tag[MAXN], s[MAXN], dp[MAXN];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%lld", &tag[i]);
    for (int i = 1; i <= n; i++) scanf("%lld", &s[i]);
    for (int i = 1; i <= n; i++) dp[i] = 0;
    for (int j = 2; j <= n; j++) {
      for (int i = j - 1; i >= 1; i--) {
        if (tag[i] == tag[j]) continue;
        long long dpi = dp[i], dpj = dp[j], p = abs(s[i] - s[j]);
        dp[i] = max(dp[i], dpj + p);
        dp[j] = max(dp[j], dpi + p);
      }
    }
    long long res = 0;
    for (int i = 1; i <= n; i++) res = max(res, dp[i]);
    printf("%d\n", res);
  }
}
