#include <bits/stdc++.h>
using namespace std;
long long ara[5002][2], dp1[5002], dp2[5002], temp[5002];
int main() {
  long long t;
  long long i, j, k;
  cin >> t;
  while (t--) {
    long long n;
    cin >> n;
    for (i = 1; i <= n; i++) cin >> ara[i][0];
    for (i = 1; i <= n; i++) cin >> ara[i][1];
    long long res = 0;
    memset(dp1, 0, sizeof dp1);
    memset(dp2, 0, sizeof dp2);
    for (i = 2; i <= n; i++) {
      memset(temp, -1, sizeof temp);
      for (j = i - 1; j > 0; j--) {
        temp[j] = temp[j + 1];
        if (ara[j][0] != ara[i][0]) {
          temp[j] =
              max(temp[j], dp1[j] + (long long)abs(ara[j][1] - ara[i][1]));
        }
      }
      for (j = 1; j < i; j++) {
        if (ara[j][0] != ara[i][0] and temp[j + 1] != -1)
          dp2[j] = temp[j + 1] + (long long)abs(ara[j][1] - ara[i][1]);
        else
          dp2[j] = dp1[j];
        if (ara[j][0] != ara[i][0]) {
          dp2[i] = max(dp2[i], dp1[j] + (long long)abs(ara[j][1] - ara[i][1]));
        }
      }
      for (j = 1; j <= i; j++) {
        dp1[j] = dp2[j];
        res = max(res, dp2[j]);
      }
    }
    cout << res << "\n";
  }
}
