#include<bits/stdc++.h>
//#define int long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define ld long double
#define F first
#define S second
#define all(i) i.begin(),i.end()
using namespace std;

//Constants and Variables
const int maxn=2e5+7;
const int mod=1e9+7;
const long long INF=1e9;
int a[maxn],b[maxn],mn[maxn][21],mx[maxn][21],n;

void sp(int o,int L,int R)
{
  for(int i=1;i<=n;i++)
    mx[i][0]=a[i],mn[i][0]=b[i];
  for(int j=1;(1<<j)<=n;j++)
    {
      for(int i=1;i+(1<<j)-1<=n;i++)
        {
	  mx[i][j]=max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
	  mn[i][j]=min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
        }
    }
}

int query(int l,int r,int flag)
{
  if(flag)
    {
      int k = 0;
      while( (1<<(k+1)) <= r-l+1)
	k++ ;
      return max(mx[l][k],mx[r-(1<<k)+1][k]);
    }
  else
    {
      int k = 0;
      while( (1<<(k+1)) <= r-l+1)
	k++ ;
      return min(mn[l][k],mn[r-(1<<k)+1][k]);
    }
}

int check(int x,int y,int flag)
{
  int mx=query(x,y,1),mn=query(x,y,0);
  if(flag)
    {
      if(mx==mn)
	return 1;
    else if(mx>mn)
      return 2;
    return 0;
    }
  else
    {
      if(mx==mn)
	return 1;
      return 0;
    }
 }

//Siyahish Amighe!!
int32_t main()
{
  ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  cin>>n;
  for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
  for(int i=1;i<=n;i++)
    scanf("%d",&b[i]);

  sp(1,1,n);

  long long ans=0;
  int L,R;
  for(int i=1;i<=n;i++)
    {
      int l=i,r=n;
      while(l<=r)
	{
	  int mid=(l+r)>>1;
	  if(!check(i,mid,1))
	    l=mid+1;
	  else r=mid-1;
	}
      L=l;
      if(check(i,L,1)==2)
	continue;
      l=L,r=n;
      while(l<=r)
	{
	  int mid=(l+r)>>1;
	  if(check(i,mid,0))
	    l=mid+1;
	  else r=mid-1;
	}
      R=l-1;
      if(R>=L)
	ans=ans+(R-L+1);
    }
  cout<<ans<<endl;
  return 0;
}


#include<bits/stdc++.h>
//#define int long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define ld long double
#define F first
#define S second
#define all(i) i.begin(),i.end()
using namespace std;

//Constants and Variables
const int maxn=2e5+7;
const int mod=1e9+7;
const long long INF=1e9;
int n,a[maxn],b[maxn];

//Siyahish Amighe!!
int32_t main()
{
  ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  cin>>n;
  for(int i=0;i<n;i++)
    scanf("%d",&a[i]);
  for(int i=0;i<n;i++)
    scanf("%d",&b[i]);
  long long cnt=0;
  for(int l=0;l<n;l++)
    {
      int mx=-INF;
      int mn=+INF;
      for(int r=l+1;r<=n;r++)
	{
	  mx=max(mx,a[r-1]);
	  mn=min(mn,b[r-1]);
	  if (mx==mn)
	    cnt++;
	  if (mx>mn)
	    break;
	}

    }
  printf("%lld\n",cnt);
  return 0;
}


#include<bits/stdc++.h>
//#define int long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define ld long double
#define F first
#define S second
#define all(i) i.begin(),i.end()
using namespace std;

//Constants and Variables
const int maxn=2e5+7;
const int mod=1e9+7;
const long long INF=1e9;
int a[maxn],b[maxn],mn[maxn][21],mx[maxn][21],n;

void sp(int o,int L,int R)
{
  for(int i=1;i<=n;i++)
    mx[i][0]=a[i],mn[i][0]=b[i];
  for(int j=1;(1<<j)<=n;j++)
    {
      for(int i=1;i+(1<<j)-1<=n;i++)
        {
	  mx[i][j]=max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
	  mn[i][j]=min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
        }
    }
}

int query(int l,int r,int flag)
{
  if(flag)
    {
      int k = 0;
      while( (1<<(k+1)) <= r-l+1)
	k++ ;
      return max(mx[l][k],mx[r-(1<<k)+1][k]);
    }
  else
    {
      int k = 0;
      while( (1<<(k+1)) <= r-l+1)
	k++ ;
      return min(mn[l][k],mn[r-(1<<k)+1][k]);
    }
}

int check(int x,int y,int flag)
{
  int mx=query(x,y,1),mn=query(x,y,0);
  if(flag)
    {
      if(mx==mn)
	return 1;
    else if(mx>mn)
      return 2;
    return 0;
    }
  else
    {
      if(mx==mn)
	return 1;
      return 0;
    }
 }

//Siyahish Amighe!!
int32_t main()
{
  ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
  cin>>n;
  for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
  for(int i=1;i<=n;i++)
    scanf("%d",&b[i]);

  sp(1,1,n);

  long long ans=0;
  int L,R;
  for(int i=1;i<=n;i++)
    {
      int l=i,r=n;
      while(l<=r)
	{
	  int mid=(l+r)>>1;
	  if(!check(i,mid,1))
	    l=mid+1;
	  else r=mid-1;
	}
      L=l;
      if(check(i,L,1)==2)
	continue;
      l=L,r=n;
      while(l<=r)
	{
	  int mid=(l+r)>>1;
	  if(check(i,mid,0))
	    l=mid+1;
	  else r=mid-1;
	}
      R=l-1;
      if(R>=L)
	ans=ans+(R-L+1);
    }
  cout<<ans<<"\n";
  return 0;
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
int main()
{
    int n,cnt=0,mx=INT_MIN,mn=INT_MAX; scanf("%d",&n);
    LL ans=0;
    int a[n],b[n];
    for(int i=0;i<n;i++)scanf("%d",&a[i]);
    for(int i=0;i<n;i++)scanf("%d",&b[i]);
    for(int i=0;i<n;i++)
    {
        mx=max(mx,a[i]);
        mn=min(mn,b[i]);
        if(mx==mn)ans+=cnt+++1;
        else cnt=0,mx=INT_MIN,mn=INT_MAX;
    }
    cout<<ans;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=200050;
int n,aa[N],bb[N],mx[4*N],mn[4*N];
long long ans;
void build(int i,int l,int r)
{
    if(l==r)
    {
        mx[i]=aa[l];
        mn[i]=bb[l];
        return;
    }
    int mid=(l+r)/2;
    build(i*2,l,mid);
    build(i*2+1,mid+1,r);
    mx[i]=max(mx[i*2],mx[i*2+1]);
    mn[i]=min(mn[i*2],mn[i*2+1]);
}
int qmx(int i,int l,int r,int a,int b)
{
    if(r<a||l>b)return -2e9;
    if(l>=a&&r<=b)return mx[i];
    int mid=(l+r)/2;
    return max(qmx(i*2,l,mid,a,b),qmx(i*2+1,mid+1,r,a,b));
}
int qmn(int i,int l,int r,int a,int b)
{
    if(r<a||l>b)return 2e9;
    if(l>=a&&r<=b)return mn[i];
    int mid=(l+r)/2;
    return min(qmn(i*2,l,mid,a,b),qmn(i*2+1,mid+1,r,a,b));
}
void BS(int i)
{
    int l=i,r=n-1,r1=-1,r2=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        int mxx=qmx(1,0,n-1,i,mid),mnn=qmn(1,0,n-1,i,mid);
        if(mxx==mnn)r=mid-1,r1=mid;
        else if(mxx>mnn)r=mid-1;
        else l=mid+1;
    }
    l=i,r=n-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        int mxx=qmx(1,0,n-1,i,mid),mnn=qmn(1,0,n-1,i,mid);
        if(mxx==mnn)l=mid+1,r2=mid;
        else if(mxx>mnn)l=mid+1;
        else r=mid-1;
    }
    if(r2+1&&r1+1)ans+=r2-r1+1;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)scanf("%d",&aa[i]);
    for(int i=0;i<n;i++)scanf("%d",&bb[i]);
    build(1,0,n-1);
    for(int i=0;i<n;i++)BS(i);
    printf("%lld",ans);
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define f first
#define s second
using namespace std;
const int N=200050;
int n,aa[N],bb[N],mx[4*N],mn[4*N];
long long ans;
void build(int i,int l,int r)
{
    if(l==r)
    {
        mx[i]=aa[l];
        mn[i]=bb[l];
        return;
    }
    int mid=(l+r)/2;
    build(i*2,l,mid);
    build(i*2+1,mid+1,r);
    mx[i]=max(mx[i*2],mx[i*2+1]);
    mn[i]=min(mn[i*2],mn[i*2+1]);
}
pii q(int i,int l,int r,int a,int b)
{
    if(r<a||l>b)return {-2e9,2e9};
    if(l>=a&&r<=b)return {mx[i],mn[i]};
    int mid=(l+r)/2;
    pii s1=q(i*2,l,mid,a,b),s2=q(i*2+1,mid+1,r,a,b);
    return {max(s1.f,s2.f),min(s1.s,s2.s)};
}
void BS(int i)
{
    int l=i,r=n-1,r1=-1,r2=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        pii o=q(1,0,n-1,i,mid);
        int mxx=o.f,mnn=o.s;
        if(mxx==mnn)r=mid-1,r1=mid;
        else if(mxx>mnn)r=mid-1;
        else l=mid+1;
    }
    l=i,r=n-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        pii o=q(1,0,n-1,i,mid);
        int mxx=o.f,mnn=o.s;
        if(mxx==mnn)l=mid+1,r2=mid;
        else if(mxx>mnn)r=mid-1;
        else l=mid+1;
    }
    if(r2+1&&r1+1)ans+=r2-r1+1;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)scanf("%d",&aa[i]);
    for(int i=0;i<n;i++)scanf("%d",&bb[i]);
    build(1,0,n-1);
    for(int i=0;i<n;i++)BS(i);
    printf("%lld",ans);
}


#include <bits/stdc++.h>
#define LL long long
using namespace std;
int main()
{
    int n,cnt=0,mx=INT_MIN,mn=INT_MAX; scanf("%d",&n);
    LL ans=0;
    int a[n],b[n];
    for(int i=0;i<n;i++)scanf("%d",&a[i]);
    for(int i=0;i<n;i++)scanf("%d",&b[i]);
    for(int i=0;i<n;i++)
    {
        mx=max(mx,a[i]);
        mn=min(mn,b[i]);
        //cout<<mn<<" "<<mx<<" "<<cnt<<endl;
        if(a[i]==b[i])cnt++;
        if(mx==mn)ans+=cnt;
        else cnt=0,mx=INT_MIN,mn=INT_MAX;
    }
    cout<<ans+cnt;
return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N=200050;
int n,aa[N],bb[N],mx[4*N],mn[4*N];
long long ans;
void build(int i,int l,int r)
{
    if(l==r)
    {
        mx[i]=aa[l];
        mn[i]=bb[l];
        return;
    }
    int mid=(l+r)/2;
    build(i*2,l,mid);
    build(i*2+1,mid+1,r);
    mx[i]=max(mx[i*2],mx[i*2+1]);
    mn[i]=min(mn[i*2],mn[i*2+1]);
}
int qmx(int i,int l,int r,int a,int b)
{
    if(r<a||l>b)return -2e9;
    if(l>=a&&r<=b)return mx[i];
    int mid=(l+r)/2;
    return max(qmx(i*2,l,mid,a,b),qmx(i*2+1,mid+1,r,a,b));
}
int qmn(int i,int l,int r,int a,int b)
{
    if(r<a||l>b)return 2e9;
    if(l>=a&&r<=b)return mn[i];
    int mid=(l+r)/2;
    return min(qmn(i*2,l,mid,a,b),qmn(i*2+1,mid+1,r,a,b));
}
void BS(int i)
{
    int l=i,r=n-1,r1=-1,r2=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        int mxx=qmx(1,0,n-1,i,mid),mnn=qmn(1,0,n-1,i,mid);
        if(mxx==mnn)r=mid-1,r1=mid;
        else if(mxx>mnn)r=mid-1;
        else l=mid+1;
    }
    l=i,r=n-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        int mxx=qmx(1,0,n-1,i,mid),mnn=qmn(1,0,n-1,i,mid);
        if(mxx==mnn)l=mid+1,r2=mid;
        else if(mxx>mnn)r=mid-1;
        else l=mid+1;
    }
    if(r2+1&&r1+1)ans+=r2-r1+1;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)scanf("%d",&aa[i]);
    for(int i=0;i<n;i++)scanf("%d",&bb[i]);
    build(1,0,n-1);
    for(int i=0;i<n;i++)BS(i);
    printf("%lld",ans);
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10, lg = 30;
int a[maxn], b[maxn];

