#define LOCAL
#include <bits/stdc++.h>
#include <assert.h>

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define trav(a, x) for (auto& a: x)

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}

#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

void solve() {
	int n; cin >> n; vector<ll> tag(n), s(n), dp(n, 0);
	for (auto& v: tag) cin >> v;
	for (ll& v: s) cin >> v;
	for (int i = 0; i < n; i++){
		for (int j = i - 1; j >= 0; j--){
			if (tag[i] != tag[j]) {
				ll dpi = dp[i], dpj = dp[j];
				dp[i] = max(dp[i], dpj + abs(s[i] - s[j]));
				dp[j] = max(dp[j], dpi + abs(s[i] - s[j]));
			}
		}
	}
	cout << *max_element(all(dp)) << endl;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int nofcase;
	cin >> nofcase;
	for (int tt = 1; tt <= nofcase; tt++) {
		// cout << "Case #" << tt << ": ";
		solve();
	}
	return 0;
}