#include <bits/stdc++.h>
using namespace std;
long long dp[5005], arr[5005], tag[5005], N;
void solve() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> tag[i];
    dp[i] = 0;
  }
  for (int i = 0; i < N; ++i) {
    cin >> arr[i];
  }
  for (int i = 0; i < N; ++i) {
    for (int j = i - 1; j >= 0; --j) {
      if (tag[i] == tag[j]) continue;
      int tmp = dp[i];
      dp[i] = max(dp[i], dp[j] + abs(arr[i] - arr[j]));
      dp[j] = max(dp[j], tmp + abs(arr[i] - arr[j]));
    }
  }
  long long ans = 0;
  for (int i = 0; i < N; ++i) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return;
}
int main() {
  int T;
  cin >> T;
  while (T--) solve();
}