struct sparse {
    int arr[2][lg][maxn];
    void init(int n) {
        for (int i = 0; i < n; ++i) {
            arr[0][0][i] = a[i];
            arr[1][0][i] = b[i];
        }
        for (int i = 1; (1 << i) <= n; ++i) {
            for (int j = 0; j + (1 << i) <= n; ++j) {
                arr[0][i][j] = max(arr[0][i - 1][j], arr[0][i - 1][j + (1 << (i - 1))]);
                arr[1][i][j] = min(arr[1][i - 1][j], arr[1][i - 1][j + (1 << (i - 1))]);
            }
        }
    }
    pair<int, int> query(int l, int r) {
        int p = 31 - __builtin_clz(r - l + 1);
        return make_pair(max(arr[0][p][l], arr[0][p][r - (1 << p) + 1]), min(arr[1][p][l], arr[1][p][r - (1 << p) + 1]));
    }
} sp;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < n; ++i) cin >> b[i];
    sp.init(n);
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
        int rr = i, rl = i, trr = i, trl = i;
        int d = 262144; while (d >>= 1) {
            if (rr + d < n) {
                auto p = sp.query(i, rr + d);
                if (p.first == p.second) trr = rr + d;
                if (p.first <= p.second) rr += d;
            }
            if (rl + d < n) {
                auto p = sp.query(i, rl + d);
                if (p.first == p.second) trl = rl + d;
                if (p.first < p.second) rl += d;
            }
        }
        if (sp.query(i, i).first == sp.query(i, i).second) trl = i;
        // cout << "i = " << i << endl;
        // cout << "rl = " << trl << " rr = " << trr << endl;
        if (sp.query(i, trl).first != sp.query(i, trl).second || sp.query(i, trr).first != sp.query(i, trr).second) continue;
        // cout << "rl = " << rl << " rr = " << rr << endl;
        ans += max(0, trr - trl + 1);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10, lg = 30;
int a[maxn], b[maxn];

struct sparse {
    int arr[2][lg][maxn];
    void init(int n) {
        for (int i = 0; i < n; ++i) {
            arr[0][0][i] = a[i];
            arr[1][0][i] = b[i];
        }
        for (int i = 1; (1 << i) <= n; ++i) {
            for (int j = 0; j + (1 << i) <= n; ++j) {
                arr[0][i][j] = max(arr[0][i - 1][j], arr[0][i - 1][j + (1 << (i - 1))]);
                arr[1][i][j] = min(arr[1][i - 1][j], arr[1][i - 1][j + (1 << (i - 1))]);
            }
        }
    }
    pair<int, int> query(int l, int r) {
        int p = 31 - __builtin_clz(r - l + 1);
        return make_pair(max(arr[0][p][l], arr[0][p][r - (1 << p) + 1]), min(arr[1][p][l], arr[1][p][r - (1 << p) + 1]));
    }
} sp;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < n; ++i) cin >> b[i];
    sp.init(n);
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
        int rl = i, rr = i;
        int d = 262144; while (d >>= 1) {
            if (rl + d >= i && rl + d < n) {
                auto p = sp.query(i, rl + d);
                if (p.first < p.second) rl += d;
            }
            if (rr + d >= i && rr + d < n) {
                auto p = sp.query(i, rr + d);
                if (p.first <= p.second) rr += d;
            }
        }
        // cout << "i = " << i << endl;
        // cout << "rl = " << rl << " rr = " << rr << endl;
        if (sp.query(i, rl).first != sp.query(i, rl).second || sp.query(i, rr).first != sp.query(i, rr).second) continue;
        // cout << "rl = " << rl << " rr = " << rr << endl;
        ans += max(0, rr - rl + 1);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10, lg = 30;
int a[maxn], b[maxn];

struct sparse {
    int arr[2][lg][maxn];
    void init(int n) {
        for (int i = 0; i < n; ++i) {
            arr[0][0][i] = a[i];
            arr[1][0][i] = b[i];
        }
        for (int i = 1; (1 << i) <= n; ++i) {
            for (int j = 0; j + (1 << i) <= n; ++j) {
                arr[0][i][j] = max(arr[0][i - 1][j], arr[0][i - 1][j + (1 << (i - 1))]);
                arr[1][i][j] = min(arr[1][i - 1][j], arr[1][i - 1][j + (1 << (i - 1))]);
            }
        }
    }
    pair<int, int> query(int l, int r) {
        int p = 31 - __builtin_clz(r - l + 1);
        return make_pair(max(arr[0][p][l], arr[0][p][r - (1 << p) + 1]), min(arr[1][p][l], arr[1][p][r - (1 << p) + 1]));
    }
} sp;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < n; ++i) cin >> b[i];
    sp.init(n);
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
        int rr = i, trr = i;
        int d = 262144; while (d >>= 1) {
            if (rr + d < n) {
                auto p = sp.query(i, rr + d);
                if (p.first == p.second) trr = rr + d;
                if (p.first <= p.second) rr += d;
            }
        }
        int rl = rr, trl = rr;
        d = 262144; while (d >>= 1) {
            if (rl - d >= i) {
                auto p = sp.query(i, rl - d);
                if (p.first == p.second) trl = rl - d;
                if (p.first <= p.second) rl -= d;
            }
        }
        // cout << "i = " << i << endl;
        // cout << "rl = " << rl << " rr = " << rr << endl;
        if (sp.query(i, trl).first != sp.query(i, trl).second || sp.query(i, trr).first != sp.query(i, trr).second) continue;
        // cout << "rl = " << rl << " rr = " << rr << endl;
        ans += max(0, trr - trl + 1);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

const int maxn = 2e5 + 10;
int a[maxn], b[maxn], sta[20][maxn], stb[20][maxn];

void init(int n) {
    for (int i = 0; i < n; ++i) sta[0][i] = a[i], stb[0][i] = b[i];
    for (int i = 1; (1 << i) <= n; ++i) {
        for (int j = 0; j + (1 << i) <= n; ++j) {
            sta[i][j] = max(sta[i - 1][j], sta[i - 1][j + (1 << (i - 1))]);
            stb[i][j] = min(stb[i - 1][j], stb[i - 1][j + (1 << (i - 1))]);
        }
    }
}

pair<int, int> query(int l, int r) {
    int p = 31 - __builtin_clz(r - l + 1);
    return make_pair(max(sta[p][l], sta[p][r - (1 << p) + 1]), min(stb[p][l], stb[p][r- (1 << p) + 1]));
}

bool check(int l, int r) {
    pair<int, int> q = query(l, r);
    return q.first == q.second;
}

int32_t main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < n; ++i) cin >> b[i];
    init(n);
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
        int d = 1; while (d <= n) d <<= 1;
        int r = i - 1;
        while (d >>= 1) if (r + d < n) if (check(i, r + d)) r += d;
        if (r - i + 1) ans += r - i + 1;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

const int maxn = 2e5 + 10;
int a[maxn], b[maxn], sta[20][maxn], stb[20][maxn];

void init(int n) {
    for (int i = 0; i < n; ++i) sta[0][i] = a[i], stb[0][i] = b[i];
    for (int i = 1; (1 << i) <= n; ++i) {
        for (int j = 0; j + (1 << i) <= n; ++j) {
            sta[i][j] = max(sta[i - 1][j], sta[i - 1][j + (1 << (i - 1))]);
            stb[i][j] = min(stb[i - 1][j], stb[i - 1][j + (1 << (i - 1))]);
        }
    }
}

pair<int, int> query(int l, int r) {
    int p = 31 - __builtin_clz(r - l + 1);
    return make_pair(max(sta[p][l], sta[p][r - (1 << p) + 1]), min(stb[p][l], stb[p][r- (1 << p) + 1]));
}

bool check(int l, int r) {
    pair<int, int> q = query(l, r);
    return q.first == q.second;
}

int32_t main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < n; ++i) cin >> b[i];
    init(n);
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
        int d = 1; while (d <= n) d <<= 1;
        int r = i - 1;
        while (d >>= 1) if (r + d < n) if (check(i, r + d)) r += d;
        ans += r - i + 1;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10, lg = 30;
int a[maxn], b[maxn];

struct sparse {
    int arr[2][lg][maxn];
    void init(int n) {
        for (int i = 0; i < n; ++i) {
            arr[0][0][i] = a[i];
            arr[1][0][i] = b[i];
        }
        for (int i = 1; (1 << i) <= n; ++i) {
            for (int j = 0; j + (1 << i) <= n; ++j) {
                arr[0][i][j] = max(arr[0][i - 1][j], arr[0][i - 1][j + (1 << (i - 1))]);
                arr[1][i][j] = min(arr[1][i - 1][j], arr[1][i - 1][j + (1 << (i - 1))]);
            }
        }
    }
    pair<int, int> query(int l, int r) {
        int p = 31 - __builtin_clz(r - l + 1);
        return make_pair(max(arr[0][p][l], arr[0][p][r - (1 << p) + 1]), min(arr[1][p][l], arr[1][p][r - (1 << p) + 1]));
    }
} sp;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < n; ++i) cin >> b[i];
    sp.init(n);
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
        int rr = i, rl = i, trr = i, trl = i;
        int d = 262144; while (d >>= 1) {
            if (rr + d < n) {
                auto p = sp.query(i, rr + d);
                if (p.first == p.second) trr = rr + d;
                if (p.first <= p.second) rr += d;
            }
            if (rl + d < n) {
                auto p = sp.query(i, rl + d);
                if (p.first == p.second) trl = rl + d;
                if (p.first < p.second) rl += d;
            }
        }
        if (sp.query(i, i).first == sp.query(i, i).second) trl = i;
        if (sp.query(i, trl).first != sp.query(i, trl).second || sp.query(i, trr).first != sp.query(i, trr).second) continue;
        ans += max(0, trr - trl + 1);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10, lg = 30;
int a[maxn], b[maxn];

