Under construction

Still being built. Things may change

Through Another Maze Darkly

Time limit8sMemory limit512 MB

Summary
Given a tree maze where each room has a rotating laser pointer, find the room reached from room 1 after exactly K corridor moves for each of Q queries.
Level

Hard9 of 10

Topics
Tree, Graph, Simulation
Solved
No attempts yet

Problem

There is a maze formed by connecting NN rooms with N−1N-1 corridors. The rooms are numbered 1 to NN, and each room is shaped like a circle. The corridors follow these rules:

  • Each corridor connects two distinct rooms.
  • Every pair of rooms is connected by exactly one path of corridors.

The lights are all out, so you cannot see where you are, which makes the maze hard to navigate. To help, each room contains a laser pointer that initially points to a corridor. Consider this strategy:

  • Rotate the room's laser pointer clockwise until it points to another corridor.
  • Follow the room's laser pointer and traverse the corridor.
  • Repeat the previous two steps indefinitely.

You created QQ queries to investigate this strategy. For each query, you are given an integer KK, and you start at room 1. Determine the final room after traversing exactly KK corridors. All laser pointers reset to their original orientation after each query.

Input

The first line contains the integers NN and QQ (2≤N≤800 0002 \le N \le 800\,000, 1≤Q≤800 0001 \le Q \le 800\,000).

The next NN lines describe the layout of the maze. Line ii describes room ii. It contains an integer kk, the number of corridors connected to room ii, followed by kk integers c1,c2,…,ckc_1, c_2, \ldots, c_k, the rooms these corridors lead to, in clockwise order. Room ii's laser pointer initially points to the corridor leading to room c1c_1.

The final QQ lines each contain an integer KK (1≤K≤10151 \le K \le 10^{15}).

Output

Output QQ lines, answering the queries in order.

Hint

This is the initial state of the maze.

The strategy visits these rooms in order: 1, 2, 1, 2, 4, 2, 3, ...

Examples1

  1. Example 1

    Input
    5 6
    1 2
    3 3 1 4
    1 2
    2 5 2
    1 4
    1
    2
    3
    4
    5
    6
    
    Expected output
    2
    1
    2
    4
    2
    3