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: # 蟻本参照
  import sys
  
  sys.setrecursionlimit(200000)
  input = sys.stdin.readline
  ##redline,recursionlimit変更
  v, e = map(int, input().split())
  
  E = [[] for _ in range(v)]
  revE = [[] for _ in range(v)]
  used = [0] * v
  kaeri = []
  topo = [-1 for _ in range(v)]
  
  for _ in range(e):
      s, t = map(int, input().split())
      E[s].append(t)
      revE[t].append(s)
  
  q = int(input())
  
  
  ############
  def dfs(v):
      used[v] = 1
      for u in E[v]:
          if not used[u]:
              dfs(u)
      kaeri.append(v)
  
  
  def re_dfs(v, k):
      used[v] = 1
      topo[v] = k
      for u in revE[v]:
          if not used[u]:
              re_dfs(u, k)
  
  
  ###
  for u in range(v):
      if not used[u]:
          dfs(u)
  kaeri.reverse()
  
  
  used = [0] * v
  k = 0
  for u in kaeri:
      if not used[u]:
          k += 1
          re_dfs(u, k)
  # print(topo)
  for _ in range(q):
      u, v = map(int, input().split())
      if topo[u] == topo[v]:
          print(1)
      else:
          print(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"}]