As a punishment for misbehaving, Byteasar must evaluate a mysterious Boolean-valued function F(x,y), defined for a pair of positive-integer sequences x=(x1,…,xn) and y=(y1,…,ym) as follows:
boolean F(x, y)
if W(x) != W(y): return 0
else if |W(x)| = |W(y)| = 1: return 1
else: return F(p(x), p(y)) AND F(s(x), s(y))
Here:
For example, for x=(2,3,7,2,7,4,7,2,4) we have W(x)={2,3,4,7}, p(x)=(2,3,7,2,7), and s(x)=(7,2,7,4,7,2,4).
Evaluating F straight from its definition is hopelessly slow on large inputs, so you must compute it as fast as possible. Read several pairs of sequences (x,y) and print the value of F(x,y) for each pair.
The first line contains one integer k (1≤k≤13), the number of sequence pairs to analyse. The pairs follow on the next 3k lines. For each pair, the first line holds two integers n and m (1≤n,m≤100,000), the lengths of x and y. The second line holds the n integers xi (1≤xi≤100) forming x, and the third line holds the m integers yi (1≤yi≤100) forming y, each separated by single spaces.
Print exactly k lines. The i-th line (1≤i≤k) contains a single integer, 0 or 1, equal to the value of F(x,y) for the i-th pair.