#include <bits/stdc++.h>
using namespace std;
int N;
int tag[5003];
long long S[5003];
int minP[5003], maxP[5003];
int min2P[5003], max2P[5003];
long long dfs(int u, int fa) {
  if (fa > u) {
    for (int i = fa + 1; i <= N; ++i)
      if (tag[i] != tag[u]) return dfs(i, u) + abs(S[i] - S[u]);
    return 0;
  } else {
    int pos = -1;
    for (int i = fa - 1; i >= 1; --i)
      if (tag[i] != tag[u]) {
        if (pos == -1 || abs(S[i] - S[u]) > abs(S[pos] - S[u])) pos = i;
      }
    if (pos != -1) return dfs(pos, u) + abs(S[pos] - S[u]);
    for (int i = u + 1; i <= N; ++i)
      if (tag[i] != tag[u]) return dfs(i, u) + abs(S[i] - S[u]);
    return 0;
  }
  assert(1 == 0);
}
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("%lld", S + i);
    minP[1] = maxP[1] = 1;
    for (int i = 2; i <= N; ++i) {
      if (S[i] > S[maxP[i - 1]])
        maxP[i] = i;
      else
        maxP[i] = maxP[i - 1];
      if (S[i] < S[minP[i - 1]])
        minP[i] = i;
      else
        minP[i] = minP[i - 1];
    }
    for (int i = 1; i <= N; ++i) {
      min2P[i] = max2P[i] = -1;
      for (int j = 1; j < i; ++j) {
        if (tag[j] != tag[minP[i]]) {
          if (min2P[i] == -1 || S[j] < S[min2P[i]]) min2P[i] = j;
        }
        if (tag[j] != tag[maxP[i]]) {
          if (max2P[i] == -1 || S[j] > S[min2P[i]]) max2P[i] = j;
        }
      }
    }
    printf("%lld\n", dfs(1, 0));
  }
  return 0;
}
