Under construction

Still being built. Things may change

Anyone Can Solve It

Time limit2sMemory limit1024 MB

Summary
Given sequences A and B of digits 1 to 9, decide whether B can be reached from A by appending digits, where an even digit first sorts the last three elements.
Level

Medium6 of 10

Topics
Dynamic programming, Simulation, Implementation
Solved
No attempts yet

Problem

Geonmo has been playing with a sequence and decided to turn it into a game.

The game goes as follows.

  1. Geonmo starts with sequence AA.
  2. Geonmo appends a number of his choice from 11 to 99 to the end of the sequence. However, if the number to append is even, he must first sort the last three numbers of the sequence in ascending order, and then append the number.
  3. If Geonmo can build sequence BB, he wins the game. If he cannot build it no matter what, he loses.

Geonmo lives by the motto 'if you try, you can do it,' but he will not even attempt something that is impossible.

Print whether Geonmo wins the game.

Input

The first line contains the number of test cases TT. (1≤T≤5 0001 \le T \le 5\,000)

Each test case starts with a line containing NN and MM, the lengths of sequence AA and sequence BB, separated by a space. (3≤N≤M≤5 0003 \le N \le M \le 5\,000)

The second line of each test case contains A1,A2,⋯ ,ANA_1, A_2, \cdots, A_N, separated by spaces. (1≤Ai≤91 \le A_i \le 9)

The third line of each test case contains B1,B2,⋯ ,BMB_1, B_2, \cdots, B_M, separated by spaces. (1≤Bi≤91 \le B_i \le 9)

The sum of MM over all test cases does not exceed 15 00015\,000.

All numbers are integers.

Output

For each test case, print the answer on its own line. Print YES if Geonmo wins and NO if he loses. Letter case does not matter, so yEs, yes, and YES are all accepted.

Examples1

  1. Example 1

    Input
    3
    5 8
    1 4 7 3 4
    1 4 3 4 3 7 8 2
    3 5
    1 2 3
    1 2 3 4 5
    3 5
    5 4 3
    5 4 3 2 1
    
    Expected output
    YES
    YES
    NO