

def solve():


    n  = int(input())

    c = list(map(int,input().split()))


    s = list(map(int,input().split()))


    ans=0


    dp=[0]*(n)


    for i in range(n):

        for j in range(i-1,-1,-1):

            if c[i]!=c[j]:

                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]))

    return(max(dp))


for i in range(int(input())):

    print(solve())
