require "tsort"
n, m, s = gets.split.map(&:to_i)
g = {}
n.times { |i| g[i] = [] }
m.times do
  a, b = gets.split.map(&:to_i)
  g[a].push(b)
end
each_node = lambda { |&b| g.each_key(&b) }
each_child = lambda { |n, &b| g[n].each(&b) }
scc = TSort.strongly_connected_components(each_node, each_child)
root = Array.new(n)
scc.each do |comp|
  comp.each do |v|
    root[v] = comp[0]
  end
end
q = gets.chomp.to_i
q.times do
  a, b = gets.split.map(&:to_i)
  if root[a] == root[b]
    puts 1
  else
    puts 0
  end
end

