#include <bits/stdc++.h>
using namespace std;
#define gok ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
//# define int long long
#define pb push_back
const int N = 3005;

signed main()
{
    gok
    int t;
    cin>>t;
  while(t--)
  {
      int n;
      cin>>n;
      int tg[n],s[n];
      for(int i=0;i<n;i++)
            cin>>tg[i];

      for(int i=0;i<n;i++)
        cin>>s[i];

      int dp[n+1] = {0},an=0;

      for(int i=0;i<n;i++)
      {
          for(int j=i-1;j>=0;j--)
          {
              if(tg[i]==tg[j])
                continue;
              int prv = dp[j];
              dp[j] = max(dp[j],dp[i]+(abs(s[i]-s[j])));
              dp[i]  =max(dp[i],prv+abs(s[i]-s[j]));
              an = max(an,dp[j]);
          }
          an = max(an,dp[i]);
      }
    cout<<an<<endl;

  }
}

