human
Give me a the Python program you requested given the following:
A direced graph is strongly connected if every two nodes are reachable from each other. In a strongly connected component of a directed graph, every two nodes of the component are mutually reachable.

Constraints

* 1 ≤ |V| ≤ 10,000
* 0 ≤ |E| ≤ 30,000
* 1 ≤ Q ≤ 100,000

Input

A directed graph G(V, E) and a sequence of queries where each query contains a pair of nodes u and v.

|V| |E|
s0 t0
s1 t1
:
s|E|-1 t|E|-1
Q
u0 v0
u1 v1
:
uQ-1 vQ-1

|V| is the number of nodes and |E| is the number of edges in the graph. The graph nodes are named with the numbers 0, 1,..., |V|-1 respectively.

si and ti represent source and target nodes of i-th edge (directed).

ui and vi represent a pair of nodes given as the i-th query.

Output

For each query, pinrt "1" if the given nodes belong to the same strongly connected component, "0" otherwise.

Example

Input

5 6
0 1
1 0
1 2
2 4
4 3
3 2
4
0 1
0 3
2 3
3 4

Output

1
0
1
1
Using python3 can you solve the prior task?
assistant
#Here is the Python program you requested.

import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

class StronglyConnectedComponents:
    def __init__(self,N):
        self.N = N
        self.G = [[] for _ in range(self.N)]
        self.rG = [[] for _ in range(self.N)]
        self.group = [1] * self.N
        self.order = []
    
    def add_edge(self,u,v): # u -> v
        self.G[u].append(v)
        self.rG[v].append(u)
    
    def build(self):
        stack = []
        for s in range(self.N):
            if self.group[s] > 0:
                stack.append(s + 1)
                while stack:
                    v = stack.pop()
                    if v < 0:
                        self.order.append(- v - 1)
                    else:
                        if self.group[v - 1] > 0:
                            stack.append(-v)
                            self.group[v - 1] = -1
                            for e in self.G[v - 1]:
                                stack.append(e + 1)
        
        cnt = 0
        for s in self.order[::-1]:
            if self.group[s] < 0:
                stack.append(s)
                self.group[s] = cnt
                while stack:
                    v = stack.pop()
                    for e in self.rG[v]:
                        if self.group[e] < 0:
                            self.group[e] = cnt
                            stack.append(e)
                cnt += 1
    
    def __getitem__(self,i):
        if 0 <= i < self.N:
            return self.group[i]
        else:
            raise ValueError("StrongglyConnectedComponents index out of range")

def main():
    V,E = map(int,input().split())
    scc = StronglyConnectedComponents(V)
    for _ in range(E):
        a,b = map(int,input().split())
        scc.add_edge(a,b)
    scc.build()

    Q = int(input())
    ans = []
    for _ in range(Q):
        u,v = map(int,input().split())
        if scc[u] == scc[v]:
            ans.append('1')
        else:
            ans.append('0')
    print('\n'.join(ans))
if __name__ == '__main__':
    main()