#include <bits/stdc++.h>
using namespace std;
const long long N = 5006, INF = 1e18, MOD = 1e9 + 7;
int n, k, t, a[N], s[N], ans[N], mx[2][2], q = 1, e, x[2], y[2], Ans;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  cin >> q;
  while (q--) {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> s[i];
    y[0] = y[1] = a[1];
    ans[0] = -INF;
    mx[0][1] = mx[0][0] = 1;
    mx[1][0] = mx[1][1] = 0;
    for (int i = 2; i <= n; i++) {
      x[0] = (y[0] == a[i]);
      x[1] = (y[1] == a[i]);
      ans[i] = max(ans[mx[x[0]][0]] + abs(s[mx[x[0]][0]] - s[i]),
                   ans[mx[x[1]][1]] + abs(s[mx[x[1]][1]] - s[i]));
      if (ans[i] - s[i] > ans[mx[1][0]] - s[mx[1][0]]) mx[1][0] = i;
      if (ans[mx[1][0]] - s[mx[1][0]] > ans[mx[0][0]] - s[mx[0][0]]) {
        y[0] = a[mx[1][0]];
        swap(mx[1][0], mx[0][0]);
      }
      if (ans[i] + s[i] > ans[mx[1][1]] + s[mx[1][1]]) mx[1][1] = i;
      if (ans[mx[1][1]] + s[mx[1][1]] > ans[mx[0][1]] + s[mx[0][1]]) {
        y[1] = a[mx[1][1]];
        swap(mx[1][1], mx[0][1]);
      }
    }
    cout << ans[n] << endl;
  }
}
