/*
..................／＞　 フ.....................
        　　　　　| 　_　 _|
        　 　　　／`ミ _x 彡
        　　 　 /　　　 　 |
        　　　 /　 ヽ　　 ﾉ
        　／￣|　　 |　|　|
        　| (￣ヽ＿_ヽ_)_)
        　＼二つ
*/

#include<bits/stdc++.h>

#include<ext/pb_ds/tree_policy.hpp>

#define re return
#define fi first
#define se second
#define pb push_back
#define endl '\n'

#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define fr(i, a, b) for(int i = (a); i <= (b); i++)
#define rf(i, a, b) for(int i = (a); i >= (b); i--)
#define fe(x, y) for(auto& x : y)

using namespace std;
using namespace __gnu_pbds;

typedef double db;
typedef long double ld;
typedef bool bl;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template<typename T> void vout(T s){cout << s << endl;exit(0);}

void umin(ll& x, ll y){x = min(x, y);}
void umax(ll& x, ll y){x = max(x, y);}

mt19937_64 rnd(time(0));

int n, t[5005], s[5005];
ll p[5005], p2[5005], p3[5005];

void solve(){

    cin >> n;
    fr(i, 1, n)cin >> t[i];
    fr(i, 1, n)cin >> s[i];

    fr(i, 0, n)p[i] = p2[i] = 0;

    fr(i, 2, n){
        fr(j, 1, i - 1)if(t[i] != t[j]){
            umax(p[i], p[j] + abs(s[i] - s[j]));
        }
    }

    ll ans = 0;
    fr(i, 2, n){
        fr(j, 1, i - 2)if(t[i] != t[j])umax(p2[i], p2[j] + abs(s[i] - s[j]));
        umax(ans, p[i] + p2[i]);
    }

    cout << ans << endl;
}
int main(){
#ifdef LOCAL

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

#endif // LOCAL

    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    int q = 1;
    cin >> q;
    while(q--)solve();

return 0;}

