Problem:
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.

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.

Constraints

1 ≤ |V| ≤ 10,000

0 ≤ |E| ≤ 30,000

1 ≤ Q ≤ 100,000

Sample Input 1

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

Sample Output 1

1
0
1
1

Solution in Ruby (judge verdict: Wrong Answer)

class Node
  attr_accessor :index, :lowlink, :onStack, :child
  def initialize
    @index = nil
    @lowlink = nil
    @onStack = false
    @child = []
  end
end

# def strongconnect(v, graph)
#   graph[v].index = graph[v].lowlink = $index
#   $index += 1
#   $stack << v
#   graph[v].onStack = true

#   graph[v].child.each{|w|
#     if graph[w].index == nil
#       strongconnect(w, graph)
#       graph[v].lowlink = [graph[v].lowlink, graph[w].lowlink].min
#     elsif graph[w].onStack
#       graph[v].lowlink = [graph[v].lowlink, graph[w].index].min
#     end
#   }

#   if graph[v].lowlink == graph[v].index
#     $scc_num += 1
#     loop do
#       w = $stack.pop
#       graph[w].onStack = false
#       $group[w] = $scc_num
#       break if w == v
#     end
#   end
# end

def strongconnect(v, graph)
  s = []
  s << v
  while s.size > 0
    v = s[-1]
    if graph[v].index == nil
      graph[v].index = graph[v].lowlink = $index
      $index += 1
      $stack << v
      graph[v].onStack = true
    end

    flag = true
    graph[v].child.each{|w|
      if graph[w].index == nil
        s << w
        flag = false
        break
      elsif graph[w].onStack
        graph[v].lowlink = [graph[v].lowlink, graph[w].index].min
      end
    }
    if flag
      s.pop
      break if s.size == 0
      u = s[-1]
#      puts "#{u}, #{v}"
      graph[u].lowlink = [graph[u].lowlink, graph[v].lowlink].min

      if graph[v].lowlink == graph[v].index
        $scc_num += 1
        loop do
          w = $stack.pop
          graph[w].onStack = false
          $group[w] = $scc_num
          break if w == v
        end
      end
    end
  end
end

v, e = gets.split.map(&:to_i)
graph = Array.new(v){Node.new}
e.times{
  s, t = gets.split.map(&:to_i)
  graph[s].child << t
}

$index = 0
$stack = []
$scc_num = -1
$group = {}

v.times{|i|
  if graph[i].index == nil
    strongconnect(i, graph)
  end
}
# p $group
# v.times{|i|
#   p graph[i]
# }

q = gets.to_i
q.times{
  u, v = gets.split.map(&:to_i)
  if $group[u] == $group[v]
    puts 1
  else
    puts 0
  end
}