#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
int n;
int tag[MAXN], s[MAXN], dp[MAXN];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    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);
    memset(dp, 0, sizeof(int) * (n + 1));
    for (int j = 2; j <= n; j++)
      for (int i = j - 1; i > 0; i--)
        if (tag[i] != tag[j]) {
          int dpi = max(dp[i], dp[j] + abs(s[i] - s[j]));
          int dpj = max(dp[j], dp[i] + abs(s[i] - s[j]));
          dp[i] = dpi, dp[j] = dpj;
        }
    int ans = 0;
    for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
    printf("%d\n", ans);
  }
  return 0;
}
