#include <bits/stdc++.h>
using namespace std;
void dbg_out() { cerr << endl; }
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) {
  cerr << ' ' << H;
  dbg_out(T...);
}
const int MXN = 1e5 + 5, INF = 1e9 + 5;
void solve() {
  int N;
  cin >> N;
  vector<int> tag(N);
  vector<int64_t> dp(N, 0), S(N, 0);
  for (auto &x : tag) cin >> x;
  for (auto &x : S) cin >> x;
  for (int i = 1; i < N; i++) {
    for (int j = i - 1; j >= 0; j--) {
      if (tag[i] == tag[j]) continue;
      int dp_i = dp[i];
      dp[i] = max(dp[i], dp[j] + abs(S[i] - S[j]));
      dp[j] = max(dp[j], dp_i + abs(S[i] - S[j]));
    }
  }
  cout << *max_element((dp).begin(), (dp).end()) << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int TC = 1;
  cin >> TC;
  while (TC--) solve();
}
