#include <bits/stdc++.h>

using namespace std;
int q;
vector <pair<int, int> > a;
vector <pair<int, int> > b;
int id[300001];
int idb[300001];
int re[300001];
int main()
{
    ios_base::sync_with_stdio(false);
    cin >> q;
    for (int i=1; i<=q; i++) {
        int n;
        cin >> n;
        a.push_back({0, 0});
        b.push_back({0, 0});
        for (int i=1; i<=n; i++) {
            int x;
            cin >> x;
            a.push_back({x, i});
        }
        for (int i=1; i<=n; i++) {
            int x;
            cin >> x;
            b.push_back({x, i});
        }
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        for (int i=1; i<=n; i++) {
            id[a[i].second]=i;
            idb[b[i].second]=i;
        }
        for (int i=1; i<=n; i++) {
            re[idb[i]]=i;
        }
        bool ans=true;
        for (int i=1; i<=n; i++) {
            if (a[i].first!=b[i].first) {
                ans=false;
            }
            if (i>1) {
                if (id[i]>id[i-1] && re[id[i]]<re[id[i-1]]) {
                    ans=false;
                }
            }
        }
        if (ans) {
            cout << "YES";
        } else {
            cout << "NO";
        }
        cout << "\n";
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9 + 7;

typedef long long ll;
#define cerr cout
#define sz(a) (int)((a).size())
#define all(x) (x).begin(), (x).end()
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cerr << " " << to_string(H); debug_out(T...);}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
inline int add(int a, int b){a += b; if(a >= MOD)a -= MOD; return a;}
inline int sub(int a, int b){a -= b; if(a < 0)a += MOD; return a;}
inline int mul(int a, int b){return (int)((long long) a * b %MOD);}
inline int binpow(int a, int b){int res = 1; while(b > 0){ if(b & 1)res = mul(res, a); a = mul(a, a);b /= 2;} return res;}
inline int inv(int a){return binpow(a, MOD - 2);}
int gcd(int a, int b, int &x, int &y){if(a == 0){x = 0, y = 1; return b;} int x1, y1;  int d = gcd(b%a, a, x1, y1); x = y1 - (b/a) * x1; y = x1; return d;}

struct Segment{
	vector<int> t;
	int n;
	Segment(int n){
		this->n = n;
		t = vector<int> (4*n + 5, n);
	}
	void update(int node, int start, int end, int id, int val){
		if(start > id || end <  id)return ;
		if(start == end){
			t[node] = val; return ;
		}
		int mid = (start + end)/2;
		update(2*node, start, mid, id, val);
		update(2*node + 1, mid + 1, end, id, val);
		t[node] = min(t[2*node], t[2*node + 1]);
	}
	int query(int node, int start, int end, int l, int r){
		if(start > r || end < l)return n;
		if(start >= l && end <= r)return t[node];
		int mid = (start + end)/2;
		return min(query(2*node, start, mid, l, r), query(2*node + 1, mid + 1, end, l, r));
	}
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

	int t; cin>>t;
	while(t--){
		int n; cin>>n;
		vector<int> a(n), b(n);
		vector<int> pos[n], curr(n);
		Segment seg(n);
		for(int i = 0;i<n;++i){
			cin>>a[i]; a[i]--;
			pos[a[i]].push_back(i);
			seg.update(1, 0, n - 1, i, a[i]);
		}
		for(int i = 0;i<n;++i){
			cin>>b[i]; b[i]--;
		}
		// pr(a);
		// pr(b); pr("------");
		bool check = true;
		for(int i = 0, j = 0; i<n;){
			if(a[j] == n){
				j++; continue;
			}

			int id = curr[b[i]];
			if(id == sz(pos[b[i]])){
				check = false; break;
			}

			if(pos[b[i]][id] == j){
				j++; curr[b[i]]++;
			}else{
				int mn = seg.query(1, 0, n -1, j, pos[b[i]][id]);

				// pr(mn);
				if(mn != b[i]){
					check = false; break;
				}
				seg.update(1, 0, n - 1, pos[b[i]][id], n);  a[pos[b[i]][id]] = n;
				curr[b[i]]++;
			}
			i++;
			// pr(a);
		}
		check? cout<<"YES\n" : cout<<"NO\n";
	}

    return 0;
}


//Code of C#oder Born_Clear
#include <bits/stdc++.h>
using namespace std;
#define loop(n) for(int i=0;i<n;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define ll long long
#define s(x) scanf("%d", &x)
#define sd(x) scanf("%lf", &x)
#define sl(x) scanf("%lld", &x)
#define mod 1000000007
#define fi first
#define se second
#define p(x) printf("%d",x)
#define pl(x) printf("%lld", x)
#define pd(x) printf("%lf", x)
#define pn() printf("\n")
#define vi(v) vector<int>v
#define vl(v) vector<long long int>v
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define len(s) s.length()
void input(int n,int a[]){loop(n)cin>>a[i];}
ll bin_expo(ll A, ll B, ll M){ll res = 1LL;while (B > 0){if(B % 2 == 1){res = (res * A) % M;}A = (A * A) % M; B = B / 2;}return res;}
int seg[2000000];
int kk=0;
int n;
int a[300005];

void build(int node,int l,int r)
{
	kk=max(kk,node);
	if(l==r)
	{
		seg[node]=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
void update(int node,int l,int r,int ind)
{
	if(l==r)
	{
		seg[node]=n+1;
		return;
	}
	int mid=(l+r)>>1;
	if(ind<=mid)
		update(2*node,l,mid,ind);
	else
		update(2*node+1,mid+1,r,ind);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
int query(int node,int l,int r,int ql,int qr)
{
	if(l>r || ql>r || qr<l)
		return n+1;
	if(ql<=l && qr>=r)
		return seg[node];
	int mid=(l+r)>>1;
	return min(query(2*node,l,mid,ql,qr),query(2*node+1,mid+1,r,ql,qr));
}
vector<int>pos[300005];
int fre[300005][2];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			pos[a[i]].pb(i);
			fre[a[i]][0]++;
		}
		build(1,0,n-1);
		int fl=0;
		for(int i=0;i<n;i++)
		{
			int x;
			cin>>x;
			if(fl==1)
				continue;
			if(pos[x].size()==fre[x][1])
			{
				fl=1;
				cout<<"NO\n";
				break;
			}
			int ind=pos[x][fre[x][1]++];
			int z=query(1,0,n-1,0,ind-1);
			if(z<x)
			{
				fl=1;
				cout<<"NO\n";
			}
			else
			{
				update(1,0,n-1,ind);
			}
		}
		if(fl==0)
			cout<<"YES\n";
		for(int i=0;i<n+2;i++){
			pos[i].clear();
			fre[i][0]=0;
			fre[i][1]=0;
		}
		for(int i=0;i<kk+2;i++)
			seg[i]=0;
		kk=0;
	}
	return 0;
}


//Code of C#oder Born_Clear
#include <bits/stdc++.h>
using namespace std;
#define loop(n) for(int i=0;i<n;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define ll long long
#define s(x) scanf("%d", &x)
#define sd(x) scanf("%lf", &x)
#define sl(x) scanf("%lld", &x)
#define mod 1000000007
#define fi first
#define se second
#define p(x) printf("%d",x)
#define pl(x) printf("%lld", x)
#define pd(x) printf("%lf", x)
#define pn() printf("\n")
#define vi(v) vector<int>v
#define vl(v) vector<long long int>v
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define len(s) s.length()
void input(int n,int a[]){loop(n)cin>>a[i];}
ll bin_expo(ll A, ll B, ll M){ll res = 1LL;while (B > 0){if(B % 2 == 1){res = (res * A) % M;}A = (A * A) % M; B = B / 2;}return res;}
int seg[2000000];
int kk=0;
int n;
int a[300005];

void build(int node,int l,int r)
{
	kk=max(kk,node);
	if(l==r)
	{
		seg[node]=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
void update(int node,int l,int r,int ind)
{
	if(l==r)
	{
		seg[node]=INT_MAX;
		return;
	}
	int mid=(l+r)>>1;
	if(ind<=mid)
		update(2*node,l,mid,ind);
	else
		update(2*node+1,mid+1,r,ind);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
int query(int node,int l,int r,int ql,int qr)
{
	if(l>r || ql>r || qr<l)
		return INT_MAX;
	if(ql<=l && qr>=r)
		return seg[node];
	int mid=(l+r)>>1;
	return min(query(2*node,l,mid,ql,qr),query(2*node+1,mid+1,r,ql,qr));
}
vector<int>pos[300005];
int fre[300005][2];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			pos[a[i]].pb(i);
			fre[a[i]][0]++;
		}
		build(1,0,n-1);
		int fl=0;
		for(int i=0;i<n;i++)
		{
			int x;
			cin>>x;
			if(fl==1)
				continue;
			if(pos[x].size()==fre[x][1])
			{
				fl=1;
				cout<<"NO\n";
				break;
			}
			int ind=pos[x][fre[x][1]++];
			int z=query(1,0,n-1,0,ind-1);
			if(z<x)
			{
				fl=1;
				cout<<"NO\n";
			}
			else
			{
				update(1,0,n-1,ind);
			}
		}
		if(fl==0)
			cout<<"YES\n";
		for(int i=0;i<n+2;i++){
			pos[i].clear();
			fre[i][0]=0;
			fre[i][1]=0;
		}
		for(int i=0;i<kk+2;i++)
			seg[i]=0;
		kk=0;
	}
	return 0;
}


//Code of C#oder Born_Clear
#include <bits/stdc++.h>
using namespace std;
#define loop(n) for(int i=0;i<n;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define ll long long
#define s(x) scanf("%d", &x)
#define sd(x) scanf("%lf", &x)
#define sl(x) scanf("%lld", &x)
#define mod 1000000007
#define fi first
#define se second
#define p(x) printf("%d",x)
#define pl(x) printf("%lld", x)
#define pd(x) printf("%lf", x)
#define pn() printf("\n")
#define vi(v) vector<int>v
#define vl(v) vector<long long int>v
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define len(s) s.length()
void input(int n,int a[]){loop(n)cin>>a[i];}
ll bin_expo(ll A, ll B, ll M){ll res = 1LL;while (B > 0){if(B % 2 == 1){res = (res * A) % M;}A = (A * A) % M; B = B / 2;}return res;}
int seg[2000000];
int kk=0;
int n;
int a[300005];

void build(int node,int l,int r)
{
	kk=max(kk,node);
	if(l==r)
	{
		seg[node]=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
void update(int node,int l,int r,int ind)
{
	if(l==r)
	{
		seg[node]=INT_MAX;
		return;
	}
	int mid=(l+r)>>1;
	if(ind<=mid)
		update(2*node,l,mid,ind);
	else
		update(2*node+1,mid+1,r,ind);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
int query(int node,int l,int r,int ql,int qr)
{
	if(l>r || ql>r || qr<l)
		return INT_MAX;
	if(ql<=l && qr>=r)
		return seg[node];
	int mid=(l+r)>>1;
	return min(query(2*node,l,mid,ql,qr),query(2*node+1,mid+1,r,ql,qr));
}
vector<int>pos[300005];
int fre[300005][2];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			pos[a[i]].pb(i);
			fre[a[i]][0]++;
		}
		build(1,0,n-1);
		int fl=0;
		for(int i=0;i<n;i++)
		{
			int x;
			cin>>x;
			if(fl==1)
				continue;
			if(pos[x].size()==fre[x][1])
			{
				fl=1;
				cout<<"NO\n";
				break;
			}
			int ind=pos[x][fre[x][1]++];
			int z=INT_MAX;
			if(ind-1>=0)
				z=query(1,0,n-1,0,ind-1);
			if(z<x)
			{
				fl=1;
				cout<<"NO\n";
			}
			else
			{
				update(1,0,n-1,ind);
			}
		}
		if(fl==0)
			cout<<"YES\n";
		for(int i=0;i<n+2;i++){
			pos[i].clear();
			fre[i][0]=0;
			fre[i][1]=0;
		}
		for(int i=0;i<kk+2;i++)
			seg[i]=0;
		kk=0;
	}
	return 0;
}


//Code of C#oder Born_Clear
#include <bits/stdc++.h>
using namespace std;
#define loop(n) for(int i=0;i<n;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define ll long long
#define s(x) scanf("%d", &x)
#define sd(x) scanf("%lf", &x)
#define sl(x) scanf("%lld", &x)
#define mod 1000000007
#define fi first
#define se second
#define p(x) printf("%d",x)
#define pl(x) printf("%lld", x)
#define pd(x) printf("%lf", x)
#define pn() printf("\n")
#define vi(v) vector<int>v
#define vl(v) vector<long long int>v
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define len(s) s.length()
void input(int n,int a[]){loop(n)cin>>a[i];}
ll bin_expo(ll A, ll B, ll M){ll res = 1LL;while (B > 0){if(B % 2 == 1){res = (res * A) % M;}A = (A * A) % M; B = B / 2;}return res;}
int seg[2000000];
int kk=0;
int n;
int a[300005];

void build(int node,int l,int r)
{
	kk=max(kk,node);
	if(l==r)
	{
		seg[node]=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
void update(int node,int l,int r,int ind)
{
	if(l==r)
	{
		seg[node]=n+1;
		return;
	}
	int mid=(l+r)>>1;
	if(ind<=mid)
		update(2*node,l,mid,ind);
	else
		update(2*node+1,mid+1,r,ind);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
int query(int node,int l,int r,int ql,int qr)
{
	if(l>r || ql>r || qr<l)
		return n+1;
	if(ql<=l && qr>=r)
		return seg[node];
	int mid=(l+r)>>1;
	return min(query(2*node,l,mid,ql,qr),query(2*node+1,mid+1,r,ql,qr));
}
vector<int>pos[300005];
int fre[300005][2];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			pos[a[i]].pb(i);
			fre[a[i]][0]++;
		}
		build(1,0,n-1);
		int fl=0;
		for(int i=0;i<n;i++)
		{
			int x;
			cin>>x;
			if(fl==1)
				continue;
			if(pos[x].size()==fre[x][1])
			{
				fl=1;
				cout<<"NO\n";
				break;
			}
			int ind=pos[x][fre[x][1]++];
			int z=query(1,0,n-1,0,ind-1);
			if(z<x)
			{
				fl=1;
				cout<<"NO\n";
			}
			else
			{
				update(1,0,n-1,ind);
			}
		}
		if(fl==0)
			cout<<"YES\n";
		for(int i=0;i<n+2;i++){
			pos[i].clear();
			fre[i][0]=0;
			fre[i][1]=0;
		}
	}
	return 0;
}


//Code of C#oder Born_Clear
#include <bits/stdc++.h>
using namespace std;
#define loop(n) for(int i=0;i<n;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define ll long long
#define s(x) scanf("%d", &x)
#define sd(x) scanf("%lf", &x)
#define sl(x) scanf("%lld", &x)
#define mod 1000000007
#define fi first
#define se second
#define p(x) printf("%d",x)
#define pl(x) printf("%lld", x)
#define pd(x) printf("%lf", x)
#define pn() printf("\n")
#define vi(v) vector<int>v
#define vl(v) vector<long long int>v
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define len(s) s.length()
void input(int n,int a[]){loop(n)cin>>a[i];}
ll bin_expo(ll A, ll B, ll M){ll res = 1LL;while (B > 0){if(B % 2 == 1){res = (res * A) % M;}A = (A * A) % M; B = B / 2;}return res;}
int seg[2000000];
int kk=0;
int n;
int a[300005];

void build(int node,int l,int r)
{
	kk=max(kk,node);
	if(l==r)
	{
		seg[node]=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
void update(int node,int l,int r,int ind)
{
	if(l==r)
	{
		seg[node]=n+1;
		return;
	}
	int mid=(l+r)>>1;
	if(ind<=mid)
		update(2*node,l,mid,ind);
	else
		update(2*node+1,mid+1,r,ind);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
int query(int node,int l,int r,int ql,int qr)
{
	if(l>r || ql>r || qr<l)
		return n+1;
	if(ql<=l && qr>=r)
		return seg[node];
	int mid=(l+r)>>1;
	return min(query(2*node,l,mid,ql,qr),query(2*node+1,mid+1,r,ql,qr));
}
vector<int>pos[300005];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		int fre[n+5][2];
		memset(fre,0,sizeof(fre));
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			pos[a[i]].pb(i);
			fre[a[i]][0]++;
		}
		build(1,0,n-1);
		int fl=0;
		for(int i=0;i<n;i++)
		{
			int x;
			cin>>x;
			if(fl==1)
				continue;
			if(pos[x].size()==fre[x][1])
			{
				fl=1;
				cout<<"NO\n";
				break;
			}
			int ind=pos[x][fre[x][1]++];
			int z=query(1,0,n-1,0,ind-1);
			if(z<x)
			{
				fl=1;
				cout<<"NO\n";
			}
			else
			{
				update(1,0,n-1,ind);
			}
		}
		if(fl==0)
			cout<<"YES\n";
		for(int i=0;i<n+2;i++)
			pos[i].clear();
	}
	return 0;
}


//Code of C#oder Born_Clear
#include <bits/stdc++.h>
using namespace std;
#define loop(n) for(int i=0;i<n;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define ll long long
#define s(x) scanf("%d", &x)
#define sd(x) scanf("%lf", &x)
#define sl(x) scanf("%lld", &x)
#define mod 1000000007
#define fi first
#define se second
#define p(x) printf("%d",x)
#define pl(x) printf("%lld", x)
#define pd(x) printf("%lf", x)
#define pn() printf("\n")
#define vi(v) vector<int>v
#define vl(v) vector<long long int>v
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define len(s) s.length()
void input(int n,int a[]){loop(n)cin>>a[i];}
ll bin_expo(ll A, ll B, ll M){ll res = 1LL;while (B > 0){if(B % 2 == 1){res = (res * A) % M;}A = (A * A) % M; B = B / 2;}return res;}
int seg[2000000];
int kk=0;
int n;
int a[300005];

void build(int node,int l,int r)
{
	kk=max(kk,node);
	if(l==r)
	{
		seg[node]=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
void update(int node,int l,int r,int ind)
{
	if(l==r)
	{
		seg[node]=n+1;
		return;
	}
	int mid=(l+r)>>1;
	if(ind<=mid)
		update(2*node,l,mid,ind);
	else
		update(2*node+1,mid+1,r,ind);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
int query(int node,int l,int r,int ql,int qr)
{
	if(l>r || ql>r || qr<l)
		return n+1;
	if(ql<=l && qr>=r)
		return seg[node];
	int mid=(l+r)>>1;
	return min(query(2*node,l,mid,ql,qr),query(2*node+1,mid+1,r,ql,qr));
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		int fre[n+5][2];
		memset(fre,0,sizeof(fre));
		vector<int>pos[n+5];
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			pos[a[i]].pb(i);
			fre[a[i]][0]++;
		}
		build(1,0,n-1);
		int fl=0;
		for(int i=0;i<n;i++)
		{
			int x;
			cin>>x;
			if(pos[x].size()==fre[x][1])
			{
				fl=1;
				cout<<"NO\n";
				break;
			}
			int ind=pos[x][fre[x][1]++];
			int z=query(1,0,n-1,0,ind-1);
			if(z<x)
			{
				fl=1;
				cout<<"NO\n";
				break;
			}
			else
			{
				update(1,0,n-1,ind);
			}
		}
		if(fl==0)
			cout<<"YES\n";
	}
	return 0;
}


//Code of C#oder Born_Clear
#include <bits/stdc++.h>
using namespace std;
#define loop(n) for(int i=0;i<n;++i)
#define FORD(i,a,b) for(int i=a;i>=b;--i)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define ll long long
#define s(x) scanf("%d", &x)
#define sd(x) scanf("%lf", &x)
#define sl(x) scanf("%lld", &x)
#define mod 1000000007
#define fi first
#define se second
#define p(x) printf("%d",x)
#define pl(x) printf("%lld", x)
#define pd(x) printf("%lf", x)
#define pn() printf("\n")
#define vi(v) vector<int>v
#define vl(v) vector<long long int>v
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define len(s) s.length()
void input(int n,int a[]){loop(n)cin>>a[i];}
ll bin_expo(ll A, ll B, ll M){ll res = 1LL;while (B > 0){if(B % 2 == 1){res = (res * A) % M;}A = (A * A) % M; B = B / 2;}return res;}
int seg[2000000];
int kk=0;
int n;
int a[300005];

void build(int node,int l,int r)
{
	kk=max(kk,node);
	if(l==r)
	{
		seg[node]=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
void update(int node,int l,int r,int ind)
{
	if(l==r)
	{
		seg[node]=n+1;
		return;
	}
	int mid=(l+r)>>1;
	if(ind<=mid)
		update(2*node,l,mid,ind);
	else
		update(2*node+1,mid+1,r,ind);
	seg[node]=min(seg[node*2],seg[node*2+1]);
}
int query(int node,int l,int r,int ql,int qr)
{
	if(l>r || ql>r || qr<l)
		return n+1;
	if(ql<=l && qr>=r)
		return seg[node];
	int mid=(l+r)>>1;
	return min(query(2*node,l,mid,ql,qr),query(2*node+1,mid+1,r,ql,qr));
}
vector<int>pos[300005];
int fre[300005][2];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			pos[a[i]].pb(i);
			fre[a[i]][0]++;
		}
		build(1,0,n-1);
		int fl=0;
		for(int i=0;i<n;i++)
		{
			int x;
			cin>>x;
			if(fl==1)
				continue;
			if(pos[x].size()==fre[x][1])
			{
				fl=1;
				cout<<"NO\n";
				break;
			}
			int ind=pos[x][fre[x][1]++];
			int z=query(1,0,n-1,0,ind-1);
			if(z<x)
			{
				fl=1;
				cout<<"NO\n";
			}
			else
			{
				update(1,0,n-1,ind);
			}
		}
		if(fl==0)
			cout<<"YES\n";
		for(int i=0;i<n+2;i++){
			pos[i].clear();
			fre[i][0]=0;
			fre[i][1]=0;
		}
		for(int i=0;i<kk+2;i++)
			seg[node]=0;
		kk=0;
	}
	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// ----------------------------------------Defines and Template-------------------------------------//
#define ll long long
#define double long double
#define f first
#define ss second
#define mp make_pair
#define pb push_back

#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<vi>
#define vl vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).

#define mod 1000000007
#define INF 1000000000
#define setitr set <int, greater <int> > :: iterator
#define RE(i,n) for(int i=1; i<=n; i++)
#define REP(i,j,n) for(int i=j; i<=n; i++)
#define REB(i,j,n) for(int i=j; i>=n; i--)
#define FO(i,n) for(int i=n; i>0; i--)

//-------------------------------------------------Template Ends------------------------------------------------------/

const int N = 3e5+5;
int a[N], b[N], t[4*N];
set<int> p[N];

void update(int v, int tl, int tr, int pos, int val)
{
    if(tl==tr)
        t[v] = val;
    else
    {
        int tm = (tl+tr)>>1;
        if(pos<=tm)
            update(2*v, tl, tm, pos, val);
        else
            update(2*v+1, tm+1, tr, pos, val);
        t[v] = min(t[2*v], t[2*v+1]);
    }
}

int query(int v, int tl, int tr, int l, int r)
{
    if(l>r) return INF;
    if(l<=tl && tr<=r)
        return t[v];
    int tm = (tl+tr)>>1;
    return min(query(2*v, tl, tm, l, min(r,tm)), query(2*v+1, tm+1, tr, max(l,tm+1), r));
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    int n,i,j,c,k,m;
    cin >> c;
    while(c--)
    {
        cin >> n;
        RE(i,n)
        {
            cin >> j;
            p[j].insert(i-1);
        }
        RE(i,n) cin >> b[i-1];
        REP(i,0,4*n) t[i] = INF;

        RE(i,n)
        {
            //if(!p[i].empty()) update(1,0,n-1,*p[i].begin(), i);
            for(setitr it = p[i].begin(); it!=p[i].end(); it++)
            {
                update(1,0,n-1,*it,i);
            }
        }
        bool ok = true;
        REP(i,0,n-1)
        {
            if(p[b[i]].empty()) {ok = false; break; }
            j = *p[b[i]].begin();
            int q = query(1,0,n-1,i,j);
            if(q<b[i])
            {
                //cout << i << " " << j << " " << b[i] << "\n";
                ok = false;
                break;
            }
            update(1,0,n-1,j,INF);
            p[b[i]].erase(j);
            //if(!p[b[i]].empty())
                //update(1,0,n-1,*p[b[i]].begin(),b[i]);
        }
        if(ok) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// ----------------------------------------Defines and Template-------------------------------------//
#define ll long long
#define double long double
#define f first
#define ss second
#define mp make_pair
#define pb push_back

#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<vi>
#define vl vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).

#define mod 1000000007
#define INF 1000000000
#define setitr set <int, greater <int> > :: iterator
#define RE(i,n) for(int i=1; i<=n; i++)
#define REP(i,j,n) for(int i=j; i<=n; i++)
#define REB(i,j,n) for(int i=j; i>=n; i--)
#define FO(i,n) for(int i=n; i>0; i--)

//-------------------------------------------------Template Ends------------------------------------------------------/

const int N = 3e5+5;
int a[N], b[N], t[4*N];
set<int> p[N];

void update(int v, int tl, int tr, int pos, int val)
{
    if(tl==tr)
        t[v] = val;
    else
    {
        int tm = (tl+tr)>>1;
        if(pos<=tm)
            update(2*v, tl, tm, pos, val);
        else
            update(2*v+1, tm+1, tr, pos, val);
        t[v] = min(t[2*v], t[2*v+1]);
    }
}

int query(int v, int tl, int tr, int l, int r)
{
    if(l>r) return INF;
    if(l<=tl && tr<=r)
        return t[v];
    int tm = (tl+tr)>>1;
    return min(query(2*v, tl, tm, l, min(r,tm)), query(2*v+1, tm+1, tr, max(l,tm+1), r));
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    int c;
    cin >> c;
    while(c--)
    {
        int n,i,j,k,m;
        cin >> n;
        RE(i,n) p[i].clear();
        RE(i,n)
        {
            cin >> j;
            p[j].insert(i-1);
        }
        RE(i,n) cin >> b[i-1];
        REP(i,0,4*n) t[i] = INF;

        RE(i,n)
        {
            //if(!p[i].empty()) update(1,0,n-1,*p[i].begin(), i);
            for(setitr it = p[i].begin(); it!=p[i].end(); it++)
            {
                update(1,0,n-1,*it,i);
            }
        }
        bool ok = true;
        REP(i,0,n-1)
        {
            if(p[b[i]].empty()) {ok = false; break; }
            j = *p[b[i]].begin();
            int q = query(1,0,n-1,i,j);
            if(q<b[i])
            {
                //cout << i << " " << j << " " << b[i] << "\n";
                ok = false;
                break;
            }
            update(1,0,n-1,j,INF);
            p[b[i]].erase(j);
            //if(!p[b[i]].empty())
                //update(1,0,n-1,*p[b[i]].begin(),b[i]);
        }
        if(ok) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// ----------------------------------------Defines and Template-------------------------------------//
#define ll long long
#define double long double
#define f first
#define ss second
#define mp make_pair
#define pb push_back

#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<vi>
#define vl vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).

#define mod 1000000007
#define INF 1000000000
#define setitr set <int, greater <int> > :: iterator
#define RE(i,n) for(int i=1; i<=n; i++)
#define REP(i,j,n) for(int i=j; i<=n; i++)
#define REB(i,j,n) for(int i=j; i>=n; i--)
#define FO(i,n) for(int i=n; i>0; i--)

//-------------------------------------------------Template Ends------------------------------------------------------/

const int N = 3e5+5;
int a[N], b[N];
pii t[4*N];
set<int> p[N];

void update(int v, int tl, int tr, int pos, int val)
{
    if(tl==tr)
        t[v] = mp(val,pos);
    else
    {
        int tm = (tl+tr)>>1;
        if(pos<=tm)
            update(2*v, tl, tm, pos, val);
        else
            update(2*v+1, tm+1, tr, pos, val);
        t[v] = min(t[2*v], t[2*v+1]);
    }
}

pii query(int v, int tl, int tr, int l, int r)
{
    if(l>r) return mp(INF,INF);
    if(tl==l && tr==r)
        return t[v];
    int tm = (tl+tr)>>1;
    return min(query(2*v, tl, tm, l, min(r,tm)), query(2*v+1, tm+1, tr, max(l,tm+1), r));
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    int n,i,j,c,k,m;
    cin >> c;
    while(c--)
    {
        cin >> n;
        RE(i,n)
        {
            cin >> j;
            p[j].insert(i-1);
        }
        RE(i,n) cin >> b[i-1];
        REP(i,0,4*n) t[i] = mp(INF,INF);

        RE(i,n)
        {
            if(!p[i].empty()) update(1,0,n-1,*p[i].begin(), i);
        }
        bool ok = true;
        REP(i,0,n-1)
        {
            if(p[b[i]].empty()) {ok = false; break; }
            j = *p[b[i]].begin();
            pii q = query(1,0,n-1,i,j);
            if(q.f<b[i])
            {
                ok = false;
                break;
            }
            p[b[i]].erase(j);
            update(1,0,n-1,q.ss,INF);
            update(1,0,n-1,*p[b[i]].begin(),b[i]);
        }
        if(ok) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// ----------------------------------------Defines and Template-------------------------------------//
#define ll long long
#define double long double
#define f first
#define ss second
#define mp make_pair
#define pb push_back

#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<vi>
#define vl vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).

#define mod 1000000007
#define INF 1000000000
#define setitr set <int, greater <int> > :: iterator
#define RE(i,n) for(int i=1; i<=n; i++)
#define REP(i,j,n) for(int i=j; i<=n; i++)
#define REB(i,j,n) for(int i=j; i>=n; i--)
#define FO(i,n) for(int i=n; i>0; i--)

//-------------------------------------------------Template Ends------------------------------------------------------/

const int N = 3e5+5;
int a[N], b[N], t[4*N];
set<int> p[N];

void update(int v, int tl, int tr, int pos, int val)
{
    if(tl==tr)
        t[v] = val;
    else
    {
        int tm = (tl+tr)>>1;
        if(pos<=tm)
            update(2*v, tl, tm, pos, val);
        else
            update(2*v+1, tm+1, tr, pos, val);
        t[v] = min(t[2*v], t[2*v+1]);
    }
}

int query(int v, int tl, int tr, int l, int r)
{
    if(l>r) return INF;
    if(l<=tl && tr<=r)
        return t[v];
    int tm = (tl+tr)>>1;
    return min(query(2*v, tl, tm, l, min(r,tm)), query(2*v+1, tm+1, tr, max(l,tm+1), r));
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    int c;
    cin >> c;
    while(c--)
    {
        int n,i,j,k,m;
        cin >> n;
        RE(i,n) p[i].clear();
        RE(i,n)
        {
            cin >> j;
            p[j].insert(i-1);
        }
        RE(i,n) cin >> b[i-1];
        REP(i,0,4*n) t[i] = INF;

        RE(i,n)
        {
            if(!p[i].empty()) update(1,0,n-1,*p[i].begin(), i);
            /*
            for(setitr it = p[i].begin(); it!=p[i].end(); it++)
            {
                update(1,0,n-1,*it,i);
            }
            */
        }
        bool ok = true;
        REP(i,0,n-1)
        {
            if(p[b[i]].empty()) {ok = false; break; }
            j = *p[b[i]].begin();
            int q = query(1,0,n-1,0,j);
            if(q<b[i])
            {
                //cout << i << " " << j << " " << b[i] << "\n";
                ok = false;
                break;
            }
            update(1,0,n-1,j,INF);
            p[b[i]].erase(j);
            if(!p[b[i]].empty())
                update(1,0,n-1,*p[b[i]].begin(),b[i]);
        }
        if(ok) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// ----------------------------------------Defines and Template-------------------------------------//
#define ll long long
#define double long double
#define f first
#define ss second
#define mp make_pair
#define pb push_back

#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<vi>
#define vl vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).

#define mod 1000000007
#define INF 1000000000
#define setitr set <int, greater <int> > :: iterator
#define RE(i,n) for(int i=1; i<=n; i++)
#define REP(i,j,n) for(int i=j; i<=n; i++)
#define REB(i,j,n) for(int i=j; i>=n; i--)
#define FO(i,n) for(int i=n; i>0; i--)

//-------------------------------------------------Template Ends------------------------------------------------------/

const int N = 3e5+5;
int a[N], b[N], t[4*N];
set<int> p[N];

void update(int v, int tl, int tr, int pos, int val)
{
    if(tl==tr)
        t[v] = val;
    else
    {
        int tm = (tl+tr)>>1;
        if(pos<=tm)
            update(2*v, tl, tm, pos, val);
        else
            update(2*v+1, tm+1, tr, pos, val);
        t[v] = min(t[2*v], t[2*v+1]);
    }
}

int query(int v, int tl, int tr, int l, int r)
{
    if(l>r) return INF;
    if(l<=tl && tr<=r)
        return t[v];
    int tm = (tl+tr)>>1;
    return min(query(2*v, tl, tm, l, min(r,tm)), query(2*v+1, tm+1, tr, max(l,tm+1), r));
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    int c;
    cin >> c;
    while(c--)
    {
        int n,i,j,k,m;
        cin >> n;
        RE(i,n) p[i].clear();
        RE(i,n)
        {
            cin >> j;
            p[j].insert(i-1);
        }
        RE(i,n) cin >> b[i-1];
        REP(i,0,4*n) t[i] = INF;

        RE(i,n)
        {
            if(!p[i].empty()) update(1,0,n-1,*p[i].begin(), i);
            /*
            for(setitr it = p[i].begin(); it!=p[i].end(); it++)
            {
                update(1,0,n-1,*it,i);
            }
            */
        }
        bool ok = true;
        REP(i,0,n-1)
        {
            if(p[b[i]].empty()) {ok = false; break; }
            j = *p[b[i]].begin();
            int q = query(1,0,n-1,i,j);
            if(q<b[i])
            {
                //cout << i << " " << j << " " << b[i] << "\n";
                ok = false;
                break;
            }
            update(1,0,n-1,j,INF);
            p[b[i]].erase(j);
            if(!p[b[i]].empty())
                update(1,0,n-1,*p[b[i]].begin(),b[i]);
        }
        if(ok) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// ----------------------------------------Defines and Template-------------------------------------//
#define ll long long
#define double long double
#define f first
#define ss second
#define mp make_pair
#define pb push_back

#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vii vector<vi>
#define vl vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).

#define mod 1000000007
#define INF 1000000000
#define setitr set <int, greater <int> > :: iterator
#define RE(i,n) for(int i=1; i<=n; i++)
#define REP(i,j,n) for(int i=j; i<=n; i++)
#define REB(i,j,n) for(int i=j; i>=n; i--)
#define FO(i,n) for(int i=n; i>0; i--)

//-------------------------------------------------Template Ends------------------------------------------------------/

const int N = 3e5+5;
int a[N], b[N];
pii t[4*N];
set<int> p[N];

void update(int v, int tl, int tr, int pos, int val)
{
    if(tl==tr)
        t[v] = mp(val,pos);
    else
    {
        int tm = (tl+tr)>>1;
        if(pos<=tm)
            update(2*v, tl, tm, pos, val);
        else
            update(2*v+1, tm+1, tr, pos, val);
        t[v] = min(t[2*v], t[2*v+1]);
    }
}

pii query(int v, int tl, int tr, int l, int r)
{
    if(l>r) return mp(INF,INF);
    if(l<=tl && tr<=r)
        return t[v];
    int tm = (tl+tr)>>1;
    return min(query(2*v, tl, tm, l, min(r,tm)), query(2*v+1, tm+1, tr, max(l,tm+1), r));
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    //freopen("input.txt", "r", stdin);
    int n,i,j,c,k,m;
    cin >> c;
    while(c--)
    {
        cin >> n;
        RE(i,n)
        {
            cin >> j;
            p[j].insert(i-1);
        }
        RE(i,n) cin >> b[i-1];
        REP(i,0,4*n) t[i] = mp(INF,INF);

        RE(i,n)
        {
            if(!p[i].empty()) update(1,0,n-1,*p[i].begin(), i);
        }
        bool ok = true;
        REP(i,0,n-1)
        {
            if(p[b[i]].empty()) {ok = false; break; }
            j = *p[b[i]].begin();
            pii q = query(1,0,n-1,i,j);
            if(q.f<b[i])
            {
                ok = false;
                break;
            }
            p[b[i]].erase(j);
            update(1,0,n-1,q.ss,INF);
            if(!p[b[i]].empty())
                update(1,0,n-1,*p[b[i]].begin(),b[i]);
        }
        if(ok) cout << "YES\n";
        else cout << "NO\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=1e9+7;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=3e5+5;
vi cnt(N),cnt1(N);
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int t;cin>>t;
	int a1,b1;
	for(int it=1;it<=t;it++){
		int n;cin>>n;
		for(int i=1;i<=n;i++)cnt[i]=cnt1[i]=0;
		vi a(n),b(n);
		for(int i=0;i<n;i++)cin>>a[i],cnt[a[i]]++;
		for(int i=0;i<n;i++)cin>>b[i],cnt1[b[i]]++;
		bool bo=1;
		for(int i=1;i<=n;i++)bo&=(cnt[i]==cnt1[i]);
		if(!bo){cout<<"NO"<<endl;continue;}
		int j=0;multiset<int> m;
		for(int i=0;i<n;i++){
			while(m.empty()||(j<n&&*m.begin()>b[i]))
				m.I(a[j++]);
			if(*m.begin()!=b[i]){bo=0;break;}
			m.erase(m.begin());
		}
		if(bo)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=1e9+7;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=1e5+5;
typedef tree<int ,null_type,less_equal<int >,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
ordered_set X;
vi cnt(N),cnt1(N);
vvi ng(N);
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int t;cin>>t;
	while(t--){
		int n;cin>>n;
		for(int i=1;i<=n;i++)cnt[i]=cnt1[i]=0,ng[i].clear();
		vi a(n),b(n);
		for(int i=0;i<n;i++)cin>>a[i],cnt[a[i]]++;
		for(int i=0;i<n;i++)cin>>b[i],cnt1[b[i]]++;
		X.clear();
		for(int i=0;i<n;i++){
			ng[a[i]].pb(X.order_of_key(a[i]+1));
			X.I(a[i]);
		}
		X.clear();
		bool bo=1;
		for(int i=1;i<=n;i++)reverse(all(ng[i])),bo&=(cnt[i]==cnt1[i]);
		for(int i=0;i<n;i++){
			int temp=X.order_of_key(b[i]+1);
			bo&=(temp>=ng[b[i]].back());
			ng[b[i]].pop_back();
			X.I(b[i]);
		}
		X.clear();
		if(bo)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=1e9+7;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=3e5+5;
vi cnt(N),cnt1(N);
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int t;cin>>t;
	int a1,b1;
	for(int it=1;it<=t;it++){
		int n;cin>>n;
		for(int i=1;i<=n;i++)cnt[i]=cnt1[i]=0;
		vi a(n),b(n);
		for(int i=0;i<n;i++)cin>>a[i],cnt[a[i]]++;
		for(int i=0;i<n;i++)cin>>b[i],cnt1[b[i]]++;
		bool bo=1;
		for(int i=1;i<=n;i++)bo&=(cnt[i]==cnt1[i]);
		if(!bo){cout<<"NO"<<endl;continue;}
		int j=0;multiset<int> m;
		for(int i=0;i<n;i++){
			while(m.empty()||(j<n&&m.find(b[i])==m.end()))
				m.I(a[j++]);
			if(*m.begin()!=b[i]){bo=0;break;}
			m.erase(m.begin());
		}
		if(bo)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=1e9+7;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=3e5+5;
typedef tree<int ,null_type,less_equal<int >,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
ordered_set X;
vi cnt(N),cnt1(N);
vvi ng(N);
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int t;cin>>t;
	while(t--){
		int n;cin>>n;
		for(int i=1;i<=n;i++)cnt[i]=cnt1[i]=0,ng[i].clear();
		vi a(n),b(n);
		for(int i=0;i<n;i++)cin>>a[i],cnt[a[i]]++;
		for(int i=0;i<n;i++)cin>>b[i],cnt1[b[i]]++;
		X.clear();
		for(int i=0;i<n;i++){
			ng[a[i]].pb(X.order_of_key(a[i]+1));
			X.I(a[i]);
		}
		X.clear();
		bool bo=1;
		for(int i=1;i<=n;i++)reverse(all(ng[i])),bo&=(cnt[i]==cnt1[i]);
		if(!bo){cout<<"NO"<<endl;continue;}
		for(int i=0;i<n;i++){
			int temp=X.order_of_key(b[i]+1);
			bo&=(temp>=ng[b[i]].back());
			ng[b[i]].pop_back();
			X.I(b[i]);
		}
		X.clear();
		if(bo)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=1e9+7;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=3e5+5;
vi cnt(N),cnt1(N);
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int t;cin>>t;
	int a1,b1;
	for(int it=1;it<=t;it++){
		int n;cin>>n;
		for(int i=1;i<=n;i++)cnt[i]=cnt1[i]=0;
		vi a(n),b(n);
		for(int i=0;i<n;i++)cin>>a[i],cnt[a[i]]++;
		for(int i=0;i<n;i++)cin>>b[i],cnt1[b[i]]++;
		bool bo=1;
		for(int i=1;i<=n;i++)bo&=(cnt[i]==cnt1[i]);
		if(!bo){cout<<"NO"<<endl;continue;}
		int j=n-1;multiset<int> m;
		for(int i=n-1;i>=0;i--){
			while(m.empty()||(j>=0&&*m.rbegin()<b[i]))
				m.I(a[j--]);
			if(*m.rbegin()!=b[i]){bo=0;break;}
			m.erase(m.find(b[i]));
		}
		if(bo)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=1e9+7;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=1e5+5;
typedef tree<int ,null_type,less_equal<int >,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
ordered_set X;
vi cnt(N),cnt1(N);
vvi ng(N);
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int t;cin>>t;
	while(t--){
		int n;cin>>n;
		for(int i=1;i<=n;i++)cnt[i]=cnt1[i]=0,ng[i].clear();
		vi a(n),b(n);
		for(int i=0;i<n;i++)cin>>a[i],cnt[a[i]]++;
		for(int i=0;i<n;i++)cin>>b[i],cnt1[b[i]]++;
		X.clear();
		for(int i=0;i<n;i++){
			ng[a[i]].pb(X.order_of_key(a[i]+1));
			X.I(a[i]);
		}
		X.clear();
		bool bo=1;
		for(int i=1;i<=n;i++)reverse(all(ng[i])),bo&=(cnt[i]==cnt1[i]);
		if(!bo){cout<<"NO"<<endl;continue;}
		for(int i=0;i<n;i++){
			int temp=X.order_of_key(b[i]+1);
			bo&=(temp>=ng[b[i]].back());
			ng[b[i]].pop_back();
			X.I(b[i]);
		}
		X.clear();
		if(bo)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=1e9+7;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=3e5+5;
const int inf=1e9;
vi cnt(N),cnt1(N);
vvi idx(N);
int arr[N],st[N<<2];

void build(int l,int r,int id)
{
	if(l==r) { st[id] = arr[l]; return; }
	build ( l, l+r >>1 , id<< 1); build( (l + r >> 1) + 1, r , id<< 1 | 1);
	st[id] = min(st[ id << 1] , st[id << 1 | 1]);
}

void upd(int l,int r,int id,int x,int val)
{
	if (l > x || r < x ) return;
	if (l == x && r == x ) { st[id]=val; return;}
	upd(l,l + r >> 1,id << 1, x, val);upd((l + r >> 1) + 1,r ,id << 1 | 1,x, val);
	st[id] = min(st[id << 1] , st[ id << 1 | 1]);
}

int getmin(int l,int r,int id,int x,int y)
{
	if (l > y || r < x ) return inf;
	if (l >= x && r <= y ) return st[id];
	return min(getmin(l, l + r >> 1,id << 1, x, y) , getmin((l + r >> 1 ) + 1,r ,id << 1 | 1,x, y));
}
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int t;cin>>t;
	int a1,b1;
	for(int it=1;it<=t;it++){
		int n;cin>>n;
		for(int i=0;i<=n;i++)cnt[i]=cnt1[i]=0,idx[i].clear(),arr[i]=0;
		for(int i=0;i<=4*n;i++)st[i]=0;
		vi a(n),b(n);
		for(int i=0;i<n;i++)cin>>a[i],idx[a[i]].pb(i),arr[i]=a[i],cnt[a[i]]++;
		for(int i=0;i<n;i++)cin>>b[i],cnt1[b[i]]++;
		bool bo=1;
		for(int i=1;i<=n;i++)bo&=(cnt[i]==cnt1[i]),reverse(all(idx[i]));
		if(!bo){cout<<"NO"<<endl;continue;}
		build(0,n-1,1);
		for(int i=0;i<n;i++){
			if(getmin(0,n-1,1,0,idx[b[i]].back())<b[i]){bo=0;break;}
			upd(0,n-1,1,idx[b[i]].back(),inf);
			idx[b[i]].pop_back();
		}
		if(bo)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=1e9+7;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=3e5+5;
typedef tree<int ,null_type,less_equal<int >,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
ordered_set X;
vi cnt(N),cnt1(N);
vvi ng(N),bg(N);
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int t;cin>>t;
	while(t--){
		int n;cin>>n;
		for(int i=1;i<=n;i++)cnt[i]=cnt1[i]=0,ng[i].clear(),bg[i].clear();
		vi a(n),b(n);
		for(int i=0;i<n;i++)cin>>a[i],cnt[a[i]]++;
		for(int i=0;i<n;i++)cin>>b[i],cnt1[b[i]]++;
		X.clear();
		for(int i=0;i<n;i++){
			ng[a[i]].pb(X.order_of_key(a[i]+1));
			bg[a[i]].pb(X.size()-X.order_of_key(a[i]+1));
			X.I(a[i]);
		}
		X.clear();
		bool bo=1;
		for(int i=1;i<=n;i++)reverse(all(ng[i])),reverse(all(bg[i])),bo&=(cnt[i]==cnt1[i]);
		if(!bo){cout<<"NO"<<endl;continue;}
		for(int i=0;i<n;i++){
			int temp=X.order_of_key(b[i]+1);
			bo&=(temp>=ng[b[i]].back());
			bo&=(X.size()-temp<=bg[b[i]].back());
			ng[b[i]].pop_back();bg[b[i]].pop_back();
			X.I(b[i]);
		}
		X.clear();
		if(bo)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define ll long long

template<class T> ostream& operator<<(ostream &os,vector<T> V){
	os<<"[ ";for(auto v:V)os<<v<<" ";return os<<" ]";
}
template<class L,class R> ostream& operator<<(ostream &os,pair<L,R> P){
	return os<<"("<<P.first<<","<<P.second<<")";
}
ll mxm(){return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a,Args... args){return max(a,mxm(args...));}
ll mnm(){return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a,Args... args){return min(a,mnm(args...));}

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__,__VA_ARGS__)
template<typename Arg1>
void __f(const char* name,Arg1&& arg1){
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1,typename... Args>
void __f(const char* names,Arg1&& arg1,Args&&... args){
		const char* comma=strchr(names+1,',');cout.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1,args...);
}
#else
#define trace(...) 1
#endif

#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define ii pair<int,int>
#define vi vector<int>
#define vpll vector<pll>
#define vii vector<ii>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvii vector<vii>
#define vld vector<ld>
#define vvld vector<vld>
#define I insert
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define endl "\n"
#define siz(a)	((int)(a).size())

const int mod=1e9+7;
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}

const int N=3e5+5;
typedef tree<int ,null_type,less_equal<int >,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
ordered_set X;
vi cnt(N),cnt1(N);
vvi ng(N),bg(N);
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
	int t;cin>>t;
	int a1,b1;
	for(int it=1;it<=t;it++){
		int n;cin>>n;
		for(int i=1;i<=n;i++)cnt[i]=cnt1[i]=0,ng[i].clear(),bg[i].clear();
		vi a(n),b(n);
		for(int i=0;i<n;i++)cin>>a[i],cnt[a[i]]++;
		for(int i=0;i<n;i++)cin>>b[i],cnt1[b[i]]++;
		if(n>=2&&it==1)a1=a[0],b1=a[1];
		X.clear();
		if(it==75&&a1==7&&b1==7&&t==3e4){
			trace(a);trace(b);
		}
		for(int i=0;i<n;i++){
			ng[a[i]].pb(X.order_of_key(a[i]+1));
			bg[a[i]].pb(X.size()-X.order_of_key(a[i]+1));
			X.I(a[i]);
		}
		X.clear();
		bool bo=1;
		for(int i=1;i<=n;i++)reverse(all(ng[i])),reverse(all(bg[i])),bo&=(cnt[i]==cnt1[i]);
		if(!bo){cout<<"NO"<<endl;continue;}
		for(int i=0;i<n;i++){
			int temp=X.order_of_key(b[i]+1);
			bo&=(temp>=ng[b[i]].back());
			bo&=(X.size()-temp<=bg[b[i]].back());
			ng[b[i]].pop_back();bg[b[i]].pop_back();
			X.I(b[i]);
		}
		X.clear();
		if(bo)cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


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

#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb_ push_back
#define pf_ push_front
#define mp_ make_pair
#define ins_ insert
#define rsz_ resize
#define clr_ clear
#define itr_ iterator
#define ritr_ reverse_iterator
#define lb_ lower_bound
#define ub_ upper_bound
#define len_ length
#define fir first
#define sec second

typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
int solve(int n,int m, int k){
	int c,nn,nm,nk;
	if(k==n*m) return 0;
	if(k==0 || m==0) return 0;
	if(k<m && m==1) return 1;
	if(k<m) return m*m+1;
	else{
		c=m*m;
		nk=k%m;
		nn=n;
		nm=m-(k/m);
		if(nn>=nm) c+=solve(nn,nm,nk);
		else c+=solve(nm,nn,nk);
		return c;
	}
}
int main(){
	IOS;
	int t,n,m,k,r;
	cin>>t;
	while(t--){
		cin>>n>>m>>k;
		if(n>=m)r=solve(n,m,k);
		else r=solve(m,n,k);
		cout<<r<<"\n";
	}
	return 0;
}


/*
                " اللهم صلي و سلم علي سيدنا محمد و علي اله وصحبه اجمعين "
*/

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define f for
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define intial(s1, s2, x, dp) f(int i = 0; i < s1; i++) f(int j = 0; j < s2; j++) dp[i][j] = x;
using namespace std;
#define BI acos(-1)
#define all(a) a.begin(), a.end()
#define loop1(n) f(int i = 0; i < n; i++)
#define loop2(n) f(int j = 0; j < n; j++)
#define mem(dp) memset(dp, -1, sizeof dp);
#define oo 1e9;
void Open()
{
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
}
ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}
void madesetprobblem()
{
    //$ $
    //less than or equal => $ 1 \leq 100 $
    //  $(1 \le A,B \le 10^5 )$
    // a[i] => $a_i$
    // a[i][j] => $a_{i,j}$
    // double qoutes ''yext''
    /*
        image
        \begine{center}
        \includegraphics{image.jpg}
        \end{center}
    */
    /*
        list
        \begin{itemize}
            \item first one
            \item second one
        \end{itemize}
    */
}
int n,m,k;
int dp[31][31][51];
int ans(int x,int y,int k)
{
    if(k==0)return 0;
    int &ret=dp[x][y][k];
    if(~ret)return ret;
    if(x*y<=k)
    {
        if(x*y==k)
        return 0;
        else return 1e6;
    }
    ret=1e6;
    f(int i=1;i<x;i++)
    {
        if(i*y<=k)
        {
            ret=min(ret,ans(x-i,y,k-i*y)+y*y);
        }
        if((x-i)*y<=k)
        {
            ret=min(ret,ans(i,y,k-(x-i)*y)+y*y);
        }
        if(i*y>k&&(x-i)*y>k)
            ret=min({ret,ans(x-i,y,k)+y*y,ans(i,y,k)+y*y});
    }
    f(int i=1;i<y;i++)
    {
        if(i*x<=k)
        {
            ret=min(ret,ans(x,y-i,k-i*x)+x*x);
        }
        if((y-i)*x<=k)
        {
            ret=min(ret,ans(x,i,k-(y-i)*x)+x*x);
        }
        if(i*x>k&&(y-i)*x>k)
        ret=min({ret,ans(x,y-i,k)+x*x,ans(x,i,k)+x*x});
    }
    return ret;
}
int main()
{
    //Open();
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    mem(dp);
    int t;
    cin>>t;
    while(t--)
    {cin>>n>>m>>k;
    cout<<ans(n,m,k)<<endl;
    }
}


/*
                " اللهم صلي و سلم علي سيدنا محمد و علي اله وصحبه اجمعين "
*/

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define f for
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define intial(s1, s2, x, dp) f(int i = 0; i < s1; i++) f(int j = 0; j < s2; j++) dp[i][j] = x;
using namespace std;
#define BI acos(-1)
#define all(a) a.begin(), a.end()
#define loop1(n) f(int i = 0; i < n; i++)
#define loop2(n) f(int j = 0; j < n; j++)
#define mem(dp) memset(dp, -1, sizeof dp);
#define oo 1e9;
void Open()
{
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
}
ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}
void madesetprobblem()
{
    //$ $
    //less than or equal => $ 1 \leq 100 $
    //  $(1 \le A,B \le 10^5 )$
    // a[i] => $a_i$
    // a[i][j] => $a_{i,j}$
    // double qoutes ''yext''
    /*
        image
        \begine{center}
        \includegraphics{image.jpg}
        \end{center}
    */
    /*
        list
        \begin{itemize}
            \item first one
            \item second one
        \end{itemize}
    */
}
int n,m,k;
int dp[31][31][51];
int ans(int x,int y,int k)
{
    int &ret=dp[x][y][k];
    if(~ret)return ret;
    if(x*y<=k)
    {
        if(x*y==k)
        return 0;
        else return 1e6;
    }
    ret=1e6;
    f(int i=1;i<x;i++)
    {
        if(i*y<k)
        {
            ret=min(ret,ans(x-i,y,k-i*y)+y*y);
        }
        else if((x-i)*y<k)
        {
            ret=min(ret,ans(i,y,k-(x-i)*y)+y*y);
        }
        else
        {
            ret=min({ret,ans(x-i,y,k)+y*y,ans(i,y,k)+y*y});
        }
    }
    f(int i=1;i<y;i++)
    {
        if(i*x<k)
        {
            ret=min(ret,ans(x,y-i,k-i*x)+x*x);
        }
        else if((x-i)*y<k)
        {
            ret=min(ret,ans(x,i,k-(y-i)*x)+x*x);
        }
        else
        {
            ret=min({ret,ans(x,y-i,k)+x*x,ans(x,i,k)+x*x});
        }
    }
    return ret;
}
int main()
{
    //Open();
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    mem(dp);
    int t;
    cin>>t;
    while(t--)
    {cin>>n>>m>>k;
    cout<<ans(n,m,k)<<endl;
    }
}


/*
                " اللهم صلي و سلم علي سيدنا محمد و علي اله وصحبه اجمعين "
*/

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define f for
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define intial(s1, s2, x, dp) f(int i = 0; i < s1; i++) f(int j = 0; j < s2; j++) dp[i][j] = x;
using namespace std;
#define BI acos(-1)
#define all(a) a.begin(), a.end()
#define loop1(n) f(int i = 0; i < n; i++)
#define loop2(n) f(int j = 0; j < n; j++)
#define mem(dp) memset(dp, -1, sizeof dp);
#define oo 1e9;
void Open()
{
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
}
ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}
void madesetprobblem()
{
    //$ $
    //less than or equal => $ 1 \leq 100 $
    //  $(1 \le A,B \le 10^5 )$
    // a[i] => $a_i$
    // a[i][j] => $a_{i,j}$
    // double qoutes ''yext''
    /*
        image
        \begine{center}
        \includegraphics{image.jpg}
        \end{center}
    */
    /*
        list
        \begin{itemize}
            \item first one
            \item second one
        \end{itemize}
    */
}
int n,m,k;
int dp[31][31][51];
int ans(int x,int y,int k)
{
    if(k==0)return 0;
    int &ret=dp[x][y][k];
    if(~ret)return ret;
    if(x*y<=k)
    {
        if(x*y==k)
        return 0;
        else return 1e6;
    }
    ret=1e6;
    f(int i=1;i<x;i++)
    {
        if(i*y<k)
        {
            ret=min(ret,ans(x-i,y,k-i*y)+y*y);
        }
        else if((x-i)*y<k)
        {
            ret=min(ret,ans(i,y,k-(x-i)*y)+y*y);
        }
        else
        {
            ret=min({ret,ans(x-i,y,k)+y*y,ans(i,y,k)+y*y});
        }
    }
    f(int i=1;i<y;i++)
    {
        if(i*x<k)
        {
            ret=min(ret,ans(x,y-i,k-i*x)+x*x);
        }
        else if((x-i)*y<k)
        {
            ret=min(ret,ans(x,i,k-(y-i)*x)+x*x);
        }
        else
        {
            ret=min({ret,ans(x,y-i,k)+x*x,ans(x,i,k)+x*x});
        }
    }
    return ret;
}
int main()
{
    //Open();
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    mem(dp);
    int t;
    cin>>t;
    while(t--)
    {cin>>n>>m>>k;
    cout<<ans(n,m,k)<<endl;
    }
}


/*
                " اللهم صلي و سلم علي سيدنا محمد و علي اله وصحبه اجمعين "
*/

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define f for
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define intial(s1, s2, x, dp) f(int i = 0; i < s1; i++) f(int j = 0; j < s2; j++) dp[i][j] = x;
using namespace std;
#define BI acos(-1)
#define all(a) a.begin(), a.end()
#define loop1(n) f(int i = 0; i < n; i++)
#define loop2(n) f(int j = 0; j < n; j++)
#define mem(dp) memset(dp, -1, sizeof dp);
#define oo 1e9;
void Open()
{
    freopen("Input.txt", "r", stdin);
    freopen("Output.txt", "w", stdout);
}
ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}
void madesetprobblem()
{
    //$ $
    //less than or equal => $ 1 \leq 100 $
    //  $(1 \le A,B \le 10^5 )$
    // a[i] => $a_i$
    // a[i][j] => $a_{i,j}$
    // double qoutes ''yext''
    /*
        image
        \begine{center}
        \includegraphics{image.jpg}
        \end{center}
    */
    /*
        list
        \begin{itemize}
            \item first one
            \item second one
        \end{itemize}
    */
}
int n,m,k;
int dp[31][31][51];
int ans(int x,int y,int k)
{
    if(k==0)return 0;
    int &ret=dp[x][y][k];
    if(~ret)return ret;
    if(x*y<=k)
    {
        if(x*y==k)
        return 0;
        else return 1e6;
    }
    ret=1e6;
    f(int i=1;i<x;i++)
    {
        if(i*y<=k)
        {
            ret=min(ret,ans(x-i,y,k-i*y)+y*y);
        }
        if((x-i)*y<=k)
        {
            ret=min(ret,ans(i,y,k-(x-i)*y)+y*y);
        }
        if(i*y>k)
            ret=min({ret,ans(i,y,k)+y*y});
        if((x-i*y)>k)
            ret=min({ret,ans(x-i,y,k)+y*y});
    }
    f(int i=1;i<y;i++)
    {
        if(i*x<=k)
        {
            ret=min(ret,ans(x,y-i,k-i*x)+x*x);
        }
        if((y-i)*x<=k)
        {
            ret=min(ret,ans(x,i,k-(y-i)*x)+x*x);
        }
        if(i*x>k)
        ret=min({ret,ans(x,i,k)+x*x});
        if((y-i)*x>k)
        {
            ret=min({ret,ans(x,y-i,k)+x*x});
        }
    }
    return ret;
}
int main()
{
    //Open();
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    mem(dp);
    int t;
    cin>>t;
    while(t--)
    {cin>>n>>m>>k;
    cout<<ans(n,m,k)<<endl;
    }
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=1e9+7;
int n,q,k,m,dp[35][35][55];
int DFS(int n,int m,int k)
{
	if(dp[n][m][k]!=inf) return dp[n][m][k];
	if(k==n*m||k==0) return dp[n][m][k]=0;
	if(k>n*m) return dp[n][m][k]=inf;
	for(int i=1;i<n;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[n][m][k]=min(dp[n][m][k],DFS(n-i,m,j)+DFS(i,m,k-j)+m*m);
		}
	}
	for(int i=1;i<m;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[n][m][k]=min(dp[n][m][k],DFS(n,m-i,j)+DFS(n,i,k-j)+n*n);
		}
	}
	return dp[n][m][k];
}
signed main()
{
	cin>>q;
	while(q--)
	{
		cin>>n>>m>>k;
		for(int i=0;i<=30;i++)
		for(int j=0;j<=30;j++)
		for(int t=0;t<=50;t++) dp[i][j][t]=inf;
		cout<<DFS(n,m,k)<<"\n";
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=1e9+7;
int n,q,k,m,dp[35][35][55];
int DFS(int n,int m,int k)
{
	if(dp[n][m][k]!=inf) return dp[n][m][k];
	if(k==n*m||k==0) return dp[n][m][k]=0;
	if(k>n*m) return dp[n][m][k]=inf;
	for(int i=1;i<n;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[n][m][k]=min(dp[n][m][k],DFS(n-i,m,j)+DFS(i,m,k-j)+m*m);
		}
	}
	for(int i=1;i<n;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[n][m][k]=min(dp[n][m][k],DFS(n,m-i,j)+DFS(n,i,k-j)+n*n);
		}
	}
	return dp[n][m][k];
}
signed main()
{
	cin>>q;
	while(q--)
	{
		cin>>n>>m>>k;
		for(int i=0;i<=30;i++)
		for(int j=0;j<=30;j++)
		for(int t=0;t<=50;t++) dp[i][j][t]=inf;
		cout<<DFS(n,m,k)<<"\n";
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=1e9+7;
int n,q,k,m,dp[35][35][55];
int DFS(int n,int m,int k)
{
	if(dp[n][m][k]!=inf) return dp[n][m][k];
	if(k==n*m||k==0) return dp[n][m][k]=0;
	if(k>n*m) return dp[n][m][k]=inf;
	for(int i=1;i<n;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[n][m][k]=min(dp[n][m][k],DFS(n-i,m,j)+DFS(i,m,k-j)+m*m);
		}
	}
	for(int i=1;i<m;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[n][m][k]=min(dp[n][m][k],DFS(n,m-i,j)+DFS(n,i,k-j)+n*n);
		}
	}
	return dp[n][m][k];
}
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>q;
	while(q--)
	{
		cin>>n>>m>>k;
		for(int i=0;i<=30;i++)
		for(int j=0;j<=30;j++)
		for(int t=0;t<=50;t++) dp[i][j][t]=inf;
		cout<<DFS(n,m,k)<<"\n";
	}
}


#include<bits/stdc++.h>
#pragma O3
#define int long long
using namespace std;
const int inf=1e9+7;
int n,q,k,m,dp[35][35][55];
int DFS(int n,int m,int k)
{
	int res=inf;
	if(dp[n][m][k]!=inf) return dp[n][m][k];
	if(k==n*m||k==0) return dp[n][m][k]=0;
	if(k>n*m) return dp[n][m][k]=inf;
	for(int i=1;i<n;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[n][m][k]=min(res,DFS(n-i,m,j)+DFS(i,m,k-j)+m*m);
		}
	}
	for(int i=1;i<m;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[n][m][k]=min(res,DFS(n,m-i,j)+DFS(n,i,k-j)+n*n);
		}
	}
	return dp[n][m][k]=res;
}
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>q;
	while(q--)
	{
		cin>>n>>m>>k;
		cout<<DFS(n,m,k)<<"\n";
	}
}


#include<bits/stdc++.h>
#define int long long
#pragma O3
using namespace std;
const int inf=1e9+7;
int n,q,k,m,dp[35][35][55];
int DFS(int n,int m,int k)
{
	if(dp[n][m][k]!=inf) return dp[n][m][k];
	if(k==n*m||k==0) return dp[n][m][k]=0;
	if(k>n*m) return dp[n][m][k]=inf;
	for(int i=1;i<=n/2;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[n][m][k]=min(dp[n][m][k],DFS(n-i,m,j)+DFS(i,m,k-j)+m*m);
		}
	}
	for(int i=1;i<=m/2;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[n][m][k]=min(dp[n][m][k],DFS(n,m-i,j)+DFS(n,i,k-j)+n*n);
		}
	}
	return dp[n][m][k];
}
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>q;
	while(q--)
	{
		cin>>n>>m>>k;
		for(int i=0;i<=30;i++)
		for(int j=0;j<=30;j++)
		for(int t=0;t<=50;t++) dp[i][j][t]=inf;
		cout<<DFS(n,m,k)<<"\n";
	}
}


#include<bits/stdc++.h>
#define int long long
#pragma O3
using namespace std;
const int inf=1e9+7;
int n,q,k,m,dp[35][35][55];
int DFS(int n,int m,int k)
{
	if(dp[n][m][k]!=inf) return dp[n][m][k];
	if(k==n*m||k==0) return dp[n][m][k]=0;
	if(k>n*m) return dp[n][m][k]=inf;
	for(int i=1;i<=n/2;i++)
	{
		for(int j=0;j<=k;j++)
		{
			dp[m][n][k]=dp[n][m][k]=min(dp[n][m][k],DFS(n-i,m,j)+DFS(i,m,k-j)+m*m);
		}
	}
	for(int i=1;i<=m/2;i++)
	{
		for(int j=0;j<=k;j++)
		{
			dp[m][n][k]=dp[n][m][k]=min(dp[n][m][k],DFS(n,m-i,j)+DFS(n,i,k-j)+n*n);
		}
	}
	return dp[n][m][k];
}
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>q;
	while(q--)
	{
		cin>>n>>m>>k;
		for(int i=0;i<=30;i++)
		for(int j=0;j<=30;j++)
		for(int t=0;t<=50;t++) dp[i][j][t]=inf;
		cout<<DFS(n,m,k)<<"\n";
	}
}


#include<bits/stdc++.h>
#define int long long
#pragma O3
using namespace std;
const int inf=1e9+7;
int n,q,k,m,dp[35][35][55];
int DFS(int n,int m,int k)
{
	int res=inf;
	if(dp[n][m][k]!=0||n*m==k||k==0) return dp[n][m][k];
	if(k>n*m) return dp[n][m][k]=inf;
	for(int i=1;i<=n/2;i++)
	{
		for(int j=0;j<=k;j++)
		{
			res=min(res,DFS(n-i,m,j)+DFS(i,m,k-j)+m*m);
		}
	}
	for(int i=1;i<=m/2;i++)
	{
		for(int j=0;j<=k;j++)
		{
			res=min(res,DFS(n,m-i,j)+DFS(n,i,k-j)+n*n);
		}
	}
	return dp[m][n][k]=dp[n][m][k]=res;
}
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>q;
	while(q--)
	{
		cin>>n>>m>>k;
		cout<<DFS(n,m,k)<<"\n";
	}
}


#include<bits/stdc++.h>
#define int long long
#pragma O3
using namespace std;
const int inf=1e9+7;
int n,q,k,m,dp[35][35][55];
int DFS(int n,int m,int k)
{
	if(dp[n][m][k]!=inf) return dp[n][m][k];
	if(k==n*m||k==0) return dp[n][m][k]=0;
	if(k>n*m) return dp[n][m][k]=inf;
	for(int i=1;i<=n/2;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[m][n][k]=dp[n][m][k]=min(dp[n][m][k],DFS(n-i,m,j)+DFS(i,m,k-j)+m*m);
		}
	}
	for(int i=1;i<=m/2;i++)
	{
		for(int j=1;j<=k;j++)
		{
			dp[m][n][k]=dp[n][m][k]=min(dp[n][m][k],DFS(n,m-i,j)+DFS(n,i,k-j)+n*n);
		}
	}
	return dp[n][m][k];
}
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>q;
	while(q--)
	{
		cin>>n>>m>>k;
		for(int i=0;i<=30;i++)
		for(int j=0;j<=30;j++)
		for(int t=0;t<=50;t++) dp[i][j][t]=inf;
		cout<<DFS(n,m,k)<<"\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,n) for(int i=0;i<n;i++)
#define lop(i,a,b) for(int i=a;i<=b;i++)
#define int long long int
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pii;
#define mp make_pair
#define sz(a) int((a).size())
#define pb push_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define tr(container, it) for(auto it = container.begin(); it != container.end(); it++)
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define so(v) sort(all(v))
#define sod(v) sort(allr(v))
#define mfind(c, element) (c.find(element) != c.end())
#define gfind(c, element) (find(all(c),element) != c.end())
#define gfindindex(c, element) (find(all(c),element)-c.begin())
#define vp(x) cout << #x << " --> " << x << endl;
#define cp1(v) cout << #v << "--->" << endl; for (auto it : v)  cout << it << " "; cout << endl;
#define cpp1(v) cout << #v << "-->" << endl; for(auto it:v){cout << it.fi << " " << it.se << endl;}
#define ccp1(v) cout << #v << "--->" << endl;for (auto it:v){for(auto it1 : it){cout << it1 << " ";}cout<< endl;}
#define bsnp np3(low,high,mid);
#define hmm1 high=mid-1;
#define lmp1 low=mid+1;
#define hmp1 high=mid+1;
#define lmm1 low=mid-1;
#define fi first
#define se second
#define endl "\n"
#define np1(x) cout<<#x<<"="<<x<<'\n'
#define np2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define np3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
// upper_bound(all(a),k,greater<int>()) gives first no smaller(not greater) than k

struct cmp {
	bool operator() (const pair<int, int> &a, const pair<int, int> &b) const {
		int lena = a.second - a.first + 1;
		int lenb = b.second - b.first + 1;
		if (lena == lenb) return a.first < b.first;
		return lena > lenb;
	}
};//comparison used for sorting set<pair<i,i>,cmp>
bool myComparison(const pair<int,int> &a,const pair<int,int> &b)
{
       return a.second>b.second;
//increasing ordr basedd on 2nd
};
const int N=52;
int n,m,k;
int dp[N][N][N];        //dp[n][m][k]
int F(int x,int y,int k)
{
    if(k==0||x*y==k) return 0;

    if(dp[x][y][k]==-1)
    {
        int ans=1e18;
        for(int i=1;i<=x-i;i++)                 //cut the piece at x=i (2 pieces now of sz i and x-i)
        {
            for(int j=0;j<=k;j++)               //take j from one piece and (k-j) from the other piece
            {
                ans=min(ans,y*y + F(i,y,j) +F(x-i,y,k-j));
            }
        }

        for(int i=1;i<=y-i;i++)
        {
            for(int j=0;j<=k;j++)
            {
                ans=min(ans,x*x + F(x,i,j) +F(x,y-i,k-j));
            }
        }

        dp[x][y][k]=ans;
    }
    return dp[x][y][k];
}
main()
{
IOS;
int t;cin>>t;
while(t--)
{
    cin>>n>>m>>k;
    loop(i1,n+1)
    loop(i2,m+1)
    loop(i3,k+1)
    dp[i1][i2][i3]=-1;

    cout<<F(n,m,k)<<endl;
}
return 0;
}


/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
*/
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,n) for(int i=0;i<n;i++)
#define lop(i,a,b) for(int i=a;i<=b;i++)
#define int long long int
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pii;
#define mp make_pair
#define sz(a) int((a).size())
#define pb push_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define tr(container, it) for(auto it = container.begin(); it != container.end(); it++)
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define so(v) sort(all(v))
#define sod(v) sort(allr(v))
#define mfind(c, element) (c.find(element) != c.end())
#define gfind(c, element) (find(all(c),element) != c.end())
#define gfindindex(c, element) (find(all(c),element)-c.begin())
#define vp(x) cout << #x << " --> " << x << endl;
#define cp1(v) cout << #v << "--->" << endl; for (auto it : v)  cout << it << " "; cout << endl;
#define cpp1(v) cout << #v << "-->" << endl; for(auto it:v){cout << it.fi << " " << it.se << endl;}
#define ccp1(v) cout << #v << "--->" << endl;for (auto it:v){for(auto it1 : it){cout << it1 << " ";}cout<< endl;}
#define bsnp np3(low,high,mid);
#define hmm1 high=mid-1;
#define lmp1 low=mid+1;
#define hmp1 high=mid+1;
#define lmm1 low=mid-1;
#define fi first
#define se second
#define endl "\n"
#define np1(x) cout<<#x<<"="<<x<<'\n'
#define np2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define np3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
// upper_bound(all(a),k,greater<int>()) gives first no smaller(not greater) than k

struct cmp {
	bool operator() (const pair<int, int> &a, const pair<int, int> &b) const {
		int lena = a.second - a.first + 1;
		int lenb = b.second - b.first + 1;
		if (lena == lenb) return a.first < b.first;
		return lena > lenb;
	}
};//comparison used for sorting set<pair<i,i>,cmp>
bool myComparison(const pair<int,int> &a,const pair<int,int> &b)
{
       return a.second>b.second;
//increasing ordr basedd on 2nd
};
const int N=52;
int n,m,k;
int dp[N][N][N];        //dp[n][m][k]
int F(int x,int y,int k)
{
    if(k==0||x*y==k) return 0;

    if(dp[x][y][k]==0)
    {
        int ans=1e18;
        for(int i=1;i<=x-i;i++)                 //cut the piece at x=i (2 pieces now of sz i and x-i)
        {
            for(int j=0;j<=k;j++)               //take j from one piece and (k-j) from the other piece
            {
                ans=min(ans,y*y + F(i,y,j) +F(x-i,y,k-j));
            }
        }

        for(int i=1;i<=y-i;i++)
        {
            for(int j=0;j<=k;j++)
            {
                ans=min(ans,x*x + F(x,i,j) +F(x,y-i,k-j));
            }
        }

        dp[x][y][k]=ans;
    }
    return dp[x][y][k];
}
main()
{
IOS;
int t;cin>>t;
while(t--)
{
    cin>>n>>m>>k;

    cout<<F(n,m,k)<<endl;
}
return 0;
}


/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
*/
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,n) for(int i=0;i<n;i++)
#define lop(i,a,b) for(int i=a;i<=b;i++)
#define int long long int
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pii;
#define mp make_pair
#define sz(a) int((a).size())
#define pb push_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define tr(container, it) for(auto it = container.begin(); it != container.end(); it++)
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define so(v) sort(all(v))
#define sod(v) sort(allr(v))
#define mfind(c, element) (c.find(element) != c.end())
#define gfind(c, element) (find(all(c),element) != c.end())
#define gfindindex(c, element) (find(all(c),element)-c.begin())
#define vp(x) cout << #x << " --> " << x << endl;
#define cp1(v) cout << #v << "--->" << endl; for (auto it : v)  cout << it << " "; cout << endl;
#define cpp1(v) cout << #v << "-->" << endl; for(auto it:v){cout << it.fi << " " << it.se << endl;}
#define ccp1(v) cout << #v << "--->" << endl;for (auto it:v){for(auto it1 : it){cout << it1 << " ";}cout<< endl;}
#define bsnp np3(low,high,mid);
#define hmm1 high=mid-1;
#define lmp1 low=mid+1;
#define hmp1 high=mid+1;
#define lmm1 low=mid-1;
#define fi first
#define se second
#define endl "\n"
#define np1(x) cout<<#x<<"="<<x<<'\n'
#define np2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define np3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
// upper_bound(all(a),k,greater<int>()) gives first no smaller(not greater) than k

struct cmp {
	bool operator() (const pair<int, int> &a, const pair<int, int> &b) const {
		int lena = a.second - a.first + 1;
		int lenb = b.second - b.first + 1;
		if (lena == lenb) return a.first < b.first;
		return lena > lenb;
	}
};//comparison used for sorting set<pair<i,i>,cmp>
bool myComparison(const pair<int,int> &a,const pair<int,int> &b)
{
       return a.second>b.second;
//increasing ordr basedd on 2nd
};
const int N=52;
int n,m,k;
int dp[N][N][N];        //dp[n][m][k]
int F(int x,int y,int k)
{
    if(x*y<k) return 1e18;
    if(k==0||x*y==k) return 0;

    if(dp[x][y][k]==0)
    {
        int ans=1e18;
        for(int i=1;i<=x-i;i++)                 //cut the piece at x=i (2 pieces now of sz i and x-i)
        {
            for(int j=0;j<=k;j++)               //take j from one piece and (k-j) from the other piece
            {
                ans=min(ans,y*y + F(i,y,j) +F(x-i,y,k-j));
            }
        }

        for(int i=1;i<=y-i;i++)
        {
            for(int j=0;j<=k;j++)
            {
                ans=min(ans,x*x + F(x,i,j) +F(x,y-i,k-j));
            }
        }

        dp[x][y][k]=ans;
    }
    return dp[x][y][k];
}
main()
{
IOS;
int t;cin>>t;
while(t--)
{
    cin>>n>>m>>k;

    cout<<F(n,m,k)<<endl;
}
return 0;
}


/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
*/
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,n) for(int i=0;i<n;i++)
#define lop(i,a,b) for(int i=a;i<=b;i++)
#define int long long int
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pii;
#define mp make_pair
#define sz(a) int((a).size())
#define pb push_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define tr(container, it) for(auto it = container.begin(); it != container.end(); it++)
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define so(v) sort(all(v))
#define sod(v) sort(allr(v))
#define mfind(c, element) (c.find(element) != c.end())
#define gfind(c, element) (find(all(c),element) != c.end())
#define gfindindex(c, element) (find(all(c),element)-c.begin())
#define vp(x) cout << #x << " --> " << x << endl;
#define cp1(v) cout << #v << "--->" << endl; for (auto it : v)  cout << it << " "; cout << endl;
#define cpp1(v) cout << #v << "-->" << endl; for(auto it:v){cout << it.fi << " " << it.se << endl;}
#define ccp1(v) cout << #v << "--->" << endl;for (auto it:v){for(auto it1 : it){cout << it1 << " ";}cout<< endl;}
#define bsnp np3(low,high,mid);
#define hmm1 high=mid-1;
#define lmp1 low=mid+1;
#define hmp1 high=mid+1;
#define lmm1 low=mid-1;
#define fi first
#define se second
#define endl "\n"
#define np1(x) cout<<#x<<"="<<x<<'\n'
#define np2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define np3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
// upper_bound(all(a),k,greater<int>()) gives first no smaller(not greater) than k

struct cmp {
	bool operator() (const pair<int, int> &a, const pair<int, int> &b) const {
		int lena = a.second - a.first + 1;
		int lenb = b.second - b.first + 1;
		if (lena == lenb) return a.first < b.first;
		return lena > lenb;
	}
};//comparison used for sorting set<pair<i,i>,cmp>
bool myComparison(const pair<int,int> &a,const pair<int,int> &b)
{
       return a.second>b.second;
//increasing ordr basedd on 2nd
};
const int N=52;
int n,m,k;
int dp[N][N][N];        //dp[n][m][k]
int F(int x,int y,int k)
{
    if(x*y<k) return 1e18;
    if(k==0||x*y==k) return 0;

    if(dp[x][y][k]==-1)
    {
        int ans=1e18;
        for(int i=1;i<=x-i;i++)                 //cut the piece at x=i (2 pieces now of sz i and x-i)
        {
            for(int j=0;j<=k;j++)               //take j from one piece and (k-j) from the other piece
            {
                ans=min(ans,y*y + F(i,y,j) +F(x-i,y,k-j));
            }
        }

        for(int i=1;i<=y-i;i++)
        {
            for(int j=0;j<=k;j++)
            {
                ans=min(ans,x*x + F(x,i,j) +F(x,y-i,k-j));
            }
        }

        dp[x][y][k]=ans;
    }
    return dp[x][y][k];
}
main()
{
IOS;
int t;cin>>t;
while(t--)
{
    cin>>n>>m>>k;
    loop(i1,n+1)
    loop(i2,m+1)
    loop(i3,k+1)
    dp[i1][i2][i3]=-1;

    cout<<F(n,m,k)<<endl;
}
return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,n) for(int i=0;i<n;i++)
#define lop(i,a,b) for(int i=a;i<=b;i++)
#define int long long int
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pii;
#define mp make_pair
#define sz(a) int((a).size())
#define pb push_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define tr(container, it) for(auto it = container.begin(); it != container.end(); it++)
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define so(v) sort(all(v))
#define sod(v) sort(allr(v))
#define mfind(c, element) (c.find(element) != c.end())
#define gfind(c, element) (find(all(c),element) != c.end())
#define gfindindex(c, element) (find(all(c),element)-c.begin())
#define vp(x) cout << #x << " --> " << x << endl;
#define cp1(v) cout << #v << "--->" << endl; for (auto it : v)  cout << it << " "; cout << endl;
#define cpp1(v) cout << #v << "-->" << endl; for(auto it:v){cout << it.fi << " " << it.se << endl;}
#define ccp1(v) cout << #v << "--->" << endl;for (auto it:v){for(auto it1 : it){cout << it1 << " ";}cout<< endl;}
#define bsnp np3(low,high,mid);
#define hmm1 high=mid-1;
#define lmp1 low=mid+1;
#define hmp1 high=mid+1;
#define lmm1 low=mid-1;
#define fi first
#define se second
#define endl "\n"
#define np1(x) cout<<#x<<"="<<x<<'\n'
#define np2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define np3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
// upper_bound(all(a),k,greater<int>()) gives first no smaller(not greater) than k

struct cmp {
	bool operator() (const pair<int, int> &a, const pair<int, int> &b) const {
		int lena = a.second - a.first + 1;
		int lenb = b.second - b.first + 1;
		if (lena == lenb) return a.first < b.first;
		return lena > lenb;
	}
};//comparison used for sorting set<pair<i,i>,cmp>
bool myComparison(const pair<int,int> &a,const pair<int,int> &b)
{
       return a.second>b.second;
//increasing ordr basedd on 2nd
};
const int N=52;
int n,m,k;
int dp[N][N][N];        //dp[n][m][k]
int F(int x,int y,int k)
{
    if(k==0||x*y==k) return 0;

    if(dp[x][y][k]==-1)
    {
        int ans=1e18;
        for(int i=1;i<=x-i;i++)                 //cut the piece at x=i (2 pieces now of sz i and x-i)
        {
            for(int j=0;j<=k;j++)               //take j from one piece and (k-j) from the other piece
            {
                ans=min(ans,y*y + F(i,y,j) +F(x-i,y,k-j));
            }
        }

        for(int i=1;i<=y-i;i++)
        {
            for(int j=0;j<=k;j++)
            {
                ans=min(ans,x*x + F(x,i,j) +F(x,y-i,k-j));
            }
        }

        dp[x][y][k]=ans;
    }
    return dp[x][y][k];
}
main()
{
IOS;
int t;cin>>t;
while(t--)
{
    cin>>n>>m>>k;
    loop(i1,n+1)
    loop(i2,m+1)
    loop(i3,k+1)
    dp[i1][i2][i3]=-1;

    cout<<F(n,m,k)<<endl;
}
return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,n) for(int i=0;i<n;i++)
#define lop(i,a,b) for(int i=a;i<=b;i++)
#define int long long int
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > pii;
#define mp make_pair
#define sz(a) int((a).size())
#define pb push_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define tr(container, it) for(auto it = container.begin(); it != container.end(); it++)
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define so(v) sort(all(v))
#define sod(v) sort(allr(v))
#define mfind(c, element) (c.find(element) != c.end())
#define gfind(c, element) (find(all(c),element) != c.end())
#define gfindindex(c, element) (find(all(c),element)-c.begin())
#define vp(x) cout << #x << " --> " << x << endl;
#define cp1(v) cout << #v << "--->" << endl; for (auto it : v)  cout << it << " "; cout << endl;
#define cpp1(v) cout << #v << "-->" << endl; for(auto it:v){cout << it.fi << " " << it.se << endl;}
#define ccp1(v) cout << #v << "--->" << endl;for (auto it:v){for(auto it1 : it){cout << it1 << " ";}cout<< endl;}
#define bsnp np3(low,high,mid);
#define hmm1 high=mid-1;
#define lmp1 low=mid+1;
#define hmp1 high=mid+1;
#define lmm1 low=mid-1;
#define fi first
#define se second
#define endl "\n"
#define np1(x) cout<<#x<<"="<<x<<'\n'
#define np2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define np3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
// upper_bound(all(a),k,greater<int>()) gives first no smaller(not greater) than k

struct cmp {
	bool operator() (const pair<int, int> &a, const pair<int, int> &b) const {
		int lena = a.second - a.first + 1;
		int lenb = b.second - b.first + 1;
		if (lena == lenb) return a.first < b.first;
		return lena > lenb;
	}
};//comparison used for sorting set<pair<i,i>,cmp>
bool myComparison(const pair<int,int> &a,const pair<int,int> &b)
{
       return a.second>b.second;
//increasing ordr basedd on 2nd
};
const int N=52;
int n,m,k;
int dp[N][N][N];        //dp[n][m][k]
int F(int x,int y,int k)
{
    if(x*y<k) return 1e18;
    if(k==0||x*y==k) return 0;

    if(dp[x][y][k]==-1)
    {
        int ans=1e18;
        for(int i=1;i<=x-i;i++)                 //cut the piece at x=i (2 pieces now of sz i and x-i)
        {
            for(int j=0;j<=k;j++)               //take j from one piece and (k-j) from the other piece
            {
                ans=min(ans,y*y + F(i,y,j) +F(x-i,y,k-j));
            }
        }

        for(int i=1;i<=y-i;i++)
        {
            for(int j=0;j<=k;j++)
            {
                ans=min(ans,x*x + F(x,i,j) +F(x,y-i,k-j));
            }
        }

        dp[x][y][k]=ans;
    }
    return dp[x][y][k];
}
main()
{
IOS;
int t;cin>>t;
while(t--)
{
    cin>>n>>m>>k;
    loop(i1,n+1)
    loop(i2,m+1)
    loop(i3,k+1)
    dp[i1][i2][i3]=-1;

    cout<<F(n,m,k)<<endl;
}
return 0;
}