#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;
int dp[N];
int tag[N];
int s[N];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i <= n; ++i) cin >> tag[i];
    for (int i = 1; i <= n; ++i) cin >> s[i];
    for (int i = 1; i <= n; ++i)
      for (int j = i - 1; j >= 1; --j) {
        if (tag[i] == tag[j]) continue;
        int nowi = dp[i];
        int nowj = dp[j];
        dp[i] = max(dp[i], nowj + abs(s[i] - s[j]));
        dp[j] = max(dp[j], nowi + abs(s[i] - s[j]));
      }
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      ans = max(ans, dp[i]);
    }
    cout << ans << endl;
  }
  return 0;
}
