#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 5e3 + 1;
long long dp[maxn], k[maxn];
long long tag[maxn], s[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T = 1;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      cin >> tag[i];
    }
    for (int i = 1; i <= n; ++i) {
      cin >> s[i];
      dp[i] = 0;
    }
    for (int i = 2; i <= n; ++i) {
      for (int j = 1; j <= i; ++j) k[j] = 0;
      for (int j = i - 1; j >= 1; --j) {
        k[j] = max(k[j], dp[i] + abs(s[i] - s[j]));
        if (tag[i] != tag[j]) dp[i] = max(dp[j] + abs(s[i] - s[j]), dp[i]);
      }
      for (int j = i - 2; j >= 1; --j) {
        if (tag[i] == tag[j]) continue;
        dp[j] = max(dp[j], k[j]);
      }
    }
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
      ans = max(ans, dp[i]);
    }
    cout << ans << "\n";
  }
  return 0;
}
