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

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


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