struct sparse {
    int arr[2][lg][maxn];
    void init(int n) {
        for (int i = 0; i < n; ++i) {
            arr[0][0][i] = a[i];
            arr[1][0][i] = b[i];
        }
        for (int i = 1; (1 << i) <= n; ++i) {
            for (int j = 0; j + (1 << i) <= n; ++j) {
                arr[0][i][j] = max(arr[0][i - 1][j], arr[0][i - 1][j + (1 << (i - 1))]);
                arr[1][i][j] = min(arr[1][i - 1][j], arr[1][i - 1][j + (1 << (i - 1))]);
            }
        }
    }
    pair<int, int> query(int l, int r) {
        int p = 31 - __builtin_clz(r - l + 1);
        return make_pair(max(arr[0][p][l], arr[0][p][r - (1 << p) + 1]), min(arr[1][p][l], arr[1][p][r - (1 << p) + 1]));
    }
} sp;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < n; ++i) cin >> b[i];
    sp.init(n);
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
        int rr = i;
        int d = 262144; while (d >>= 1) {
            if (rr + d < n) {
                auto p = sp.query(i, rr + d);
                if (p.first <= p.second) rr += d;
            }
        }
        int rl = rr;
        d = 262144; while (d >>= 1) {
            if (rl - d >= i) {
                auto p = sp.query(i, rl - d);
                if (p.first <= p.second) rl -= d;
            }
        }
        // cout << "i = " << i << endl;
        // cout << "rl = " << rl << " rr = " << rr << endl;
        if (sp.query(i, rl).first != sp.query(i, rl).second || sp.query(i, rr).first != sp.query(i, rr).second) continue;
        // cout << "rl = " << rl << " rr = " << rr << endl;
        ans += max(0, rr - rl + 1);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 10;
int a[maxn], b[maxn], sta[20][maxn], stb[20][maxn];

void init(int n) {
    for (int i = 0; i < n; ++i) sta[0][i] = a[i], stb[0][i] = b[i];
    for (int i = 1; (1 << i) <= n; ++i) {
        for (int j = 0; j + (1 << i) <= n; ++j) {
            sta[i][j] = max(sta[i - 1][j], sta[i - 1][j + (1 << (i - 1))]);
            stb[i][j] = min(stb[i - 1][j], stb[i - 1][j + (1 << (i - 1))]);
        }
    }
}

pair<int, int> query(int l, int r) {
    int p = 31 - __builtin_clz(r - l + 1);
    return make_pair(max(sta[p][l], sta[p][r - (1 << p) + 1]), min(stb[p][l], stb[p][r- (1 << p) + 1]));
}

bool check(int l, int r) {
    pair<int, int> q = query(l, r);
    return q.first == q.second;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < n; ++i) cin >> b[i];
    init(n);
    long long ans = 0;
    for (int i = 0; i < n; ++i) {
        int d = 1; while (d <= n) d <<= 1;
        int r = i - 1;
        while (d >>= 1) if (r + d < n) if (check(i, r + d)) r += d;
        ans += r - i + 1;
    }
    cout << ans << endl;
    return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long lld;
const int N=20;

struct ST{
	int n, t, a[N][1<<N];
    void init(int _n, int _t){
		n=_n, t=_t;
        memset(a, t ? 0x00 : 0x10, sizeof(a));
		for(int i=0; i<n; i++)
            scanf("%d", &a[0][i]);
		for(int u=1; u<=lg(n); u++){
			int u2=1<<u, u3=u2>>1;
			for(int i=0; i+u2<=n; i++)
                a[u][i]=f(a[u-1][i], a[u-1][i+u3]);
		}
	}
	int query(int l, int r) const{
        int x=lg(r-l), x2=1<<x;
		return f(a[x][l], a[x][r-x2]);
	}
    int f(int x, int y) const{
        return t ? max(x, y) : min(x, y);
    }
	static int lg(int x){
		return 30-__builtin_clrsb(x);
	}
} s, t;

