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