Under construction

Still being built. Things may change

Days of the week

Interview

Time limit1sMemory limit1024 MB

Summary
Each button shifts its linked universes forward one weekday (mod 7). Decide whether every weekday configuration is reachable, and print an unreachable one if not.
Level

Medium5 of 10

Topics
Matrix, Math, Implementation
Solved
No attempts yet

Problem

The Architect has created a multiverse with MM universes. In each universe, the day of the week has been set individually. The Architect also has NN buttons. Each button is connected to a certain set of universes. Pressing a button shifts all of its connected universes one day forward.

The Architect is curious: is there such a configuration of weekdays in universes that it is unachievable by any sequence of pressing buttons? Help the Architect solve this problem.

All universes use the Earth week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. When shifting the day of the week forward, Monday changes to Tuesday, Tuesday to Wednesday, and so on, and Sunday changes to Monday.

The Architect can press any button any number of times and in any order. When a button is pressed, all its connected universes change instantly and simultaneously.

Input

The first line contains the integer TT, the number of tests (1≤T≤5001 \le T \le 500). Each test follows.

Each test begins with a line containing two integers NN and MM, the number of buttons and the number of universes (1≤N,M≤5001 \le N,M \le 500).

The second line of the test contains the initial day of the week in each universe. The universes are numbered from 1 through MM, and the days are written in the same order.

Then each button is described on its own line. The first number KK is the number of universes connected to the button (0≤K≤M0 \le K \le M), followed by the KK numbers of those universes. The defined numbers are different for each button.

The total number of buttons over all tests is at most 500, and the total number of universes over all tests is at most 500.

Output

For each test, print one answer on a separate line.

If all 7M7^M possible configurations of days of the week can be obtained, print NO. Otherwise print YES, followed by any unachievable configuration, with the days separated by spaces.

Examples1

  1. Example 1

    Input
    3
    3 3
    Monday Saturday Thursday
    1 1
    1 2
    1 3
    3 3
    Friday Thursday Thursday
    1 3
    1 3
    1 3
    4 3
    Sunday Sunday Monday
    2 1 3
    3 1 2 3
    0
    1 3
    
    Expected output
    NO
    YES Saturday Thursday Thursday
    NO