#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
int n;
int tag[N];
int score[N];
int dp[N], tmp[N], his[N];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &tag[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &score[i]);
    int ans = 0;
    memset(dp, 0, sizeof(dp));
    for (int i = 1; i < n; i++) {
      memset(tmp, 0, sizeof(tmp));
      int mx = 0;
      for (int j = i - 2; j >= 1; j--) {
        mx = max(mx, dp[j + 1]);
        if (tag[j] != tag[i] && tag[j] != tag[i + 1])
          tmp[j] = max(tmp[j], mx + abs(score[i] - score[j]) +
                                   abs(score[j] - score[i + 1]));
      }
      for (int j = 1; j <= i; j++)
        if (tag[j] != tag[i + 1])
          tmp[j] = max(tmp[j], his[j] + abs(score[j] - score[i + 1]));
      mx = 0;
      for (int j = 1; j <= n; j++) dp[j] = tmp[j], mx = max(mx, tmp[j]);
      his[i + 1] = mx;
      mx = 0;
      for (int j = i - 1; j >= 1; j--) {
        mx = max(mx, dp[j + 1]);
        if (tag[j] != tag[i + 1])
          ans = max(ans, mx + abs(score[i + 1] - score[j]));
      }
      ans = max(ans, his[i + 1]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
