require 'tsort'

class Hash
  include TSort
  alias tsort_each_node each_key
  def tsort_each_child(node, &block)
    fetch(node).each(&block)
  end
end

v,e = gets.split.map(&:to_i)
g = {}
e.times do
  s,t = gets.split.map(&:to_i)
  g[s] ||= []; g[s] << t
end
cmp = [-1] * v
scc = g.strongly_connected_components
scc.each_with_index{ |ar, i| ar.each{ |v| cmp[v] = i } }
gets.to_i.times do
  u, v = gets.split.map(&:to_i)
  p cmp[u] == cmp[v] ? 1 : 0
end