class Node
  attr_accessor :post, :visited, :child, :parent, :group
  def initialize
    @post = nil
    @visited = false
    @child = []
    @parent = []
    @group = nil
  end
end

def dfs(g)
  arr = []
  v = g.size
  t = 1
  v.times{|i|
    next if g[i].visited
    stack = []
    g[i].visited = true

    stack << i
    while stack.size > 0
      u = stack[-1]
      flag = true
      g[u].child.each{|x|
        if g[x].visited == false
          g[x].visited = true
          stack << x
          flag = false
        end
      }
      if flag == true
        g[u].post = t
        arr.unshift(u)
        t += 1
        stack.pop
      end
    end
  }
  return arr
end

def scc_decomposition(arr, g)
  arr.each{|i|
    next unless g[i].visited
    stack = []
    g[i].visited = false
    g[i].group = i
    stack << i
    while stack.size > 0
      u = stack[-1]
      flag = true
      g[u].parent.each{|x|
        if g[x].visited == true
          g[x].visited = false
          g[x].group = i
          stack << x
          flag = false
        end
      }
      if flag == true
        stack.pop
      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
  graph[t].parent << s
}

arr = dfs(graph)
scc_decomposition(arr, graph)

# v.times{|i|
#   p graph[i]
# }

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