Under construction

Still being built. Things may change

Connect

Time limit2.5sMemory limit1024 MB

Summary
For each city pair, find the lowest-numbered road whose closure breaks two-way reachability in a directed graph.
Level

Hard9 of 10

Topics
Graph, DFS, Divide and conquer
Solved
No attempts yet

Problem

Because of all the travelling around the country for IATI 2022, the tour guide company of Kyusho was once again overwhelmed with questions. The region that his company is responsible for consists of NN cities, numbered from 11 to NN. Between them, there are MM direct roads, numbered from 11 to MM, each connecting a pair of different cities. There may be more than one direct road in a given direction between two cities.

The dispatch department often receives questions from bus drivers of this form: "Can you always go from city AA to city BB and then go back to city AA even if one of the roads is closed?"

Kyusho knows that you are an experienced programmer, so he asks you to write a program connect that answers these questions.

Input

The first line contains two positive integers NN, the number of cities, and MM, the number of direct roads. Each of the next MM lines contains two positive integers AA and BB, meaning there is a direct road from city AA to city BB.

The next line contains the positive integer QQ, the number of questions. Each of the following QQ lines contains two positive integers AA and BB, describing one question about cities AA and BB.

Sometimes there are so many questions that it is better to get the answer for every unordered pair of cities (A,B)(A, B). When Q=0Q = 0, the answers are needed for the pairs (1,1),(1,2),(1,3),…,(1,N),(2,2),(2,3),…,(2,N),(3,3),…,(N,N)(1,1), (1,2), (1,3), \dots, (1,N), (2,2), (2,3), \dots, (2,N), (3,3), \dots, (N,N), in that order.

Output

For each question, assign a number as follows.

  • 00: Even without closing one of the roads, there is no route between the two cities in at least one of the two directions.
  • M+1M+1: No matter which road is closed, there is always a route between the two cities in both directions.
  • A number between 11 and MM: the number of the road whose closure leaves no route in one of the two directions. If more than one road qualifies, print the lowest-numbered one.

Let the resulting numbers be s1,s2,…,sKs_1, s_2, \dots, s_K, where K=QK = Q if Q≠0Q \neq 0, and K=N×(N+1)2K = \frac{N \times (N+1)}{2} if Q=0Q = 0. Print on one line the remainder of

P=s1×BK−1+s2×BK−2+⋯+sK×B0P = s_1 \times B^{K-1} + s_2 \times B^{K-2} + \cdots + s_K \times B^0

modulo 109+710^9 + 7, where B=2×105B = 2 \times 10^5.

Constraints

  • 2≤N≤20002 \le N \le 2000
  • 1≤A,B≤N1 \le A, B \le N
  • 1≤M≤1051 \le M \le 10^5
  • 0≤Q≤1050 \le Q \le 10^5

Examples2

  1. Example 1

    Input
    6 11
    1 4
    4 3
    3 5
    5 1
    1 3
    3 6
    5 6
    6 2
    4 2
    5 1
    3 5
    0
    
    Expected output
    575589257
    
  2. Example 2

    Input
    8 17
    5 2
    6 5
    4 6
    4 8
    4 3
    3 1
    2 7
    7 4
    5 4
    8 7
    8 3
    3 6
    6 1
    2 4
    1 5
    1 5
    5 2
    4
    4 6
    8 1
    5 2
    6 7
    
    Expected output
    995598902