#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 5;
int T, n, tag[N], s[N], dp[N];
void solve() {
  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(dp));
  for (int j = 1; j <= n; j++) {
    for (int i = j - 1; i >= 1; i--) {
      if (tag[i] == tag[j]) continue;
      int dpi = dp[i], dpj = dp[j], p = abs(s[i] - s[j]);
      ;
      dp[i] = max(dp[i], dpj + p);
      dp[j] = max(dp[j], dpi + p);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
  cout << ans << endl;
}
int main() {
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
