#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
int getValid(vector<pair<int, int>> valid, int ban1, int ban2) {
  for (int i = 0; i < (valid.size()); ++i) {
    if (valid[i].first != ban1 && valid[i].first != ban2 &&
        valid[i].first != -1 && valid[i].first != INF) {
      return valid[i].second;
    }
  }
  return -1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  for (int tt = 0; tt < t; tt++) {
    int n;
    cin >> n;
    vector<int> tag(n);
    vector<long long> score(n);
    for (int i = 0; i < (n); ++i) {
      cin >> tag[i];
    }
    for (int i = 0; i < (n); ++i) {
      cin >> score[i];
    }
    vector<map<int, long long>> best(n, map<int, long long>());
    for (int i = 0; i < (n); ++i) {
      best[i][0] = 0;
    }
    long long veryBest = 0;
    vector<vector<pair<int, int>>> tagMin(
        n, vector<pair<int, int>>(3, pair<int, int>(-1, INF)));
    vector<vector<pair<int, int>>> tagMax(
        n, vector<pair<int, int>>(3, pair<int, int>(-1, -1)));
    for (int i = 0; i < (n); ++i) {
      if (i != 0) {
        tagMin[i] = tagMin[i - 1];
        tagMax[i] = tagMax[i - 1];
      }
      for (int k = 0; k < 3; k++) {
        if (tagMin[i][k].second > score[i]) {
          for (int l = 2; l > k; l--) {
            tagMin[i][l] = tagMin[i][l - 1];
          }
          tagMin[i][k] = pair<int, int>(tag[i], score[i]);
        }
        if (tag[i] == tagMin[i][k].first) {
          break;
        }
      }
      for (int k = 0; k < 3; k++) {
        if (tagMax[i][k].second < score[i]) {
          for (int l = 2; l > k; l--) {
            tagMax[i][l] = tagMax[i][l - 1];
          }
          tagMax[i][k] = pair<int, int>(tag[i], score[i]);
        }
        if (tag[i] == tagMax[i][k].first) {
          break;
        }
      }
      for (auto& prev : best[i]) {
        if (prev.first != 0) {
          for (int k = i + 1; k < (n); ++k) {
            long long minValid =
                getValid(tagMin[prev.first - 1], tag[i], tag[k]);
            if (minValid != -1) {
              best[k][0] =
                  max(best[k][0], prev.second + abs(minValid - score[i]) +
                                      abs(score[k] - minValid));
            }
            long long maxValid =
                getValid(tagMax[prev.first - 1], tag[i], tag[k]);
            if (maxValid != -1) {
              best[k][0] =
                  max(best[k][0], prev.second + abs(maxValid - score[i]) +
                                      abs(score[k] - maxValid));
            }
          }
          for (int j = 0; j < (prev.first); ++j) {
            if (tag[i] != tag[j]) {
              veryBest = max(veryBest, prev.second + abs(score[j] - score[i]));
            }
          }
        }
      }
      for (auto& p : best[i]) {
        best[i][0] = max(p.second, best[i][0]);
      }
      for (int k = i + 1; k < (n); ++k) {
        if (tag[i] != tag[k]) {
          best[k][i] = max(best[k][i], best[i][0] + abs(score[i] - score[k]));
          break;
        }
      }
    }
    for (auto& v : best) {
      for (auto& theScore : v) {
        veryBest = max(theScore.second, veryBest);
      }
    }
    cout << veryBest << "\n";
  }
  return 0;
}
