atcoder#ARC154C. [ARC154C] Roller

[ARC154C] Roller

Score : 500500 points

Problem Statement

You are given sequences of positive integers of length NN: A=(A1,A2,,AN)A=(A_1,A_2,\dots,A_N) and B=(B1,B2,,BN)B=(B_1,B_2,\dots,B_N).

You can repeat the following operation any number of times (possibly zero).

  • Choose an integer ii such that 1iN1 \le i \le N and replace AiA_i with Ai+1A_{i+1}.

Here, regard AN+1A_{N+1} as A1A_1.

Determine whether it is possible to make AA equal BB.

You have TT test cases to solve.

Constraints

  • 1T50001 \le T \le 5000
  • 1N50001 \le N \le 5000
  • 1Ai,BiN1 \le A_i,B_i \le N
  • For each input file, the sum of NN over all test cases does not exceed 50005000.

Input

The input is given from Standard Input in the following format:

TT

case1\mathrm{case}_1

case2\mathrm{case}_2

\vdots

caseT\mathrm{case}_T

Each test case is in the following format:

NN

A1A_1 A2A_2 \dots ANA_N

B1B_1 B2B_2 \dots BNB_N

Output

Print TT lines. The ii-th line should contain Yes if it is possible to make AA equal BB in the ii-th test case, and No otherwise.

3
2
1 2
2 2
4
2 3 1 1
2 1 1 2
2
1 1
2 2
Yes
Yes
No

In the first test case, you can make AA equal BB as follows.

  • Choose i=1i=1 to replace A1A_1 with A2A_2, making A=(2,2)A=(2,2).

In the second test case, you can make AA equal BB as follows.

  • Choose i=4i=4 to replace A4A_4 with A1A_1, making A=(2,3,1,2)A=(2,3,1,2).
  • Choose i=2i=2 to replace A2A_2 with A3A_3, making A=(2,1,1,2)A=(2,1,1,2).

In the third test case, there is no way to make AA equal BB.