output_description: For each query, pinrt "1" if the given nodes belong to the same strongly
  connected component, "0" otherwise.
input_description: 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.
attempt: def strongly_connected_components(E):
      N = len(E)
      postorder = []
      visited = set([])
      
      def reverse_edge(E):
          result = [[] for _ in range(len(E))]
          for v, neighbor in enumerate(E):
              for n in neighbor:
                  result[n].append(v)
          return result
  
      def dfs(E, s):
          postorder = [] # a dfs postordering of each vertex
          nonlocal visited
          stack = [(s, -1, 0)] # (vertex, parent, status)
          while stack:
              v, p, st = stack.pop()
              if st == 0 and v not in visited: # visited v for the first time
                  visited |= {v}
                  n_children = 0
                  for u in E[v]:
                      if u in visited:
                          continue
                      if n_children == 0:
                          stack += [(v, p, 2), (u, v, 0)]
                          n_children += 1
                      else:
                          stack += [(v, p, 1), (u, v, 0)]
                          n_children += 1
                  if n_children == 0: # v is a leaf
                      postorder.append(v)
              elif st == 0 and v in visited: # the edge (v, p) is a back edge
                  continue
              elif st == 1: # now searching
                  continue
              else: # search finished
                  postorder.append(v)
          return postorder
  
      for v in range(N):
          if v not in visited:
              order = dfs(E, v)
              postorder += order
      
      reverse_E = reverse_edge(E)
      
      scc = [N] * N
      visited = set([])
      k = 0
      while postorder:
          v = postorder.pop()
          if scc[v] == N:
              order = dfs(reverse_E, v)
              for u in order: scc[u] = k
              k += 1
      return scc
  
  N, M = map(int, input().split())
  E = [[] for _ in range(N)]
  for _ in range(M):
      s, t = map(int, input().split())
      E[s].append(t)
  scc = strongly_connected_components(E)
  Q = int(input())
  for _ in range(Q):
      u, v = map(int, input().split())
      print(1 if scc[u] == scc[v] else 0)
  
problem_description: Strongly Connected Components
  
  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.
samples: [{"input": "5 6\n    0 1\n    1 0\n    1 2\n    2 4\n    4 3\n    3 2\n    4\n    0 1\n    0 3\n    2 3\n    3 4", "output": "1\n    0\n    1\n    1"}]