#include <bits/stdc++.h>
using namespace std;
long long datL[8192 * 2 - 1];
long long datR[8192 * 2 - 1];
int n;
void init(int n_) {
  n = 1;
  while (n < n_) n <<= 1;
  for (int i = 0; i < 2 * n - 1; i++) datL[i] = datR[i] = -1e18;
}
void update(int i, long long a, long long b) {
  i += n - 1;
  datL[i] = max(datL[i], a);
  datR[i] = max(datR[i], b);
  while (i > 0) {
    i = (i - 1) / 2;
    datL[i] = max(datL[2 * i + 1], datL[2 * i + 2]);
    datR[i] = max(datR[2 * i + 1], datR[2 * i + 2]);
  }
}
pair<long long, long long> query(int a, int b) {
  int L = (a < 0 ? 0 : a > n ? n : a) + n - 1;
  int R = (b < 0 ? 0 : b > n ? n : b) + n - 1;
  long long x = -1e18, y = -1e18;
  for (; L < R; L >>= 1, R >>= 1) {
    if (!(L & 1)) {
      x = max(x, datL[L]);
      y = max(y, datR[L]);
    }
    if (!(R & 1)) {
      --R;
      x = max(x, datL[R]);
      y = max(y, datR[R]);
    }
  }
  return make_pair(x, y);
}
int T, N;
int t[5000], s[5000];
long long dp[2][5000];
int main() {
  scanf("%d", &T);
  for (; T--;) {
    scanf("%d", &N);
    for (int i = 0; i < N; i++) scanf("%d", &t[i]), t[i]--;
    for (int i = 0; i < N; i++) scanf("%d", &s[i]);
    int now = 0;
    for (int i = 0; i < N; i++) dp[now][i] = -1e18;
    dp[now][0] = 0;
    for (int i = 1; i < N; i++) {
      int nxt = 1 - now;
      for (int j = 0; j < N; j++) dp[nxt][j] = dp[now][j];
      init(N);
      for (int j = i - 1; j >= 0; j--) {
        pair<long long, long long> p = query(0, t[j]);
        dp[nxt][j] = max(dp[nxt][j], max(p.first + s[j], p.second - s[j]));
        p = query(t[j] + 1, N);
        dp[nxt][j] = max(dp[nxt][j], max(p.first + s[j], p.second - s[j]));
        if (t[j] != t[i]) {
          long long X = dp[now][j] + abs(s[i] - s[j]);
          dp[nxt][i] = max(dp[nxt][i], X);
          update(t[i], X - s[i], X + s[i]);
        }
      }
      now = nxt;
    }
    long long ans = -1e18;
    for (int j = 0; j < N; j++) ans = max(ans, dp[now][j]);
    printf("%lld\n", ans);
  }
}
