import sys
range = xrange
input = raw_input

inp = [int(x) for x in sys.stdin.read().split()]; ii = 0
out = []

t = inp[ii]; ii += 1
for _ in range(t):
    n = inp[ii]; ii += 1
    tag = inp[ii: ii + n]; ii += n
    S = inp[ii: ii + n]; ii += n
    
    maxi = 0.0
    cur = [0.0] * n

    for j in reversed(range(n)):
        prev = cur
        cur = [0.0] * n
        DP1 = [0.0] * n
        
        ans2 = prev[j]
        
        for i in range(j + 1):
            ans = prev[i]
            ans2 = max(ans2, DP1[i - 1]) 
            #maxi = max(maxi, ans)
            if tag[i] != tag[j]:
                sdiff = abs(S[i] - S[j])
                DP1[i] = ans + sdiff
                cur[i] = max(ans, ans2 + sdiff) 
        maxi = max(maxi, ans2, max(prev))
    out.append(int(maxi))
sys.stdout.write('\n'.join(str(x) for x in out))
