#include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) { return (a > b) ? a : b; }
long long min(long long a, long long b) { return (a < b) ? a : b; }
long long min(long long a, long long b, long long c) {
  return min(a, min(b, c));
}
long long max(long long a, long long b, long long c) {
  return max(a, max(b, c));
}
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
int main(int argc, char **argv) {
  long long t;
  cin >> t;
  for (long long ii = 0; ii < t; ii++) {
    long long n;
    cin >> n;
    vector<long long> tag;
    vector<long long> s;
    long long temp;
    for (long long i = 0; i < n; i++) {
      cin >> temp;
      tag.push_back(temp);
    }
    for (long long i = 0; i < n; i++) {
      cin >> temp;
      s.push_back(temp);
    }
    long long points = 0;
    long long lastProblem = 0;
    long long lastTag = tag[0];
    long long lastS = s[0];
    long long bestScore = 0;
    long long lastJump = 0;
    bool noJump;
    for (long long i = 1; i < n; i++) {
      if (lastTag == tag[i]) continue;
      if (i == 1) {
        points += abs(s[i] - s[0]);
        lastProblem = i;
        lastTag = tag[i];
        lastS = s[i];
        bestScore = 0;
        noJump = true;
        for (long long j = min(lastJump - 1, i - 2); j >= 0; j--) {
          if (tag[i] != tag[j] && abs(s[i] - s[j]) > bestScore) {
            bestScore = abs(s[i] - s[j]);
            lastProblem = j;
            lastTag = tag[j];
            lastS = s[j];
            lastJump = j;
            noJump = false;
          }
        }
        if (noJump) {
          lastJump = 1000000000;
        }
        points += bestScore;
      } else {
        points += abs(s[i] - lastS);
        lastProblem = i;
        lastTag = tag[i];
        lastS = s[i];
        bestScore = 0;
        noJump = true;
        for (long long j = min(lastJump - 1, i - 2); j >= 0; j--) {
          if (tag[i] != tag[j] && abs(s[i] - s[j]) > bestScore) {
            bestScore = abs(s[i] - s[j]);
            lastProblem = j;
            lastTag = tag[j];
            lastS = s[j];
            lastJump = j;
            noJump = false;
          }
        }
        if (noJump) {
          lastJump = 1000000000;
        }
        points += bestScore;
      }
    }
    cout << points << endl;
  }
  return 0;
}
