Robots

Decide whether some sequence of red and green moves brings robots starting on every intersection together onto one intersection.

Medium7GraphBFSNo attempts yetTime limit2sMemory limit256 MB

Problem

While you were not watching, your NN robots started moving on their own and spread across your hometown. The town has NN intersections numbered 0 through N1N-1, and exactly one robot stands on each intersection. Intersection ii carries one red signpost that points at an intersection riir_i \neq i and one green signpost that points at an intersection giig_i \neq i.

When you press the red button on the remote control, every robot moves along the red signpost at the same time: a robot on intersection ii moves to rir_i. When you press the green button, every robot moves to gig_i instead. Write a program that decides whether some sequence of button presses brings all NN robots to the same intersection at the same time.

Input

The first line contains the number of data sets PP (1P5001 \le P \le 500). The data sets are independent and are processed the same way.

Each data set consists of three lines.

  • The first line contains the data set number KK and the number of intersections NN. The data sets are numbered 1 through PP in order.
  • The second line contains r0,,rN1r_0, \ldots, r_{N-1} separated by spaces (0riN10 \le r_i \le N-1, riir_i \neq i).
  • The third line contains g0,,gN1g_0, \ldots, g_{N-1} separated by spaces (0giN10 \le g_i \le N-1, giig_i \neq i).

2N5002 \le N \le 500, and the sum of NN over all data sets is at most 2000. Both signposts on an intersection may point the same way, so ri=gir_i = g_i is possible.

Output

Print one line per data set. The line contains the data set number KK, one space, then YES if you can gather every robot on one intersection, or NO otherwise.

Hint

In the second data set of the example, pressing GREEN, RED, RED, GREEN gathers every robot on intersection 2.