#include <bits/stdc++.h>
using namespace std;
int t;
int n;
int tag[5010];
int s[5010];
void Solve() {
  long long iq = 0;
  int begin = 1;
  int left = 0;
  int diff = -1;
  while (begin <= n && tag[begin] == tag[1]) begin++;
  if (begin > n) {
    printf("0\n");
    return;
  }
  for (int i = 1; i < begin; i++) {
    if (abs(s[i] - s[begin]) > diff) {
      diff = abs(s[i] - s[begin]);
      left = i;
    }
  }
  iq = diff;
  int right = begin + 1;
  left--;
  int cur;
  while (right <= n || left > 0) {
    while (left > 0 && tag[begin] == tag[left]) left--;
    while (right <= n && tag[right] == tag[begin]) right++;
    if (right > n && left <= 0) break;
    diff = -1;
    for (int i = left; i; i--) {
      if (abs(s[i] - s[begin]) > diff) {
        diff = abs(s[i] - s[begin]);
        cur = i;
      }
    }
    if (right <= n) {
      if (abs(s[right] - s[begin]) > diff) {
        diff = abs(s[right] - s[begin]);
        cur = right;
      }
    }
    iq += diff;
    if (cur < begin) {
      left = 0;
      right = begin + 1;
    } else {
      left = begin - 1;
      right = cur + 1;
    }
    begin = cur;
  }
  printf("%lld\n", iq);
}
int main() {
  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", s + i);
    }
    Solve();
  }
  return 0;
}
