
A deterministic finite automaton (DFA) is a finite state machine that accepts or rejects finite input strings. The figure above draws one DFA as a state diagram. This automaton has three states S0, S1 and S2, drawn as circles, and it reads a finite sequence of 0s and 1s as input. Every state has one outgoing arrow for 0 and one outgoing arrow for 1, so on reading a symbol the automaton moves from its current state to the state that arrow points at. If the automaton is in state S0 and the current symbol is 1, it moves to S1, written δ(S0,1)=S1. Extending the notation to whole strings gives statements such as δ(S0,10)=S2 and δ(S1,011010)=S0.
A DFA has a start state, drawn as an arrow coming in from nowhere, and a set of accept states, drawn as double circles. A DFA with start state q0 accepts a string w if and only if δ(q0,w) is an accept state. In the figure above S0 is both the start state and an accept state, and that DFA accepts exactly the binary numbers that are multiples of 3, the empty string included.
Now take a DFA D with states S0,S1,…,Sn−1. A string w is a history cleaner for D if δ(Si,w)=δ(Sj,w) holds for all i,j∈{0,…,n−1}. In other words, whichever state the run starts from, reading w leaves the DFA in one common state. D is history cleanable if a history cleaner exists for it. Given a DFA whose input is a sequence of 0s and 1s, decide whether it is history cleanable.
The first line contains one integer t, the number of test cases (1≤t≤500). Each test case starts with a line holding one integer n, the number of states of a DFA with states S0,S1,…,Sn−1 (2≤n≤500). The second line of the test case holds n space separated integers a0,a1,…,an−1 and the third line holds n space separated integers b0,b1,…,bn−1 (0≤ai,bi<n), which means δ(Si,0)=Sai and δ(Si,1)=Sbi for every i with 0≤i<n.
For each test case print one line with the answer for that DFA. Print YES if it is history cleanable, and NO otherwise.