// Parasparopagraho Jīvānām
 
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
 
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
 
using namespace  __gnu_pbds ;
using namespace __gnu_cxx ;
using namespace std ; 
 
typedef long long ll ;
typedef long double ldb ;
 
typedef pair<int, int> pii ;
typedef pair<int, ll> pil ;
typedef pair<ll,ll> pll ;
 
typedef vector<int> vi ;
typedef vector<ll> vll ;
typedef vector<pii> vpi ;
typedef vector<pll> vpl ;
typedef vector<pair<ll,int> > vpli ; 
typedef vector<int,pil> edges ;
typedef vector<vi> matrix ; 
typedef priority_queue<int> pqu ; 
 
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ;
 
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define per(i,b,a) for (int i = (b); i >= (a); i--)
 
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pob pop_back 
#define fi first
#define se second
#define ins insert 
#define bk back
#define con continue 
#define lbd lower_bound 
#define ubd upper_bound 
 
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
 
#define pr cout << // I don't like this but my laptop's 'O' key is not functioning well
 
/* Careful when using long long
__builtin_clz(int x) {} //: the number of zeros at the beginning of the number
__builtin_ctz(int x) {} //: the number of zeros at the end of the number
__builtin_popcount(int x) {} //: the number of ones in the number
__builtin_parity(int x) {} //: the parity (even or odd) of the number of ones */
 
const int mod1 = 1000000007 ;
const int mod2 = 998244353 ;
const ll  infl = 4e18 ;
const int infi = 2e9 ;
const int maxn = 5e5 + 5 ;
const int block = 500 ;
const int logn = 60 ;
const int alpha = 27 ;
const ldb pi = acos(-1) ;
 
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) ;
int rng_lr(int a, int b)
{
    int ret ;
    ret = uniform_int_distribution<int>(a, b)(rng) ;
    return ret ;
    // For shuffling use shuffle(all, rng) ; 
}
 
ll modpow(ll a, ll b, int MOD) { ll res = 1 ; while(b) { if(b&1) { res = res*a ; res %= MOD ; } a *= a ; a %= MOD ; b >>= 1 ; } return res%MOD ; }
void upmin(int &a, int b) { if(a < b) { a = b ; } }
void relax(int &a, int b) { if(a > b) { a = b ; } }
ll add(ll a, ll b, int MOD) { a += b ; if(a >= MOD) { a -= MOD ; } return  a ; }
ll sub(ll a, ll b, int MOD) { a -= b ; if(a < 0) { a += MOD ; } return a ; }
ll mul(ll a, ll b, int MOD) { b %= MOD ; a *= b ; a %= MOD ; return a ; }
ll inverse(ll a, ll MOD) { a = modpow(a, MOD - 2, MOD) ; return a ; }
ll lcm(ll a, ll b) { ll ret ; ll g = __gcd(a, b) ; ret = a/g ; ret = ret*b ; return ret ; }
int icast(char ch) { return int(ch-'a') ; }
void hyn(int a, int b) { if(a == b) cout << "YES\n" ; else cout << "NO\n" ; } 
void pyd(int a, int b) { if(a == b) cout << "First\n" ; else cout << "Second\n" ; } 

// Check constraints + overflows + types in typedef 
int main() 
{
    ios::sync_with_stdio(false) ;
    cin.tie(NULL) ;
    int tc ; 
    cin >> tc ; 
    while(tc--) {
    	int n ; 
    	cin >> n ; 
    	vi tag(n+1) ; 
    	vi s(n+1) ; 
    	rep(i,0,n-1) {
    		cin >> tag[i+1] ; 
    	}
        tag[0] = -1 ; 
    	rep(i,0,n-1) {
    		cin >> s[i+1] ;
    	}
    	s[0] = 0 ; 
    	ll dp[2][n+1] ; 
    	rep(i,0,1) {
    		rep(j,0,n) {
    			if(j == 0) {
    				dp[i][j] = 0 ; 
    				con ; 
    			}
    			dp[i][j] = -1 ; 
    		}
    	}
    	ll pd[n] ; 
    	per(i,n-1,0) {
    		pd[i] = -1 ; 
    	}
    	per(i,n-1,0) {
    		pd[i] = dp[1][i] ; 
    		if(i+1 < n) {
    			pd[i] = max(pd[i], pd[i+1]) ; 
    		}
    	}
    	rep(i,2,n) {
    		int ni = (i&1) ; 
            rep(j,0,n-1) { 
                dp[ni][j] = -1 ; 
            }
            dp[ni][0] = 0 ; 
    		per(j,i-2,1) {
    			if(tag[i] == tag[j]) con ; 
    			dp[ni][j] = 1LL*abs(s[i] - s[j]) ;
    		    if(tag[j] == tag[i-1]) con ;  
    		    ll nbest = -1 ; 
    		    if(pd[j+1] != -1) {
    		    	nbest = pd[j+1] ; 
    		    	nbest += abs(s[i] - s[j]) ; 
    		    	nbest += abs(s[j] - s[i-1]) ; 
    		    }
   
    		    dp[ni][j] = max(nbest, dp[ni][j]) ; 
    	    }
    		ll best = -1 ; 
    		rep(nj,0,i-2) {
    			best = max(best, dp[(ni^1)][nj]) ; 
    		}
    		if(tag[i-1] != tag[i] && best != -1) {
    			dp[ni][i-1] = best + abs(s[i] - s[i-1]) ; 
    		}
    		pd[n-1] = dp[ni][n-1] ; 
    		per(j,n-2,0) {
    			pd[j] = max(dp[ni][j], pd[j+1]) ; 
    		}
    	}
    	ll res = 0 ;
    	int m = ((n)&1) ;  
    	rep(i,1,n-1) {
    		ll w = 0 ; 
    		rep(j,1,i-1) {
    			if(tag[j] != tag[n]) {
    				w = max(w, 1LL*abs(s[n] - s[j])) ; 
    			}
    		}
    		res = max(res, dp[m][i] + w) ; 
    	}
    	pr res << "\n" ; 
    }

    return 0 ; 
}