for i in range(int(input())):
    n = int(input())
    count = 0
    tagi = list(map(int, input().split()))
    si = list(map(int, input().split()))
    ci = [0] * n
    IQ = 0
    for j in range(n):
        ci[j] = 2 ** (j + 1)
    for k in range(1, n):
        if (tagi[k-1] != tagi[k]) and IQ < abs(ci[k - 1] - ci[k]):
            IQ = abs(ci[k - 1] - ci[k])
            count += abs(si[k - 1] - si[k])
            