Hierarchy#2946

Hierarchy

3 seconds
64 megabytes
Very hard

In a certain organization, there are employees. Employee is considered a subordinate of employee if:

  1. is a direct subordinate of , or

  2. There exists an employee such that is a direct subordinate of , and is a subordinate of .

No employee is their own subordinate. Messages in the organization may be passed between and only if is a subordinate of or vice versa. The structure of the organization is such that messages can be passed between any two employees. For given employees and , determine whether is a subordinate of , is a subordinate of , or there is no subordinate relationship between them.

Input

The first line contains the number of employees and the number of queries .

The second and third lines contain the query parameters , , and , , .

Then lines follow describing the organization's structure. The (i+3)-rd line contains information about the direct subordinates of the -th employee: k_i — the number of direct subordinates, followed by the indices of these k_i direct subordinates.

Employees are numbered from to n-1.

Each query is of the form (x_j, y_j), and the answer for each query should be if y_j is a subordinate of x_j, if x_j is a subordinate of y_j, or otherwise.

Here,

x_0 = X,\ y_0 = Y
x_j = (a x_{j-1} + b + \mathrm{sum}_{j-1}) \bmod n,\quad y_j = (c y_{j-1} + d + \mathrm{sum}_{j-1}) \bmod n

where j = 1..q-1, and \mathrm{sum}_{j-1} is the sum of the answers (moduli) to the first j-1 queries.

Constraints

1 \leq n \leq 10^5

1 \leq q \leq 5 \cdot 10^5

0 \leq X, a, b, Y, c, d < n

n - 1 \leq k_i < n + 100

Output

For each query, print the answer in a separate line as described above.

Examples

Example 1

Loading
Loading