Problem: Snuke has found two trees A and B, each with N vertices numbered 1 to N. The i-th edge of A connects Vertex a_i and b_i, and the j-th edge of B connects Vertex c_j and d_j. Also, all vertices of A are initially painted white.

Snuke would like to perform the following operation on A zero or more times so that A coincides with B:

* Choose a leaf vertex that is painted white. (Let this vertex be v.)
* Remove the edge incident to v, and add a new edge that connects v to another vertex.
* Paint v black.



Determine if A can be made to coincide with B, ignoring color. If the answer is yes, find the minimum number of operations required.

You are given T cases of this kind. Find the answer for each of them.

Constraints

* 1 \leq T \leq 20
* 3 \leq N \leq 50
* 1 \leq a_i,b_i,c_i,d_i \leq N
* All given graphs are trees.

Input

Input is given from Standard Input in the following format:


T
case_1
:
case_{T}


Each case is given in the following format:


N
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 d_1
:
c_{N-1} d_{N-1}


Output

For each case, if A can be made to coincide with B ignoring color, print the minimum number of operations required, and print `-1` if it cannot.

Examples

Input

2
3
1 2
2 3
1 3
3 2
6
1 2
2 3
3 4
4 5
5 6
1 2
2 4
4 3
3 5
5 6


Output

1
-1


Input

3
8
2 7
4 8
8 6
7 1
7 3
5 7
7 8
4 2
5 2
1 2
8 1
3 2
2 6
2 7
4
1 2
2 3
3 4
3 4
2 1
3 2
9
5 3
4 3
9 3
6 8
2 3
1 3
3 8
1 7
4 1
2 8
9 6
3 6
3 5
1 8
9 7
1 6


Output

6
0
7

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int T;
int N;
set<int> A[51],B[51];
vector<int> E[51];
int ret,numng;
int P[2][51],D[2][51],NG[51];

class SCC {
public:
	static const int MV = 60;
	vector<vector<int> > SC; int NV,GR[MV];
private:
	vector<int> E[MV], RE[MV], NUM; int vis[MV];
public:
	void init(int NV) { this->NV=NV; for(int i=0;i<MV;i++) { E[i].clear(); RE[i].clear();}}
	void add_edge(int x,int y) { E[x].push_back(y); RE[y].push_back(x); }
	void dfs(int cu) { vis[cu]=1; for(int i=0;i<E[cu].size();i++) if(!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
	void revdfs(int cu, int ind) { int i; vis[cu]=1; GR[cu]=ind; SC[ind].push_back(cu);
		FOR(i,RE[cu].size()) if(!vis[RE[cu][i]]) revdfs(RE[cu][i],ind);}
	void scc() {
		int c=0; SC.clear(); SC.resize(MV); NUM.clear();
		ZERO(vis); for(int i=0;i<NV;i++) if(!vis[i]) dfs(i);
		ZERO(vis); for(int i=NUM.size()-1;i>=0;i--) if(!vis[NUM[i]]){
			SC[c].clear(); revdfs(NUM[i],c); sort(SC[c].begin(),SC[c].end()); c++;
		}
		SC.resize(c);
	}
};

SCC sc;

void dfsa(int cur,int pre) {
	P[0][cur]=pre;
	if(cur!=pre) D[0][cur]=D[0][pre]+1;
	if(P[0][cur]==P[1][cur] && (cur==pre || NG[pre]==0)) NG[cur]=0, numng--;
	FORR(e,A[cur]) if(e!=pre) dfsa(e,cur);
}

void dfsb(int cur,int pre) {
	P[1][cur]=pre;
	if(cur!=pre) D[1][cur]=D[1][pre]+1;
	FORR(e,B[cur]) if(e!=pre) dfsb(e,cur);
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>T;
	while(T--) {
		cin>>N;
		FOR(i,N) A[i].clear(), B[i].clear();
		FOR(i,N-1) {
			cin>>x>>y;
			A[x-1].insert(y-1);
			A[y-1].insert(x-1);
		}
		FOR(i,N-1) {
			cin>>x>>y;
			B[x-1].insert(y-1);
			B[y-1].insert(x-1);
		}
		ret=1010;
		FOR(i,N) {
			numng=N;
			FOR(j,N) NG[j]=1;
			ZERO(D);
			dfsb(i,i);
			dfsa(i,i);
			
			sc.init(N);
			FOR(j,N) if(NG[j]) {
				FORR(e,A[j]) if(NG[e] && D[0][e]>D[0][j]) sc.add_edge(e,j);
				FORR(e,B[j]) if(NG[e] && D[1][e]>D[1][j]) sc.add_edge(j,e);
			}
			sc.scc();
			if(sc.SC.size()==N) ret=min(ret,numng);
		}
		
		FOR(i,N) if(A[i].size()==1) {
			y=*A[i].begin();
			A[i].erase(y);
			A[y].erase(i);
			FOR(x,N) if(i!=x) {
				A[i].insert(x);
				A[x].insert(i);
				
				numng=N+1;
				FOR(j,N) NG[j]=1;
				ZERO(D);
				dfsb(i,i);
				dfsa(i,i);
				
				sc.init(N);
				FOR(j,N) if(NG[j]) {
					FORR(e,A[j]) if(NG[e] && D[0][e]>D[0][j]) sc.add_edge(e,j);
					FORR(e,B[j]) if(NG[e] && D[1][e]>D[1][j]) sc.add_edge(j,e);
				}
				sc.scc();
				if(sc.SC.size()==N) ret=min(ret,numng);
				A[i].erase(x);
				A[x].erase(i);
			}
			A[i].insert(y);
			A[y].insert(i);
		}
		
		if(ret==1010) ret=-1;
		cout<<ret<<endl;
	}
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}