#include <bits/stdc++.h>
using namespace std;
const long long N = 5005;
long long T, n;
long long dp[N], mx[2][N];
long long tag[N], s[N];
signed main() {
  cin >> T;
  while (T--) {
    cin >> n;
    for (long long i = 1; i <= n; i++) cin >> tag[i];
    for (long long i = 1; i <= n; i++) cin >> s[i];
    for (long long i = 1; i <= n + 1; i++)
      dp[i] = 0, mx[0][i] = mx[1][i] = -1e18;
    dp[1] = 0;
    mx[0][1] = -s[1];
    mx[1][1] = s[1];
    long long ans = 0;
    for (long long i = 2; i <= n; i++) {
      long long mx0 = -1e18, mx1 = -1e18;
      for (long long j = i - 1; j >= 1; j--) {
        if (i == j) {
        } else {
          if (tag[i] == tag[j])
            continue;
          else {
            dp[j] = max(dp[j], mx0 + s[i] + abs(s[i] - s[j]));
            dp[j] = max(dp[j], mx1 - s[i] + abs(s[i] - s[j]));
            mx0 = max(mx0, mx[0][j]);
            mx1 = max(mx1, mx[1][j]);
          }
        }
        ans = max(ans, dp[j]);
      }
      dp[i] = max(dp[i], mx0 + s[i]);
      dp[i] = max(dp[i], mx1 - s[i]);
      ans = max(ans, dp[i]);
      for (long long j = i; j >= 1; j--)
        mx[0][j] = max(mx[0][j], dp[j] - s[j]),
        mx[1][j] = max(mx[1][j], dp[j] + s[j]), ans = max(ans, dp[j]);
    }
    cout << ans << endl;
  }
  return 0;
}
