#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, t[5050], s[5050], dp[5050], tp[5050];
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> t[i];
  for (int i = 1; i <= n; i++) cin >> s[i];
  for (int i = 1; i <= n; i++) dp[i] = tp[i] = 0;
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j < i; j++)
      if (t[i] != t[j]) {
        dp[i] = max(dp[i], dp[j] + abs(s[i] - s[j]));
        tp[j - 1] = max(tp[j - 1], dp[j] + abs(s[i] - s[j]));
      }
    int mx = 0;
    for (int j = i - 2; j >= 1; j--) {
      mx = max(mx, tp[j]), tp[j] = 0;
      if (t[i] != t[j]) dp[j] = max(dp[j], mx + abs(s[i] - s[j]));
    }
  }
  cout << (*max_element(dp + 1, dp + 1 + n));
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int tc = 1;
  cin >> tc;
  while (tc--) solve(), cout << "\n";
}
