#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 20;
const int mod = 1e9 + 7;
long long dp[maxn], tag[maxn], s[maxn];
int main(int argc, char const *argv[]) {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      dp[i] = 0;
      scanf("%I64d", &tag[i]);
    }
    for (int i = 1; i <= n; i++) {
      scanf("%I64d", &s[i]);
    }
    dp[1] = 0;
    for (int i = 2; i <= n; i++) {
      if (tag[i - 1] != tag[i]) dp[i] = dp[i - 1] + abs(s[i] - s[i - 1]);
      for (int j = i - 2; j >= 1; j--) {
        if (tag[j] != tag[i]) {
          long long tmp = dp[j];
          dp[j] = max(dp[j], dp[i] + abs(s[i] - s[j]));
          dp[i] = max(dp[i], tmp + abs(s[i] - s[j]));
        }
      }
      for (int j = 1; j <= n; j++) {
        printf("%I64d ", dp[j]);
      }
      printf("\n");
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
    printf("%I64d\n", ans);
  }
  return 0;
}
