#include<bits/stdc++.h>
using namespace std;
int t, n, x;
vector<int>v[100005], w[100005];
bool dfs ( int a, int b )
{
if ( v[a].size() == 0 && w[b].size() == 0 )
return true;
else
if ( v[a].size() != w[b].size() )
return false;
else
if ( v[a].size() == 1 )
return dfs ( v[a][0], w[b][0] );
else
return ( ( dfs ( v[a][0], w[b][0] ) && dfs ( v[a][1], w[b][1] ) ) || ( dfs ( v[a][0], w[b][1] ) && dfs ( v[a][1], w[b][0] ) ) );
}
int main()
{
scanf ( "%d", &t );
while ( t-- )
{
scanf ( "%d", &n );
for ( int i = 0; i < 100005; i++ )
{
v[i].clear();
w[i].clear();
}
for ( int i = 1; i <= n; i++ )
{
scanf ( "%d", &x );
v[x].push_back ( i );
}
for ( int i = 1; i <= n; i++ )
{
scanf ( "%d", &x );
w[x].push_back ( i );
}
cout << dfs ( 0, 0 ) << endl;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IHQsIG4sIHg7CnZlY3RvcjxpbnQ+dlsxMDAwMDVdLCB3WzEwMDAwNV07CmJvb2wgZGZzICggaW50IGEsIGludCBiICkKewogICAgaWYgKCB2W2FdLnNpemUoKSA9PSAwICYmIHdbYl0uc2l6ZSgpID09IDAgKQogICAgICAgIHJldHVybiB0cnVlOwogICAgZWxzZQogICAgICAgIGlmICggdlthXS5zaXplKCkgIT0gd1tiXS5zaXplKCkgKQogICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAgICAgZWxzZQogICAgICAgICAgICBpZiAoIHZbYV0uc2l6ZSgpID09IDEgKQogICAgICAgICAgICAgICAgcmV0dXJuIGRmcyAoIHZbYV1bMF0sIHdbYl1bMF0gKTsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgcmV0dXJuICggKCBkZnMgKCB2W2FdWzBdLCB3W2JdWzBdICkgJiYgZGZzICggdlthXVsxXSwgd1tiXVsxXSApICkgfHwgKCBkZnMgKCB2W2FdWzBdLCB3W2JdWzFdICkgJiYgZGZzICggdlthXVsxXSwgd1tiXVswXSApICkgKTsKfQppbnQgbWFpbigpCnsKICAgIHNjYW5mICggIiVkIiwgJnQgKTsKICAgIHdoaWxlICggdC0tICkKICAgIHsKICAgICAgICBzY2FuZiAoICIlZCIsICZuICk7CiAgICAgICAgZm9yICggaW50IGkgPSAwOyBpIDwgMTAwMDA1OyBpKysgKQogICAgICAgIHsKICAgICAgICAgICAgdltpXS5jbGVhcigpOwogICAgICAgICAgICB3W2ldLmNsZWFyKCk7CiAgICAgICAgfQogICAgICAgIGZvciAoIGludCBpID0gMTsgaSA8PSBuOyBpKysgKQogICAgICAgIHsKICAgICAgICAgICAgc2NhbmYgKCAiJWQiLCAmeCApOwogICAgICAgICAgICB2W3hdLnB1c2hfYmFjayAoIGkgKTsKICAgICAgICB9CiAgICAgICAgZm9yICggaW50IGkgPSAxOyBpIDw9IG47IGkrKyApCiAgICAgICAgewogICAgICAgICAgICBzY2FuZiAoICIlZCIsICZ4ICk7CiAgICAgICAgICAgIHdbeF0ucHVzaF9iYWNrICggaSApOwogICAgICAgIH0KICAgICAgICBjb3V0IDw8IGRmcyAoIDAsIDAgKSA8PCBlbmRsOwogICAgfQp9Cg==