import sys
input = sys.stdin.readline

t=int(input())
for tests in range(t):
    n=int(input())
    Tag=list(map(int,input().split()))
    S=list(map(int,input().split()))

    DP=[0]*n
    
    ANS=0

    for i in range(n-1):
        NDP0=0
        MAX=DP[i]

        for j in range(i-1,-1,-1):
            temp=DP[j]
            if Tag[j]!=Tag[i] and Tag[j]!=Tag[i+1]:
                DP[j]=MAX+abs(S[j]-S[i])+abs(S[j]-S[i+1])

            if Tag[j]!=Tag[i+1]:
                DP[j]=max(DP[j],abs(S[i+1]-S[j]))
                
            MAX=max(MAX,temp)

        if Tag[i]!=Tag[i+1]:
            DP[i]=max(DP[i],MAX+abs(S[i+1]-S[i]))

        DP0=NDP0

    ANS=max(ANS,max(DP))

    MAX=DP[n-1]
    for j in range(n-2,-1,-1):
        if Tag[j]!=Tag[n-1]:
            ANS=max(ANS,MAX+abs(S[j]-S[n-1]))
        MAX=max(MAX,DP[j])

    print(ANS)
                            
                
            
            
            
            
            
            
            

    
    

