class StronglyConnectedComponent
  attr_accessor :cmp
  def initialize(n)
    @v = n
    @g = Array.new(n){ Array.new }
    @rg = Array.new(n){ Array.new }
    @vs = Array.new
    @used = Array.new(n)
    @cmp = Array.new(n)
  end
  def add_edge(from, to)
    @g[from] << to
    @rg[to] << from
  end
  def dfs(v)
    @used[v] = true
    (0...@g[v].size).each do |i|
      unless @used[@g[v][i]] then dfs(@g[v][i])
      end
    end
    @vs << v
  end
  def rdfs(v, k)
    @used[v] = true
    @cmp[v] = k
    (0...@rg[v].size).each do |i|
      unless @used[@rg[v][i]] then rdfs(@rg[v][i], k)
      end
    end
  end
  def scc
    @v.times{ |i| @used[i] = false }
    @vs.clear
    @v.times{ |i| dfs(i) unless @used[i] }
    @v.times{ |i| @used[i] = false }
    k = 0
    (@vs.size-1).downto(0) do |i|
      unless @used[@vs[i]] then rdfs(@vs[i], k); k += 1
      end
    end
    return k
  end
end

n, e = gets.split.map(&:to_i)
scc = StronglyConnectedComponent.new(n)
e.times do 
  a, b = gets.split.map(&:to_i)
  scc.add_edge(a, b)
end
scc.scc
q = gets.to_i
q.times do
  a, b = gets.split.map(&:to_i)
  if scc.cmp[a] == scc.cmp[b] then p 1
  else p 0
  end
end