int main()
{
    int n;
    scanf("%d", &n);
    s.init(n, 1);
    t.init(n, 0);
    lld ans=0;

    for(int i=1; i<=n; i++)
    {
        int p=-1, q=-1;

        for(int l=0, r=i; l<r; )
        {
            int m=(l+r)>>1;

            if( s.query(m, i)<t.query(m, i) )
                r=m;
            else
                l=p=m+1;
        }

        for(int l=0, r=i; l<r; )
        {
            int m=(l+r)>>1;

            if( s.query(m, i)>t.query(m, i) )
                l=m+1;
            else
                r=q=m;
        }

        if( p>=q && q>=0 )
            ans+=p-q;
    }

    printf("%lld\n", ans);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long lld;

const int N=18;
struct ST
{
	int n, t, a[N][1<<N];
    void init(int _n, int _t){
		n=_n;
        t=_t;
        memset(a, t ? 0x00 : 0x10, sizeof(a));
		for(int i=0; i<n; i++)
			scanf("%d", &a[0][i]);
		for(int u=1; u<=lg(n); u++){
			int u2=1<<u, u3=u2>>1;
			for(int i=0; i+u2<=n; i++)
                a[u][i]=f(a[u-1][i], a[u-1][i+u3]);
		}
	}
	int query(int l, int r) const{
        int x=lg(r-l), x2=1<<x;
		return f(a[x][l], a[x][r-x2]);
	}
    int f(int x, int y) const{
        return t ? max(x, y) : min(x, y);
    }
	static int lg(int x){
		return 30-__builtin_clrsb(x);
	}
} s, t;

int main()
{
    int n;
    scanf("%d", &n);
    s.init(n, 1);
    t.init(n, 0);
    lld ans=0;

    for(int i=1; i<=n; i++)
    {
        int p=-1, q=-1;

        for(int l=0, r=i; l<r; )
        {
            int m=(l+r)>>1;

            if( s.query(m, i)<t.query(m, i) )
                r=m;
            else
                l=p=m+1;
        }

        for(int l=0, r=i; l<r; )
        {
            int m=(l+r)>>1;

            if( s.query(m, i)>t.query(m, i) )
                l=m+1;
            else
                r=q=m;
        }

        if( p>=q && q>=0 )
            ans+=p-q;
    }

    printf("%lld\n", ans);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long lld;
const int N=21;

struct ST{
	int n, t, a[N][1<<N];
    void init(int _n, int _t){
		n=_n, t=_t;
        memset(a, t ? 0x00 : 0x10, sizeof(a));
		for(int i=0; i<n; i++)
            scanf("%d", &a[0][i]);
		for(int u=1; u<=lg(n); u++){
			int u2=1<<u, u3=u2>>1;
			for(int i=0; i+u2<=n; i++)
                a[u][i]=f(a[u-1][i], a[u-1][i+u3]);
		}
	}
	int query(int l, int r) const{
        int x=lg(r-l), x2=1<<x;
		return f(a[x][l], a[x][r-x2]);
	}
    int f(int x, int y) const{
        return t ? max(x, y) : min(x, y);
    }
	static int lg(int x){
		return 30-__builtin_clrsb(x);
	}
} s, t;

int main()
{
    int n;
    scanf("%d", &n);
    s.init(n, 1);
    t.init(n, 0);
    lld ans=0;

    for(int i=1; i<=n; i++)
    {
        int p=-1, q=-1;

        for(int l=0, r=i; l<r; )
        {
            int m=(l+r)>>1;

            if( s.query(m, i)<t.query(m, i) )
                r=m;
            else
                l=p=m+1;
        }

        for(int l=0, r=i; l<r; )
        {
            int m=(l+r)>>1;

            if( s.query(m, i)>t.query(m, i) )
                l=m+1;
            else
                r=q=m;
        }

        if( p>=q && q>=0 )
            ans+=p-q;
    }

    printf("%lld\n", ans);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long lld;
const int N=18;

struct ST{
	int n, t, a[N][1<<N];
    void init(int _n, int _t){
		n=_n, t=_t;
        memset(a, t ? 0x00 : 0x10, sizeof(a));
		for(int i=0; i<n; i++)
            scanf("%d", &a[0][i]);
		for(int u=1; u<=lg(n); u++){
			int u2=1<<u, u3=u2>>1;
			for(int i=0; i+u2<=n; i++)
                a[u][i]=f(a[u-1][i], a[u-1][i+u3]);
		}
	}
	int query(int l, int r) const{
        int x=lg(r-l), x2=1<<x;
		return f(a[x][l], a[x][r-x2]);
	}
    int f(int x, int y) const{
        return t ? max(x, y) : min(x, y);
    }
	static int lg(int x){
		return 30-__builtin_clrsb(x);
	}
} s, t;

int main()
{
    int n;
    scanf("%d", &n);
    s.init(n, 1);
    t.init(n, 0);
    lld ans=0;

    for(int i=1; i<=n; i++)
    {
        int p=-1, q=-1;

        for(int l=0, r=i; l<r; )
        {
            int m=(l+r)>>1;

            if( s.query(m, i)<t.query(m, i) )
                r=m;
            else
                l=p=m+1;
        }

        for(int l=0, r=i; l<r; )
        {
            int m=(l+r)>>1;

            if( s.query(m, i)>t.query(m, i) )
                l=m+1;
            else
                r=q=m;
        }

        if( p>=q && q>=0 )
            ans+=p-q;
    }

    printf("%lld\n", ans);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long lld;
const int N=18;

struct ST{
    int (*f)(int, int);
	int n, a[N][1<<N];
    void init(int _n, bool _t){
        f= _t ? [](int x, int y){return max(x, y);} : [](int x, int y){return min(x, y);} ;
        n=_n;
        memset(a, _t ? 0x00 : 0x10, sizeof(a));
		for(int i=0; i<n; i++)
            scanf("%d", &a[0][i]);
		for(int u=1; u<=lg(n); u++){
			int u2=1<<u, u3=u2>>1;
			for(int i=0; i+u2<=n; i++)
                a[u][i]=f(a[u-1][i], a[u-1][i+u3]);
		}
	}
	int query(int l, int r) const{
        int x=lg(r-l), x2=1<<x;
		return f(a[x][l], a[x][r-x2]);
	}
    /*
    int f(int x, int y) const{
        return t ? max(x, y) : min(x, y);
    }
     */
	static int lg(int x){
		return 30-__builtin_clrsb(x);
	}
} s, t;

int main()
{
    int n;
    scanf("%d", &n);
    s.init(n, 1);
    t.init(n, 0);
    lld ans=0;

    for(int i=1; i<=n; i++)
    {
        int p=-1, q=-1;

        for(int l=0, r=i; l<r; )
        {
            int m=(l+r)>>1;

            if( s.query(m, i)<t.query(m, i) )
                r=m;
            else
                l=p=m+1;
        }

        for(int l=0, r=i; l<r; )
        {
            int m=(l+r)>>1;

            if( s.query(m, i)>t.query(m, i) )
                l=m+1;
            else
                r=q=m;
        }

        if( p>=q && q>=0 )
            ans+=p-q;
    }

    printf("%lld\n", ans);
}


/*
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<map>

using namespace std;

int n;
int a[200005];
int b[200005];

int ma[200005][25];
int mi[200005][25];

void cal()
{
    for(int i=0;i<n;i++)
    {
        ma[i][0]=a[i];
        mi[i][0]=b[i];
    }

    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=0;i+(1<<j)<=n;i++)
        {
            ma[i][j]=max(ma[i][j-1],ma[i+(1<<(j-1))][j-1]);
            mi[i][j]=min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);
        }
    }
}

int gma(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=(r-l+1))
        k++;

    return max(ma[l][k],ma[r-(1<<k)+1][k]);
}

int gmi(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=(r-l+1))
        k++;

    return min(mi[l][k],mi[r-(1<<k)+1][k]);
}

int bins(int sta)
{
    int l=sta,r=n-1;
    int maxn,minn;
    while(l<r)
    {
        int mid=(l+r)/2;
        maxn=gma(sta,mid);
        minn=gmi(sta,mid);

        if(maxn>minn)
        {
            r=mid-1;
        }
        else if(maxn<minn)
        {
            l=mid+1;
        }
        else if(maxn==minn)
        {
            r=mid;
        }
    }
    if(l==r && gma(sta,l)==gmi(sta,r))
        return l;
    else
        return -1;

}

int bine(int sta)
{
    int l=sta,r=n-1;
    int maxn,minn;
    while(l<r)
    {
        int mid=(l+r)/2;
        maxn=gma(sta,mid);
        minn=gmi(sta,mid);

        if(maxn>minn)
        {
            r=mid-1;
        }
        else if(maxn<minn)
        {
            l=mid+1;
        }
        else if(maxn==minn)
        {
            l=mid;
        }

        if((l+1)==r)
        {
            if(gma(sta,r)==gmi(sta,r))
                return r;
            if(gma(sta,l)==gmi(sta,l))
                return l;

            return -1;
        }
    }
    if(l==r && gma(sta,l)==gmi(sta,r))
        return l;
    else
        return -1;

}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<n;i++)
            scanf("%d",&b[i]);

        cal();

        long long ans=0;
        for(int i=0;i<n;i++)
        {
            int s=bins(i);
            int e=bine(i);

            //printf("%d : %d %d\n",i,s,e);
            if(s!=-1 && e!=-1)
            {
                ans+=(e-s+1);
            }
        }
        printf("%lld\n",ans);
    }

}
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<map>

using namespace std;

int n;
int a[200005];
int b[200005];

int ma[200005][25];
int mi[200005][25];

void cal()
{
    for(int i=0;i<n;i++)
    {
        ma[i][0]=a[i];
        mi[i][0]=b[i];
    }

    for(int j=1;(1<<j)<=n;j++)
    {
        for(int i=0;i+(1<<j)<=n;i++)
        {
            ma[i][j]=max(ma[i][j-1],ma[i+(1<<(j-1))][j-1]);
            mi[i][j]=min(mi[i][j-1],mi[i+(1<<(j-1))][j-1]);
        }
    }
}

int gma(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=(r-l+1))
        k++;

    return max(ma[l][k],ma[r-(1<<k)+1][k]);
}

int gmi(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=(r-l+1))
        k++;

    return min(mi[l][k],mi[r-(1<<k)+1][k]);
}

int bins(int sta)
{
    int l=sta,r=n-1;
    int maxn,minn;
    int ans=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        maxn=gma(sta,mid);
        minn=gmi(sta,mid);

        if(maxn>minn)
        {
            r=mid-1;
        }
        else if(maxn<minn)
        {
            l=mid+1;
        }
        else if(maxn==minn)
        {
            r=mid-1;
            ans=mid;
        }
    }
    return ans;

}

int bine(int sta)
{
    int l=sta,r=n-1;
    int maxn,minn;
    int ans=-1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        maxn=gma(sta,mid);
        minn=gmi(sta,mid);

        if(maxn>minn)
        {
            r=mid-1;
        }
        else if(maxn<minn)
        {
            l=mid+1;
        }
        else if(maxn==minn)
        {
            l=mid+1;
            ans=mid;
        }
    }
    return ans;
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        for(int i=0;i<n;i++)
            scanf("%d",&b[i]);

        cal();

        long long ans=0;
        for(int i=0;i<n;i++)
        {
            int s=bins(i);
            int e=bine(i);

            //printf("%d : %d %d\n",i,s,e);
            if(s!=-1 && e!=-1)
            {
                ans+=(e-s+1);
            }
        }
        printf("%lld\n",ans);
    }

}


#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<cassert>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<typeinfo>
#include<limits>
#include<ctime>
#include<random>
#include<iomanip>
#include<bitset>
#include<unordered_map>
#include<stack>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define pb push_back
#define sc second
#define fr first
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))
#define all(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define GL(x) _for(i,0,(x)) fprintf(stderr,"\n");
#define debugline fprintf(stderr,"--------------\n");
#define uid uniform_int_distribution

typedef long long LL;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<LL,LL> pLL;
typedef pair<int,int> pii;
typedef vector<LL> vL;
typedef vector<vL> vLL;
typedef unsigned long long uLL;
typedef default_random_engine dre;

const int maxn =100000+10;
const int maxm = 21+10;
const LL inf= 4e18;
const LL mod = 998244353;
const double eps = 1e-8;
const LL o = 1;
const int apsz = 26;
const int hnum = 3;

#ifdef I128IO
char buff[100];
template<typename T>
inline void read(T& a)
{
	scanf("%s",buff);
	T ans = 0;
	for(int i = 0;buff[i];i++)
		ans*=10,ans+=buff[i]-'0';
	a = ans;
}
template<typename T>
inline void write(T x)
{
	buff[0] = '0';
	int cnt = 0;
	while(x)
	{
		buff[cnt++] = '0'+x%10;
		x/=10;
	}
	if(!cnt)cnt++;
	reverse(buff,buff+cnt);
	buff[cnt] = '\0';
	printf("%s",buff);
}
#endif

vL a[3];
int n[3];
LL f(LL x,LL y,LL z){
	return (x-y)*(x-y)+(x-z)*(x-z)+(z-y)*(z-y);
}

inline LL solve(int o)
{
	LL ans = inf;
	_for(i,o,n[o])
	{
		int p[3];
		vL e[3];
		_for(j,0,3)
		{
			if(j==o)
			{
				e[j].pb(a[j][i]);
				continue;
			}
			p[j] = lower_bound(all(a[j]),a[0][i])-a[j].begin();
			if(p[j]<n[j])
			{
				e[j].pb(a[j][p[j]]);

			}
			p[j]--;
			if(p[j]>=0)
			{
				e[j].pb(a[j][p[j]]);
			}
			p[j]+=2;
			if(p[j]<n[j])
			{
				e[j].pb(a[j][p[j]]);
			}
		}
		for(auto x:e[0])
		{
			for(auto y:e[1])
			{
				for(auto z:e[2])
				{
					ans = min(f(x,y,z),ans);
				}
			}
		}
	}
	return ans;
}

int main(void)
{
	int T;
	sci(T);
	for(;T--;)
	{
		_for(i,0,3){
			sci(n[i]);
			a[i] = vL(n[i]);
		}
		_for(i,0,3)
		{
			_for(j,0,n[i]) scl(a[i][j]);
			sort(all(a[i]));
			a[i].erase(unique(all(a[i])),a[i].end());
		}
		printf("%lld\n",solve(0));
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<cassert>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<typeinfo>
#include<limits>
#include<ctime>
#include<random>
#include<iomanip>
#include<bitset>
#include<unordered_map>
#include<stack>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define pb push_back
#define sc second
#define fr first
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))
#define all(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define GL(x) _for(i,0,(x)) fprintf(stderr,"\n");
#define debugline fprintf(stderr,"--------------\n");
#define uid uniform_int_distribution

typedef long long LL;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<LL,LL> pLL;
typedef pair<int,int> pii;
typedef vector<LL> vL;
typedef vector<vL> vLL;
typedef unsigned long long uLL;
typedef default_random_engine dre;

const int maxn =100000+10;
const int maxm = 21+10;
const LL inf= 4e18;
const LL mod = 998244353;
const double eps = 1e-8;
const LL o = 1;
const int apsz = 26;
const int hnum = 3;

#ifdef I128IO
char buff[100];
template<typename T>
inline void read(T& a)
{
	scanf("%s",buff);
	T ans = 0;
	for(int i = 0;buff[i];i++)
		ans*=10,ans+=buff[i]-'0';
	a = ans;
}
template<typename T>
inline void write(T x)
{
	buff[0] = '0';
	int cnt = 0;
	while(x)
	{
		buff[cnt++] = '0'+x%10;
		x/=10;
	}
	if(!cnt)cnt++;
	reverse(buff,buff+cnt);
	buff[cnt] = '\0';
	printf("%s",buff);
}
#endif

vL a[3];
int n[3];
LL f(LL x,LL y,LL z){
	return (x-y)*(x-y)+(x-z)*(x-z)+(z-y)*(z-y);
}

inline LL solve(int o)
{
	LL ans = inf;
	_for(i,o,n[o])
	{
		int p[3];
		vL e[3];
		_for(j,0,3)
		{
			if(j==o)
			{
				e[j].pb(a[j][i]);
				continue;
			}
			p[j] = lower_bound(all(a[j]),a[o][i])-a[j].begin();
			if(p[j]<n[j])
			{
				e[j].pb(a[j][p[j]]);
			}
			if(p[j]-1>=0)
			{
				e[j].pb(a[j][p[j]-1]);
			}
			if(p[j]+1<n[j])
			{
				e[j].pb(a[j][p[j]+1]);
			}
			if(p[j]-2>=0)
			{
				e[j].pb(a[j][p[j]-2]);
			}
			if(p[j]+2<n[j])
			{
				e[j].pb(a[j][p[j]+2]);
			}
		}
		int j = 0;
		_for(k,0,3)
		{
			if(k!=o)
			{
				j = k;
			}
		}
		for(auto x:e[o])
		{
			for(auto y:e[j])
			{
				for(auto z:e[(j^3^o)])
				{
					ans = min(f(x,y,z),ans);
				}
			}
		}
	}
	return ans;
}

inline LL check()
{
	LL ans = inf;
	_for(i,0,n[0])
	{
		_for(j,0,n[1])
		{
			_for(k,0,n[2])
			{
				LL x,y,z;
				x = a[0][i];y = a[1][j];
				z = a[2][k];
				ans = min(ans,f(x,y,z));
			}
		}
	}
	return ans;
}

int main(void)
{
	int T;
	sci(T);
	for(;T--;)
	{
		_for(i,0,3)
		{
			sci(n[i]);
			a[i] = vL(n[i]);
		}
		_for(i,0,3)
		{
			_for(j,0,n[i]) scl(a[i][j]);
			sort(all(a[i]));
			a[i].erase(unique(all(a[i])),a[i].end());
		}
		LL ans = inf;
		_for(i,0,3) ans = min(ans,solve(i));
		printf("%lld\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<cassert>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<typeinfo>
#include<limits>
#include<ctime>
#include<random>
#include<iomanip>
#include<bitset>
#include<unordered_map>
#include<stack>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define pb push_back
#define sc second
#define fr first
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))
#define all(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define GL(x) _for(i,0,(x)) fprintf(stderr,"\n");
#define debugline fprintf(stderr,"--------------\n");
#define uid uniform_int_distribution

typedef long long LL;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<LL,LL> pLL;
typedef pair<int,int> pii;
typedef vector<LL> vL;
typedef vector<vL> vLL;
typedef unsigned long long uLL;
typedef default_random_engine dre;

const int maxn =100000+10;
const int maxm = 21+10;
const LL inf= 4e18;
const LL mod = 998244353;
const double eps = 1e-8;
const LL o = 1;
const int apsz = 26;
const int hnum = 3;

#ifdef I128IO
char buff[100];
template<typename T>
inline void read(T& a)
{
	scanf("%s",buff);
	T ans = 0;
	for(int i = 0;buff[i];i++)
		ans*=10,ans+=buff[i]-'0';
	a = ans;
}
template<typename T>
inline void write(T x)
{
	buff[0] = '0';
	int cnt = 0;
	while(x)
	{
		buff[cnt++] = '0'+x%10;
		x/=10;
	}
	if(!cnt)cnt++;
	reverse(buff,buff+cnt);
	buff[cnt] = '\0';
	printf("%s",buff);
}
#endif

vL a[3];
int n[3];
LL f(LL x,LL y,LL z){
	return (x-y)*(x-y)+(x-z)*(x-z)+(z-y)*(z-y);
}

inline LL solve(int o)
{
	LL ans = inf;
	_for(i,o,n[o])
	{
		int p[3];
		vL e[3];
		_for(j,0,3)
		{
			if(j==o)
			{
				e[j].pb(a[j][i]);
				continue;
			}
			p[j] = lower_bound(all(a[j]),a[0][i])-a[j].begin();
			if(p[j]<n[j])
			{
				e[j].pb(a[j][p[j]]);
			}
			if(p[j]-1>=0)
			{
				e[j].pb(a[j][p[j]-1]);
			}
			if(p[j]+1<n[j])
			{
				e[j].pb(a[j][p[j]+1]);
			}
			if(p[j]-2>=0)
			{
				e[j].pb(a[j][p[j]-2]);
			}
			if(p[j]+2<n[j])
			{
				e[j].pb(a[j][p[j]+2]);
			}
		}
		for(auto x:e[0])
		{
			for(auto y:e[1])
			{
				for(auto z:e[2])
				{
					ans = min(f(x,y,z),ans);
				}
			}
		}
	}
	return ans;
}

inline LL check()
{
	LL ans = inf;
	_for(i,0,n[0])
	{
		_for(j,0,n[1])
		{
			_for(k,0,n[2])
			{
				LL x,y,z;
				x = a[0][i];y = a[1][j];
				z = a[2][k];
				ans = min(ans,f(x,y,z));
			}
		}
	}
	return ans;
}

int main(void)
{
	int T;
	sci(T);
	for(;T--;)
	{
		_for(i,0,3)
		{
			sci(n[i]);
			a[i] = vL(n[i]);
		}
		_for(i,0,3)
		{
			_for(j,0,n[i]) scl(a[i][j]);
			sort(all(a[i]));
			a[i].erase(unique(all(a[i])),a[i].end());
		}
		printf("%lld\n",min({solve(0),solve(1),solve(2)}));
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define fr first
#define sc second
#define all(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false);cout.tie(nullptr);
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))

typedef long long LL;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<LL> vL;
typedef vector<vL> vLL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;

#ifdef I128IO
typedef __int128 I128;
char buff[200];
template<typename T>
void read(T& o)
{
	scanf("%s",buff);
	printf("%s\n",buff);
	T f = 1;
	int i = 0;
	if(buff[0]=='-')i++,f = -1;
	for(;buff[i];++i)
	{
		o*=10,o+=buff[i]-'0';
	}
	o*=f;
}

template<typename T>
void write(T o)
{
	int len = 0;
	buff[0] = '0';
	if(o==0)
	{
		putchar('0');
		return;
	}
	int f = 1;
	if(o<0) f = -1;
	o*=f;
	for(;o;o/=10) buff[len++] = o%10+'0';
	if(f==-1) buff[len++] = '-';
	reverse(buff,buff+len);
	buff[len++] ='\0';
	printf("%s",buff);
}
#endif

const int maxn = 100000+10;
const int maxm = 3;
const LL inf = 4e18;

int n[maxm];
vL a[maxm];

inline LL f(LL x,LL y,LL z)
{
	return (x-y)*(x-y)+(x-z)*(x-z)+(y-z)*(y-z);
}

inline LL solve(int o)
{
	int k = 0;
	LL ans = inf;
	for(;k<maxm;k++)
	{
		if(k!=o)break;
	}
	_for(i,0,n[o])
	{
		vL e[3];
		_for(j,0,maxm)
		{
			if(j==o)
			{
				e[j].pb(a[o][i]);
				continue;
			}
			auto p = lower_bound(all(a[j]),a[o][i])-a[j].begin();
			if(p<n[j]) e[j].pb(a[j][p]);
			if(--p>=0) e[j].pb(a[j][p]);
		}
		for(auto x:e[o])
		{
			for(auto y:e[k])
			{
				for(auto z:e[3^k^o])
				{
					ans = min(ans,f(x,y,z));
				}
			}
		}
	}
	return ans;
}

int main(void)
{
	int T;
	sci(T);
	for(;T--;)
	{
		_for(i,0,maxm){
			sci(n[i]);
		}
		_for(i,0,maxm)
		{
			a[i] = vL(n[i]);
			_for(j,0,n[i]) scl(a[i][j]);
			sort(all(a[i]));
			a[i].erase(unique(all(a[i])),a[i].end());
		}
		_for(i,0,maxm){
			n[i] = a[i].size();
		}
		printf("%lld\n",min({solve(1),solve(0),solve(2)}));
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<cassert>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<typeinfo>
#include<limits>
#include<ctime>
#include<random>
#include<iomanip>
#include<bitset>
#include<unordered_map>
#include<stack>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define pb push_back
#define sc second
#define fr first
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))
#define all(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define GL(x) _for(i,0,(x)) fprintf(stderr,"\n");
#define debugline fprintf(stderr,"--------------\n");
#define uid uniform_int_distribution

typedef long long LL;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<LL,LL> pLL;
typedef pair<int,int> pii;
typedef vector<LL> vL;
typedef vector<vL> vLL;
typedef unsigned long long uLL;
typedef default_random_engine dre;

const int maxn =100000+10;
const int maxm = 21+10;
const LL inf= 4e18;
const LL mod = 998244353;
const double eps = 1e-8;
const LL o = 1;
const int apsz = 26;
const int hnum = 3;

#ifdef I128IO
char buff[100];
template<typename T>
inline void read(T& a)
{
	scanf("%s",buff);
	T ans = 0;
	for(int i = 0;buff[i];i++)
		ans*=10,ans+=buff[i]-'0';
	a = ans;
}
template<typename T>
inline void write(T x)
{
	buff[0] = '0';
	int cnt = 0;
	while(x)
	{
		buff[cnt++] = '0'+x%10;
		x/=10;
	}
	if(!cnt)cnt++;
	reverse(buff,buff+cnt);
	buff[cnt] = '\0';
	printf("%s",buff);
}
#endif

vL a[3];
int n[3];
LL f(LL x,LL y,LL z){
	return (x-y)*(x-y)+(x-z)*(x-z)+(z-y)*(z-y);
}

inline LL solve(int o)
{
	LL ans = inf;
	_for(i,o,n[o])
	{
		int p[3];
		vL e[3];
		_for(j,0,3)
		{
			if(j==o)
			{
				e[j].pb(a[j][i]);
				continue;
			}
			p[j] = lower_bound(all(a[j]),a[o][i])-a[j].begin();
			if(p[j]<n[j])
			{
				e[j].pb(a[j][p[j]]);
			}
			if(p[j]-1>=0)
			{
				e[j].pb(a[j][p[j]-1]);
			}
			if(p[j]+1<n[j])
			{
				e[j].pb(a[j][p[j]+1]);
			}
			if(p[j]-2>=0)
			{
				e[j].pb(a[j][p[j]-2]);
			}
			if(p[j]+2<n[j])
			{
				e[j].pb(a[j][p[j]+2]);
			}
		}
		for(auto x:e[0])
		{
			for(auto y:e[1])
			{
				for(auto z:e[2])
				{
					ans = min(f(x,y,z),ans);
				}
			}
		}
	}
	return ans;
}

inline LL check()
{
	LL ans = inf;
	_for(i,0,n[0])
	{
		_for(j,0,n[1])
		{
			_for(k,0,n[2])
			{
				LL x,y,z;
				x = a[0][i];y = a[1][j];
				z = a[2][k];
				ans = min(ans,f(x,y,z));
			}
		}
	}
	return ans;
}

int main(void)
{
	int T;
	sci(T);
	for(;T--;)
	{
		_for(i,0,3)
		{
			sci(n[i]);
			a[i] = vL(n[i]);
		}
		_for(i,0,3)
		{
			_for(j,0,n[i]) scl(a[i][j]);
			sort(all(a[i]));
			a[i].erase(unique(all(a[i])),a[i].end());
		}
		LL ans = inf;
		_for(i,0,3) ans = min(ans,solve(i));
		printf("%lld\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<cassert>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<typeinfo>
#include<limits>
#include<ctime>
#include<random>
#include<iomanip>
#include<bitset>
#include<unordered_map>
#include<stack>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define pb push_back
#define sc second
#define fr first
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))
#define all(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define GL(x) _for(i,0,(x)) fprintf(stderr,"\n");
#define debugline fprintf(stderr,"--------------\n");
#define uid uniform_int_distribution

typedef long long LL;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<LL,LL> pLL;
typedef pair<int,int> pii;
typedef vector<LL> vL;
typedef vector<vL> vLL;
typedef unsigned long long uLL;
typedef default_random_engine dre;

const int maxn =100000+10;
const int maxm = 21+10;
const LL inf= 4e18;
const LL mod = 998244353;
const double eps = 1e-8;
const LL o = 1;
const int apsz = 26;
const int hnum = 3;

#ifdef I128IO
char buff[100];
template<typename T>
inline void read(T& a)
{
	scanf("%s",buff);
	T ans = 0;
	for(int i = 0;buff[i];i++)
		ans*=10,ans+=buff[i]-'0';
	a = ans;
}
template<typename T>
inline void write(T x)
{
	buff[0] = '0';
	int cnt = 0;
	while(x)
	{
		buff[cnt++] = '0'+x%10;
		x/=10;
	}
	if(!cnt)cnt++;
	reverse(buff,buff+cnt);
	buff[cnt] = '\0';
	printf("%s",buff);
}
#endif

vL a[3];
int n[3];
LL f(LL x,LL y,LL z){
	return (x-y)*(x-y)+(x-z)*(x-z)+(z-y)*(z-y);
}

inline LL solve(int o)
{
	LL ans = inf;
	_for(i,o,n[o])
	{
		int p[3];
		vL e[3];
		_for(j,0,3)
		{
			if(j==o)
			{
				e[j].pb(a[j][i]);
				continue;
			}
			p[j] = lower_bound(all(a[j]),a[0][i])-a[j].begin();
			if(p[j]<n[j])
			{
				e[j].pb(a[j][p[j]]);

			}
			p[j]--;
			if(p[j]>=0)
			{
				e[j].pb(a[j][p[j]]);
			}
			p[j]+=2;
			if(p[j]<n[j])
			{
				e[j].pb(a[j][p[j]]);
			}
		}
		for(auto x:e[0])
		{
			for(auto y:e[1])
			{
				for(auto z:e[2])
				{
					ans = min(f(x,y,z),ans);
				}
			}
		}
	}
	return ans;
}

int main(void)
{
	int T;
	sci(T);
	for(;T--;)
	{
		_for(i,0,3){
			sci(n[i]);
			a[i] = vL(n[i]);
		}
		_for(i,0,3)
		{
			_for(j,0,n[i]) scl(a[i][j]);
			sort(all(a[i]));
		}
		LL ans = solve(0);
		printf("%lld\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<cassert>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<typeinfo>
#include<limits>
#include<ctime>
#include<random>
#include<iomanip>
#include<bitset>
#include<unordered_map>
#include<stack>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define pb push_back
#define sc second
#define fr first
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))
#define all(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define GL(x) _for(i,0,(x)) fprintf(stderr,"\n");
#define debugline fprintf(stderr,"--------------\n");
#define uid uniform_int_distribution

typedef long long LL;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<LL,LL> pLL;
typedef pair<int,int> pii;
typedef vector<LL> vL;
typedef vector<vL> vLL;
typedef unsigned long long uLL;
typedef default_random_engine dre;

const int maxn =100000+10;
const int maxm = 21+10;
const LL inf= 4e18;
const LL mod = 998244353;
const double eps = 1e-8;
const LL o = 1;
const int apsz = 26;
const int hnum = 3;

#ifdef I128IO
char buff[100];
template<typename T>
inline void read(T& a)
{
	scanf("%s",buff);
	T ans = 0;
	for(int i = 0;buff[i];i++)
		ans*=10,ans+=buff[i]-'0';
	a = ans;
}
template<typename T>
inline void write(T x)
{
	buff[0] = '0';
	int cnt = 0;
	while(x)
	{
		buff[cnt++] = '0'+x%10;
		x/=10;
	}
	if(!cnt)cnt++;
	reverse(buff,buff+cnt);
	buff[cnt] = '\0';
	printf("%s",buff);
}
#endif

vL a[3];
int n[3];
LL f(LL x,LL y,LL z){
	return (x-y)*(x-y)+(x-z)*(x-z)+(z-y)*(z-y);
}

inline LL solve(int o)
{
	LL ans = inf;
	_for(i,o,n[o])
	{
		int p[3];
		vL e[3];
		_for(j,0,3)
		{
			if(j==o)
			{
				e[j].pb(a[j][i]);
				continue;
			}
			p[j] = lower_bound(all(a[j]),a[0][i])-a[j].begin();
			if(p[j]<n[j])
			{
				e[j].pb(a[j][p[j]]);
			}
			if(p[j]-1>=0)
			{
				e[j].pb(a[j][p[j]-1]);
			}
			if(p[j]+1<n[j])
			{
				e[j].pb(a[j][p[j]+1]);
			}
			if(p[j]-2>=0)
			{
				e[j].pb(a[j][p[j]-2]);
			}
			if(p[j]+2<n[j])
			{
				e[j].pb(a[j][p[j]+2]);
			}
		}
		for(auto x:e[0])
		{
			for(auto y:e[1])
			{
				for(auto z:e[2])
				{
					ans = min(f(x,y,z),ans);
				}
			}
		}
	}
	return ans;
}

inline LL check()
{
	LL ans = inf;
	_for(i,0,n[0])
	{
		_for(j,0,n[1])
		{
			_for(k,0,n[2])
			{
				LL x,y,z;
				x = a[0][i];y = a[1][j];
				z = a[2][k];
				ans = min(ans,f(x,y,z));
			}
		}
	}
	return ans;
}

int main(void)
{
	int T;
	sci(T);
	for(;T--;)
	{
		_for(i,0,3)
		{
			sci(n[i]);
			a[i] = vL(n[i]);
		}
		_for(i,0,3)
		{
			_for(j,0,n[i]) scl(a[i][j]);
			sort(all(a[i]));
			a[i].erase(unique(all(a[i])),a[i].end());
		}
		printf("%lld\n",solve(0));
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<cassert>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<typeinfo>
#include<limits>
#include<ctime>
#include<random>
#include<iomanip>
#include<bitset>
#include<unordered_map>
#include<stack>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define pb push_back
#define sc second
#define fr first
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))
#define all(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define GL(x) _for(i,0,(x)) fprintf(stderr,"\n");
#define debugline fprintf(stderr,"--------------\n");
#define uid uniform_int_distribution

typedef long long LL;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<LL,LL> pLL;
typedef pair<int,int> pii;
typedef vector<LL> vL;
typedef vector<vL> vLL;
typedef unsigned long long uLL;
typedef default_random_engine dre;

const int maxn =100000+10;
const int maxm = 21+10;
const LL inf= 4e18;
const LL mod = 998244353;
const double eps = 1e-8;
const LL o = 1;
const int apsz = 26;
const int hnum = 3;

#ifdef I128IO
char buff[100];
template<typename T>
inline void read(T& a)
{
	scanf("%s",buff);
	T ans = 0;
	for(int i = 0;buff[i];i++)
		ans*=10,ans+=buff[i]-'0';
	a = ans;
}
template<typename T>
inline void write(T x)
{
	buff[0] = '0';
	int cnt = 0;
	while(x)
	{
		buff[cnt++] = '0'+x%10;
		x/=10;
	}
	if(!cnt)cnt++;
	reverse(buff,buff+cnt);
	buff[cnt] = '\0';
	printf("%s",buff);
}
#endif

vL a[3];
int n[3];
LL f(LL x,LL y,LL z){
	return (x-y)*(x-y)+(x-z)*(x-z)+(z-y)*(z-y);
}

inline LL solve(int o)
{
	LL ans = inf;
	_for(i,o,n[o])
	{
		int p[3];
		vL e[3];
		_for(j,0,3)
		{
			if(j==o)
			{
				e[j].pb(a[j][i]);
				continue;
			}
			p[j] = lower_bound(all(a[j]),a[0][i])-a[j].begin();
			if(p[j]<n[j])
			{
				e[j].pb(a[j][p[j]]);
			}
			if(p[j]-1>=0)
			{
				e[j].pb(a[j][p[j]-1]);
			}
			if(p[j]+1<n[j])
			{
				e[j].pb(a[j][p[j]+1]);
			}
			if(p[j]-2>=0)
			{
				e[j].pb(a[j][p[j]-2]);
			}
			if(p[j]+2<n[j])
			{
				e[j].pb(a[j][p[j]+2]);
			}
		}
		for(auto x:e[0])
		{
			for(auto y:e[1])
			{
				for(auto z:e[2])
				{
					ans = min(f(x,y,z),ans);
				}
			}
		}
	}
	return ans;
}

inline LL check()
{
	LL ans = inf;
	_for(i,0,n[0])
	{
		_for(j,0,n[1])
		{
			_for(k,0,n[2])
			{
				LL x,y,z;
				x = a[0][i];y = a[1][j];
				z = a[2][k];
				ans = min(ans,f(x,y,z));
			}
		}
	}
	return ans;
}

int main(void)
{
	int T;
	sci(T);
	for(;T--;)
	{
		_for(i,0,3)
		{
			sci(n[i]);
			a[i] = vL(n[i]);
		}
		_for(i,0,3)
		{
			_for(j,0,n[i]) scl(a[i][j]);
			sort(all(a[i]));
			a[i].erase(unique(all(a[i])),a[i].end());
		}
		LL ans = inf;
		_for(i,0,3) ans = min(ans,solve(i));
		printf("%lld\n",ans);
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#include<cassert>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<typeinfo>
#include<limits>
#include<ctime>
#include<random>
#include<iomanip>
#include<bitset>
#include<unordered_map>
#include<stack>

using namespace std;

#define _for(i,a,b) for(int i = (a);i<(b);++i)
#define _rep(i,a,b) for(int i = (a);i<=(b);++i)
#define _dep(i,a,b) for(int i = (a);i>=(b);--i)
#define pb push_back
#define sc second
#define fr first
#define sci(x) scanf("%d",&(x))
#define scl(x) scanf("%lld",&(x))
#define all(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define GL(x) _for(i,0,(x)) fprintf(stderr,"\n");
#define debugline fprintf(stderr,"--------------\n");
#define uid uniform_int_distribution

typedef long long LL;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<LL,LL> pLL;
typedef pair<int,int> pii;
typedef vector<LL> vL;
typedef vector<vL> vLL;
typedef unsigned long long uLL;
typedef default_random_engine dre;

const int maxn =100000+10;
const int maxm = 21+10;
const LL inf= 4e18;
const LL mod = 998244353;
const double eps = 1e-8;
const LL o = 1;
const int apsz = 26;
const int hnum = 3;

#ifdef I128IO
char buff[100];
template<typename T>
inline void read(T& a)
{
	scanf("%s",buff);
	T ans = 0;
	for(int i = 0;buff[i];i++)
		ans*=10,ans+=buff[i]-'0';
	a = ans;
}
template<typename T>
inline void write(T x)
{
	buff[0] = '0';
	int cnt = 0;
	while(x)
	{
		buff[cnt++] = '0'+x%10;
		x/=10;
	}
	if(!cnt)cnt++;
	reverse(buff,buff+cnt);
	buff[cnt] = '\0';
	printf("%s",buff);
}
#endif

vL a[3];
int n[3];
LL f(LL x,LL y,LL z){
	return (x-y)*(x-y)+(x-z)*(x-z)+(z-y)*(z-y);
}

inline LL solve(int o)
{
	LL ans = inf;
	_for(i,o,n[o])
	{
		int p[3];
		vL e[3];
		_for(j,0,3)
		{
			if(j==o)
			{
				e[j].pb(a[j][i]);
				continue;
			}
			p[j] = lower_bound(all(a[j]),a[0][i])-a[j].begin();
			if(p[j]<n[j])
			{
				e[j].pb(a[j][p[j]]);
			}
			if(p[j]-1>=0)
			{
				e[j].pb(a[j][p[j]-1]);
			}
			if(p[j]+1<n[j])
			{
				e[j].pb(a[j][p[j]+1]);
			}
			if(p[j]-2>=0)
			{
				e[j].pb(a[j][p[j]-2]);
			}
			if(p[j]+2<n[j])
			{
				e[j].pb(a[j][p[j]+2]);
			}
		}
		int j = 0;
		_for(k,0,3)
		{
			if(k!=o)
			{
				j = k;
			}
		}
		for(auto x:e[o])
		{
			for(auto y:e[j])
			{
				for(auto z:e[(j^3^o)])
				{
					ans = min(f(x,y,z),ans);
				}
			}
		}
	}
	return ans;
}

inline LL check()
{
	LL ans = inf;
	_for(i,0,n[0])
	{
		_for(j,0,n[1])
		{
			_for(k,0,n[2])
			{
				LL x,y,z;
				x = a[0][i];y = a[1][j];
				z = a[2][k];
				ans = min(ans,f(x,y,z));
			}
		}
	}
	return ans;
}

int main(void)
{
	int T;
	sci(T);
	for(;T--;)
	{
		_for(i,0,3)
		{
			sci(n[i]);
			a[i] = vL(n[i]);
		}
		_for(i,0,3)
		{
			_for(j,0,n[i]) scl(a[i][j]);
			sort(all(a[i]));
			a[i].erase(unique(all(a[i])),a[i].end());
		}
		LL ans = inf;
		_for(i,0,3) ans = min(ans,solve(i));
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 1e5+9;
const double esp = 1e-6;
int a[maxn] , b[maxn] , c[maxn] ;
void solve(){
    int x , y , z ;
    scanf("%lld%lld%lld" , &x , &y , &z);
    rep(i , 1 , x){
        scanf("%lld" , &a[i]);
    }
    sort(a+1 , a+1+x);
    rep(i , 1 , y){
        scanf("%lld" , &b[i]);
    }
    sort(b+1 , b+1+y);
    rep(i , 1 , z){
        scanf("%lld" , &c[i]);
    }
    sort(c+1 , c+1+z);
    a[x+1] = 2e9+1;
    c[z+1] = 2e9+1;
    int sum = 2e18+9;
    rep(i , 1 , y){
        int l = lower_bound(a+1 , a+1+x , b[i]) - a;
        if(l > 1 && a[l] != b[i]){
            l--;
        }
        int r = lower_bound(c+1 , c+1+z , b[i]) - c;
        if(r > z){
            r--;
        }
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
    cout << sum << endl;

}

signed main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);cout.tie(NULL);
    int t ;
    scanf("%lld" , &t);
    while(t--)
        solve();
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 2e5+9;
const double esp = 1e-6;
int a[maxn] , b[maxn] , c[maxn] ;
int sum ;
void work(int *a , int *b , int *c , int x , int y , int z){
    rep(i , 1 , y){
        int l = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(l > 1 && a[l] > b[i]){
            l--;
        }
        int r = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(r > z && c[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
}

void solve(){
    int x , y , z ;
    scanf("%lld%lld%lld" , &x , &y , &z);
    rep(i , 1 , x){
        scanf("%lld" , &a[i]);
    }
    sort(a+1 , a+1+x);
    rep(i , 1 , y){
        scanf("%lld" , &b[i]);
    }
    sort(b+1 , b+1+y);
    rep(i , 1 , z){
        scanf("%lld" , &c[i]);
    }
    sort(c+1 , c+1+z);
    a[x+1] = 2e10+1;
    b[y+1] = 2e10+1;
    c[z+1] = 2e10+1;
    sum = 3e18+9;
    work(a , b , c , x , y , z);
    work(b , a , c , y , x , z);
    work(a , c , b , x , z , y);
    cout << sum << endl;
    /*rep(i , 1 , y){
        int l = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(l > 1 && a[l] > b[i]){
            l--;
        }
        int r = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(r > z && c[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
    rep(i , 1 , y){
        int l = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(l > 1 && c[l] > b[i]){
            l--;
        }
        int r = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(r > z && a[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - c[l])*(b[i] - c[l]) + (b[i] - a[r])*(b[i] - a[r]) + (c[l] - a[r])*(c[l] - a[r]));
    }*/
    //cout << sum << endl;
}

signed main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);cout.tie(NULL);
    int t ;
    scanf("%lld" , &t);
    while(t--)
        solve();
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 2e5+9;
const double esp = 1e-6;
int dep[maxn] , siz[maxn];
vector<int>g[maxn];
void dfs(int u , int pre){
    dep[u] = dep[pre] + 1 ;
    siz[u] = 1 ;
    for(auto &v : g[u]){
        if(v == pre) continue;
        dfs(v , u);
        siz[u] += siz[v] ;
    }
    dep[u] -= siz[u];
}
bool cmp(int a , int b){
    return a > b ;
}

void solve(){
    int n , k ;
    cin >> n >> k ;
    rep(i , 1 , n-1){
        int u , v ;
        cin >> u >> v ;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs(1 , 0);
    sort(dep+1 , dep+1+n , cmp);
    int ans = 0 ;
    rep(i , 1 , k){
        ans += dep[i];
    }
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    //int t ;
    //cin >> t ;
    //while(t--)
        solve();
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 2e5+9;
const double esp = 1e-6;
int a[maxn] , b[maxn] , c[maxn] ;
int sum ;
int get(int x,int y,int z)
{
    return (x-y)*(x-y)+(x-z)*(x-z)+(y-z)*(y-z);
}
void work(int *a , int *b , int *c , int x , int y , int z){
    rep(i,1,x)
    {
        int posx=lower_bound(b+1,b+1+y,a[i])-b;
        int posy=lower_bound(c+1,c+1+z,a[i])-c;
        if(posx>y||b[posx]>a[i])
        {
            if(posx>1)--posx;
        }
        if(posx<=y&&posy<=z)
        {
            int x=a[i];
            int y=b[posx];
            int z=c[posy];
            sum=min(sum,get(x,y,z));
        }
        posx=lower_bound(c+1,c+1+z,a[i])-c;
        posy=lower_bound(b+1,b+1+y,a[i])-b;
        if(posx>z||c[posx]>a[i])
        {
            if(posx>1) posx--;
        }
        if(posx<=z&posy<=y)
        {
            int x=a[i];
            int y=c[posx];
            int z=b[posy];
            sum=min(sum,get(x,y,z));
        }
    }
}

void solve(){
    int x , y , z ;
    scanf("%lld%lld%lld" , &x , &y , &z);
    rep(i , 1 , x){
        scanf("%lld" , &a[i]);
    }
    sort(a+1 , a+1+x);
    rep(i , 1 , y){
        scanf("%lld" , &b[i]);
    }
    sort(b+1 , b+1+y);
    rep(i , 1 , z){
        scanf("%lld" , &c[i]);
    }
    sort(c+1 , c+1+z);
    //a[x+1] = 2e10+1;
    //b[y+1] = 2e10+1;
    //c[z+1] = 2e10+1;
    sum = 3e18+9;
    work(a,b,c,x,y,z);
    work(c,a,b,z,x,y);
    work(b,a,c,y,x,z);
    cout << sum << endl;
    /*rep(i , 1 , y){
        int l = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(l > 1 && a[l] > b[i]){
            l--;
        }
        int r = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(r > z && c[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
    rep(i , 1 , y){
        int l = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(l > 1 && c[l] > b[i]){
            l--;
        }
        int r = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(r > z && a[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - c[l])*(b[i] - c[l]) + (b[i] - a[r])*(b[i] - a[r]) + (c[l] - a[r])*(c[l] - a[r]));
    }*/
    //cout << sum << endl;
}

signed main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);cout.tie(NULL);
    int t ;
    scanf("%lld" , &t);
    while(t--)
        solve();
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 2e5+9;
const double esp = 1e-6;
int a[maxn] , b[maxn] , c[maxn] ;
void solve(){
    int x , y , z ;
    scanf("%lld%lld%lld" , &x , &y , &z);
    rep(i , 1 , x){
        scanf("%lld" , &a[i]);
    }
    sort(a+1 , a+1+x);
    rep(i , 1 , y){
        scanf("%lld" , &b[i]);
    }
    sort(b+1 , b+1+y);
    rep(i , 1 , z){
        scanf("%lld" , &c[i]);
    }
    sort(c+1 , c+1+z);
    a[x+1] = 2e10+1;
    c[z+1] = 2e10+1;
    int sum = 3e18+9;
    rep(i , 1 , y){
        int l = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(l > 1 && a[l] > b[i]){
            l--;
        }
        int r = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(r > z && c[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
    rep(i , 1 , y){
        int l = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(l > 1 && c[l] > b[i]){
            l--;
        }
        int r = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(r > z && a[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - c[l])*(b[i] - c[l]) + (b[i] - a[r])*(b[i] - a[r]) + (c[l] - a[r])*(c[l] - a[r]));
    }
    cout << sum << endl;
}

signed main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);cout.tie(NULL);
    int t ;
    scanf("%lld" , &t);
    while(t--)
        solve();
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 2e5+9;
const double esp = 1e-6;
int a[maxn] , b[maxn] , c[maxn] ;
int sum ;
void work(int *a , int *b , int *c , int x , int y , int z){
    //cout << b[1] << endl;
    rep(i , 1 , y){
        int l = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(l > 1 && a[l] > b[i]){
            l--;
        }
        int r = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(r > z && c[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
    rep(i , 1 , y){
        int l = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(l > 1 && c[l] > b[i]){
            l--;
        }
        int r = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(r > z && a[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - c[l])*(b[i] - c[l]) + (b[i] - a[r])*(b[i] - a[r]) + (c[l] - a[r])*(c[l] - a[r]));
    }
}

void solve(){
    int x , y , z ;
    scanf("%lld%lld%lld" , &x , &y , &z);
    rep(i , 1 , x){
        scanf("%lld" , &a[i]);
    }
    sort(a+1 , a+1+x);
    rep(i , 1 , y){
        scanf("%lld" , &b[i]);
    }
    sort(b+1 , b+1+y);
    rep(i , 1 , z){
        scanf("%lld" , &c[i]);
    }
    sort(c+1 , c+1+z);
    a[x+1] = 2e10+1;
    b[y+1] = 2e10+1;
    c[z+1] = 2e10+1;
    sum = 3e18;
    work(a , b , c , x , y , z);
    work(b , a , c , y , x , z);
    work(a , c , b , x , z , y);
    cout << sum << endl;
    /*rep(i , 1 , y){
        int l = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(l > 1 && a[l] > b[i]){
            l--;
        }
        int r = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(r > z && c[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
    rep(i , 1 , y){
        int l = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(l > 1 && c[l] > b[i]){
            l--;
        }
        int r = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(r > z && a[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - c[l])*(b[i] - c[l]) + (b[i] - a[r])*(b[i] - a[r]) + (c[l] - a[r])*(c[l] - a[r]));
    }*/
    //cout << sum << endl;
}

signed main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);cout.tie(NULL);
    int t ;
    scanf("%lld" , &t);
    while(t--)
        solve();
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 1e5+9;
const double esp = 1e-6;
int a[maxn] , b[maxn] , c[maxn] ;
void solve(){
    int x , y , z ;
    scanf("%lld%lld%lld" , &x , &y , &z);
    rep(i , 1 , x){
        scanf("%lld" , &a[i]);
    }
    sort(a+1 , a+1+x);
    rep(i , 1 , y){
        scanf("%lld" , &b[i]);
    }
    sort(b+1 , b+1+y);
    rep(i , 1 , z){
        scanf("%lld" , &c[i]);
    }
    sort(c+1 , c+1+z);
    a[x+1] = 1e9+1;
    c[z+1] = 1e9+1;
    int sum = 1e19;
    rep(i , 1 , y){
        int l = lower_bound(a+1 , a+1+x , b[i]) - a;
        if(l > 1 && a[l] != b[i]){
            l--;
        }
        int r = lower_bound(c+1 , c+1+z , b[i]) - c;
        if(r > z){
            r--;
        }
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
    cout << sum << endl;

}

signed main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);cout.tie(NULL);
    int t ;
    scanf("%lld" , &t);
    while(t--)
        solve();
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 2e5+9;
const double esp = 1e-6;
int ans ;
int cal(int x , int y , int z){
    return (x-y)*(x-y) + (y-z)*(y-z) + (z-x)*(z-x);
}

void work(vector<int> a , vector<int> b , vector<int> c){
    for(auto x : a){
        auto y = lower_bound(all(b) , x);
        auto z = upper_bound(all(c) , x);
        if(y == b.end() || z == c.begin()) continue;
        z--; ans = min(ans , cal(x , *y , *z));
    }
}

void solve(){
    int nr , ng , nb ;
    ans = 2e18;
    cin >> nr >> ng >> nb ;
    vector<int> r(nr) , g(ng) , b(nb);
    rep(i , 0 , nr-1) cin >> r[i];
    rep(i , 0 , ng-1) cin >> g[i];
    rep(i , 0 , nb-1) cin >> b[i];
    sort(all(r));sort(all(g));sort(all(b));
    work(r , g , b);work(r , b , g);
    work(g , r , b);work(g , b , r);
    work(b , r , g);work(b , g , r);
    cout << ans << endl;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    //cin.tie(NULL);cout.tie(NULL);
    int t ;
    cin >> t ;
    while(t--)
        solve();
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 1e5+9;
const double esp = 1e-6;
int a[maxn] , b[maxn] , c[maxn] ;
void solve(){
    int x , y , z ;
    scanf("%lld%lld%lld" , &x , &y , &z);
    rep(i , 1 , x){
        scanf("%lld" , &a[i]);
    }
    sort(a+1 , a+1+x);
    rep(i , 1 , y){
        scanf("%lld" , &b[i]);
    }
    sort(b+1 , b+1+y);
    rep(i , 1 , z){
        scanf("%lld" , &c[i]);
    }
    sort(c+1 , c+1+z);
    a[x+1] = 2e10+1;
    c[z+1] = 2e10+1;
    int sum = 3e18+9;
    rep(i , 1 , y){
        int l = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(l > 1 && a[l] > b[i]){
            l--;
        }
        int r = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(r > z){
            r--;
        }
        //cout << l << " " << i << " " << r << endl;
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
    cout << sum << endl;

}

signed main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);cout.tie(NULL);
    int t ;
    scanf("%lld" , &t);
    while(t--)
        solve();
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 2e5+9;
const double esp = 1e-6;
int a[maxn] , b[maxn] , c[maxn] ;
void solve(){
    int x , y , z ;
    scanf("%lld%lld%lld" , &x , &y , &z);
    rep(i , 1 , x){
        scanf("%lld" , &a[i]);
    }
    sort(a+1 , a+1+x);
    rep(i , 1 , y){
        scanf("%lld" , &b[i]);
    }
    sort(b+1 , b+1+y);
    rep(i , 1 , z){
        scanf("%lld" , &c[i]);
    }
    sort(c+1 , c+1+z);
    a[x+1] = 2e10+1;
    c[z+1] = 2e10+1;
    int sum = 3e18+9;
    rep(i , 1 , y){
        int l = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(l > 1 && a[l] > b[i]){
            l--;
        }
        int r = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(r > z){
            r--;
        }
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
    cout << sum << endl;
}

signed main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);cout.tie(NULL);
    int t ;
    scanf("%lld" , &t);
    while(t--)
        solve();
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 1e5+9;
const double esp = 1e-6;
int a[maxn] , b[maxn] , c[maxn] ;
void solve(){
    int x , y , z ;
    scanf("%lld%lld%lld" , &x , &y , &z);
    rep(i , 1 , x){
        scanf("%lld" , &a[i]);
    }
    sort(a+1 , a+1+x);
    rep(i , 1 , y){
        scanf("%lld" , &b[i]);
    }
    sort(b+1 , b+1+y);
    rep(i , 1 , z){
        scanf("%lld" , &c[i]);
    }
    sort(c+1 , c+1+z);
    a[x+1] = 1e9;
    c[z+1] = 1e9;
    int sum = 1e19;
    rep(i , 1 , y){
        int l = lower_bound(a+1 , a+1+x , b[i]) - a;
        if(l > 1){
            l--;
        }
        int r = lower_bound(c+1 , c+1+z , b[i]) - c;
        if(r > z){
            r--;
        }
        //cout << a[l] << " " << b[i] << " " << c[r] << endl;
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
    cout << sum << endl;

}

signed main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);cout.tie(NULL);
    int t ;
    scanf("%lld" , &t);
    while(t--)
        solve();
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 1e5+9;
const double esp = 1e-6;
int a[maxn] , b[maxn] , c[maxn] ;
void solve(){
    int x , y , z ;
    scanf("%lld%lld%lld" , &x , &y , &z);
    rep(i , 1 , x){
        scanf("%lld" , &a[i]);
    }
    sort(a+1 , a+1+x);
    rep(i , 1 , y){
        scanf("%lld" , &b[i]);
    }
    sort(b+1 , b+1+y);
    rep(i , 1 , z){
        scanf("%lld" , &c[i]);
    }
    sort(c+1 , c+1+z);
    a[x+1] = 1e9+1;
    c[z+1] = 1e9+1;
    int sum = 2e18;
    rep(i , 1 , y){
        int l = lower_bound(a+1 , a+1+x , b[i]) - a;
        if(l > 1 && a[l] != b[i]){
            l--;
        }
        int r = lower_bound(c+1 , c+1+z , b[i]) - c;
        if(r > z){
            r--;
        }
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
    cout << sum << endl;

}

signed main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);cout.tie(NULL);
    int t ;
    scanf("%lld" , &t);
    while(t--)
        solve();
}


#include<bits/stdc++.h>
#include <string.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
//int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
#define endl "\n"
const int N = 1e6+9;
const int maxn = 2e5+9;
const double esp = 1e-6;
int a[maxn] , b[maxn] , c[maxn] ;
void solve(){
    int x , y , z ;
    scanf("%lld%lld%lld" , &x , &y , &z);
    rep(i , 1 , x){
        scanf("%lld" , &a[i]);
    }
    sort(a+1 , a+1+x);
    rep(i , 1 , y){
        scanf("%lld" , &b[i]);
    }
    sort(b+1 , b+1+y);
    rep(i , 1 , z){
        scanf("%lld" , &c[i]);
    }
    sort(c+1 , c+1+z);
    a[x+1] = 2e10+1;
    c[z+1] = 2e10+1;
    int sum = 3e18+9;
    rep(i , 1 , y){
        int l = lower_bound(a+1 , a+2+x , b[i]) - a;
        if(l > 1 && a[l] > b[i]){
            l--;
        }
        int r = lower_bound(c+1 , c+2+z , b[i]) - c;
        if(r > z && c[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - a[l])*(b[i] - a[l]) + (b[i] - c[r])*(b[i] - c[r]) + (a[l] - c[r])*(a[l] - c[r]));
    }
    rep(i , 1 , y){
        int l = lower_bound(c+1 , c+2+x , b[i]) - c;
        if(l > 1 && c[l] > b[i]){
            l--;
        }
        int r = lower_bound(a+1 , a+2+z , b[i]) - a;
        if(r > z && a[r] > b[i]){
            r--;
        }
         sum = min(sum , (b[i] - c[l])*(b[i] - c[l]) + (b[i] - a[r])*(b[i] - a[r]) + (c[l] - a[r])*(c[l] - a[r]));
    }
    cout << sum << endl;
}

signed main()
{
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);cout.tie(NULL);
    int t ;
    scanf("%lld" , &t);
    while(t--)
        solve();
}


#include <iostream>
#include <algorithm>
//#include <string>
#include <vector>
//#include <fstream>
//#include <map>
//#include <functional>
typedef long long ll;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	const long long mdl = 1000000007;
	long long q;
	cin >> q;

	//q = 1;
	for (long long qi = 0; qi < q; qi++)
	{
		long long nr, ng, nb;
		cin >> nr >> ng >> nb;

		long long* r = new long long[nr];
		long long* g = new long long[ng];
		long long* b = new long long[nb];

		for (long i = 0; i < nr; i++)
		{
			cin >> r[i];
		}

		for (long i = 0; i < ng; i++)
		{
			cin >> g[i];
		}

		for (long i = 0; i < nb; i++)
		{
			cin >> b[i];
		}

		sort(r, r + nr);
		sort(g, g + ng);
		sort(b, b + nb);

		ll best = LLINF;

		ll ix[3];
		ll n[3];
		long long* c[3];
		ix[0] = 0; ix[1] = 0; ix[2] = 0;
		n[0] = nr; n[1] = ng; n[2] = nb;
		c[0] = r; c[1] = g; c[2] = b;

		while (ix[0] < n[0] && ix[1] < n[1] && ix[2] < n[2])
		{
			ll cr = 0;
			for (ll ci = 0; ci < 3; ci++)
				cr +=
					(c[ci][ix[ci]] - c[(ci + 1) % 3][ix[(ci + 1) % 3]])*
					(c[ci][ix[ci]] - c[(ci + 1) % 3][ix[(ci + 1) % 3]]);

			if (cr < best)
				best = cr;

			ll mi = 0;
			ll mx = 0;
			ll ag = 0;
			for (ll ci = 1; ci < 3; ci++)
			{
				if (c[ci][ix[ci]] < c[mi][ix[mi]])
					mi = ci;
				if (c[ci][ix[ci]] > c[mx][ix[mx]])
					mx = ci;
			}
			if (mi == mx)
				mi = (mx + 2) % 3;
			ag = (mi | mx) ^ 3;
			if (ix[ag] < n[ag] && ((ix[mi] < n[mi] - 1 &&
				 c[mi][ix[mi] + 1] > c[ag][ix[ag] + 1]) || ix[mi] == n[mi] - 1)
				&& c[mx][ix[mx]] > c[ag][ix[ag] + 1])
				ix[ag]++;
			else
				ix[mi]++;
		}

		cout << best << endl;
	}
}


#include <iostream>
#include <algorithm>
//#include <string>
#include <vector>
//#include <fstream>
//#include <map>
//#include <functional>
typedef long long ll;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

using namespace std;

int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	const long long mdl = 1000000007;
	long long q;
	cin >> q;

	//q = 1;
	for (long long qi = 0; qi < q; qi++)
	{
		long long nr, ng, nb;
		cin >> nr >> ng >> nb;

		long long* r = new long long[nr];
		long long* g = new long long[ng];
		long long* b = new long long[nb];

		for (long i = 0; i < nr; i++)
		{
			cin >> r[i];
		}

		for (long i = 0; i < ng; i++)
		{
			cin >> g[i];
		}

		for (long i = 0; i < nb; i++)
		{
			cin >> b[i];
		}

		sort(r, r + nr);
		sort(g, g + ng);
		sort(b, b + nb);

		ll best = LLINF;

		ll ix[3];
		ll n[3];
		long long* c[3];
		ix[0] = 0; ix[1] = 0; ix[2] = 0;
		n[0] = nr; n[1] = ng; n[2] = nb;
		c[0] = r; c[1] = g; c[2] = b;

		while (ix[0] < n[0] && ix[1] < n[1] && ix[2] < n[2])
		{
			ll cr = 0;
			for (ll ci = 0; ci < 3; ci++)
				cr +=
					(c[ci][ix[ci]] - c[(ci + 1) % 3][ix[(ci + 1) % 3]])*
					(c[ci][ix[ci]] - c[(ci + 1) % 3][ix[(ci + 1) % 3]]);

			if (cr < best)
				best = cr;

			ll mi = 0;
			ll mx = 0;
			ll ag = 0;
			for (ll ci = 1; ci < 3; ci++)
			{
				if (c[ci][ix[ci]] < c[mi][ix[mi]])
					mi = ci;
				if (c[ci][ix[ci]] > c[mx][ix[mx]])
					mx = ci;
			}
			if (mi == mx)
				mi = (mx + 2) % 3;
			ag = (mi | mx) ^ 3;
			if (ix[ag] < n[ag] - 1 && ((ix[mi] < n[mi] - 1 &&
				 c[mi][ix[mi] + 1] > c[ag][ix[ag] + 1]) || ix[mi] == n[mi] - 1)
				&& c[mx][ix[mx]] > c[ag][ix[ag] + 1])
				ix[ag]++;
			else
				ix[mi]++;
		}

		cout << best << endl;
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

uint64_t diff(uint64_t first, uint64_t second)
{
	if (first < second)
	{
		return second - first;
	}

	return first - second;
}

uint64_t triple(uint64_t first, uint64_t second, uint64_t third)
{
	uint64_t res = 0;
	uint64_t a = diff(first, second);
	uint64_t b = diff(first, third);
	uint64_t c = diff(third, second);
	res = a * a + b * b + c * c;
	return res;
}

uint64_t solve(vector<uint64_t> & small, vector<uint64_t> & mid, vector<uint64_t> & big)
{
	uint64_t bigIndex = 0;
	uint64_t midIndex = 0;
	uint64_t bestResult = UINT64_MAX;
	for (uint64_t i = 0; i < small.size(); i++)
	{
		uint64_t currentSmall = small[i];
		while (big[bigIndex] < currentSmall && bigIndex < big.size() - 1)
		{
			bigIndex++;
		}

		if (big[bigIndex] < currentSmall)
		{
			break;
		}

		while (mid[midIndex] < currentSmall && midIndex < mid.size() - 1)
		{
			midIndex++;
		}

		if (mid[midIndex] < currentSmall)
		{
			break;
		}

		if (mid[midIndex] > big[bigIndex])
		{
			continue;
		}

		uint64_t tempBest = UINT64_MAX;
		while (midIndex < mid.size() && triple(currentSmall, mid[midIndex], big[bigIndex]) <= tempBest)
		{
			tempBest = triple(currentSmall, mid[midIndex], big[bigIndex]);
			midIndex++;
		}

		if(tempBest < bestResult)
		{
			bestResult = tempBest;
		}
		if (midIndex != 0)
		{
			midIndex--;
		}

	}

	return bestResult;
}

int main()
{
	int T;
	cin >> T;
	for (int t = 0; t < T; t++)
	{
		int r, g, b;
		cin >> r >> g >> b;

		vector<uint64_t> Nr(r);
		vector<uint64_t> Ng(g);
		vector<uint64_t> Nb(b);

		for (int i = 0; i < r; i++)
		{
			cin >> Nr[i];
		}

		for (int i = 0; i < g; i++)
		{
			cin >> Ng[i];
		}

		for (int i = 0; i < b; i++)
		{
			cin >> Nb[i];
		}

		sort(Nr.begin(), Nr.end());
		sort(Ng.begin(), Ng.end());
		sort(Nb.begin(), Nb.end());

		uint64_t result = UINT64_MAX;
		uint64_t temp = solve(Nr, Ng, Nb);
		if (temp < result)
		{
			result = temp;
		}

		temp = solve(Nr, Nb, Ng);
		if (temp < result)
		{
			result = temp;
		}

		temp = solve(Nb, Nr, Ng);
		if (temp < result)
		{
			result = temp;
		}

		temp = solve(Nb, Ng, Nr);
		if (temp < result)
		{
			result = temp;
		}

		temp = solve(Ng, Nb, Nr);
		if (temp < result)
		{
			result = temp;
		}

		temp = solve(Ng, Nr, Nb);
		if (temp < result)
		{
			result = temp;
		}

		cout << result << endl;
	}
	return 0;
}