#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 5010;
const int maxm = 1e5 + 100;
const int maxL = 210;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int p = 23333;
const int bit = 65;
const int maxday = 2335;
int t, n;
int tag[maxn], s[maxn];
long long dp[2][maxn];
int main() {
  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]);
    }
    for (int i = 0; i < 2; i++) {
      for (int j = 1; j <= n; j++) dp[i][j] = 0;
    }
    int cur = 0;
    for (int i = 2; i <= n; i++) {
      int maxx = 0;
      for (int j = i - 1; j >= 1; j--) {
        if (tag[i] == tag[j]) continue;
        dp[cur][j] = max(dp[cur][j], dp[cur ^ 1][i] + abs(s[i] - s[j]));
        dp[cur][i] = max(dp[cur][i], dp[cur ^ 1][j] + abs(s[i] - s[j]));
      }
      cur ^= 1;
    }
    long long ans = dp[cur ^ 1][1];
    for (int i = 1; i <= n; i++) ans = max(ans, dp[cur ^ 1][i]);
    printf("%lld\n", ans);
  }
  return 0;
}
