#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 10;
long long dp[maxn];
int n, t[maxn], s[maxn];
int main() {
  int test;
  cin >> test;
  while (test--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) dp[i] = 0;
    for (int i = 1; i <= n; i++) scanf("%d", &t[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &s[i]);
    for (int i = 2; i <= n; i++) {
      for (int j = i - 1; j >= 1; j--) {
        if (t[i] == t[j]) continue;
        long long dpi = dp[i], dpj = dp[j];
        int d = abs(s[i] - s[j]);
        dp[i] = max(dp[i], dpj + d);
        dp[j] = max(dp[j], dpi + d);
      }
    }
    printf("%d\n", *max_element(dp + 1, dp + n + 1));
  }
  return 0;
}
