somnoros

Time limit: 0.7s Memory limit: 512MB Input: Output:

The cat Somnorilă is a very sleepy cat! While sleeping, he had a dream in which he and a mouse were in a labyrinth. What is special about this labyrinth is that all the paths between the rooms of the labyrinth can only be traversed in one direction, and during a walk through the labyrinth, he cannot pass through the same room twice. Moreover, only the cat knows the map of the labyrinth, as the mouse does not have the courage to move from his current room. Being a delicate cat, Somnorilă wants to know if, from the room he is currently in, he can reach the mouse's room, so he will know if his dream was a nightmare or a pleasant dream when he wakes up!

Formally, you are given a directed acyclic graph (DAG) with NN nodes and MM edges, and QQ questions of the form: can node YY be accessed from node XX?

Task

You need to answer all QQ questions from Somnorilă.

Input data

The first line contains the numbers NN and MM, where NN represents the number of nodes in the graph, and MM the number of edges. The next MM lines contain two numbers XX and YY, indicating that there is an edge from node XX to node YY in the graph. Line M+2M + 2 contains the number QQ, representing the number of questions. The next QQ lines each contain numbers XX and YY, representing a question.

Output data

The output will contain QQ lines, each having a number. Each line ii contains the answer to question ii. Print 11 if node XiX_i can reach node YiY_i, or 00 otherwise.

Constraints and clarifications

  • 1N50 0001 \leq N \leq 50\ 000
  • 1M200 0001 \leq M \leq 200\ 000
  • 1Q100 0001 \leq Q \leq 100\ 000
  • It is guaranteed that in an edge and in a query, node XX will be at a lower level than node YY.
  • It is guaranteed that for each node on a level X (X>1)X \ (X > 1), there will be at least one edge with a node on level X1X - 1, () X>1(∀) \ X > 1.
  • For 2828 points, 1N,Q1 0001 \leq N, Q \leq 1\ 000, the tests are NOT grouped.
  • For the remaining 7272 points, the original constraints apply, and the tests are grouped into 4-5 tests per group.

Example

stdin

12 13
1 4
2 5
3 5
4 6
4 7
4 11
5 6
5 8
5 12
6 9
7 9
7 10
11 12
4
1 9
4 8
7 12
3 6

stdout

1
0
0
1

Explanation

  • Node 99 can be accessed from node 11. (Example path: 14691 → 4 → 6 → 9)
  • Node 88 cannot be accessed from node 44.
  • Node 1212 cannot be accessed from node 77.
  • Node 66 can be accessed from node 33. (Example path: 3563 → 5 → 6)

Log in or sign up to be able to send submissions!