#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<int> tag(n);
  for (int i = 0; i < n; i++) cin >> tag[i];
  vector<int> s(n);
  for (int i = 0; i < n; i++) cin >> s[i];
  vector<int> dp(n);
  vector<int> ndp(n);
  for (int i = 0; i < n; i++) {
    int ms = 0;
    for (int j = i - 1; j >= 0; j--) {
      if (tag[i] == tag[j]) continue;
      ms = max(ms, dp[j] + abs(s[i] - s[j]));
      if (j != 0) ndp[j - 1] = max(dp[j - 1], ms + abs(s[i] - s[j - 1]));
    }
    for (int j = 0; j < i - 1; j++) dp[j] = ndp[j];
    dp[i] = ms;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) ans = max(ans, dp[i]);
  cout << ans << endl;
}
signed main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) solve();
}
