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