"""

強連結成分分解

"""



import sys



sys.setrecursionlimit(1000000)



def dfs(v, visited, edges, order):

  visited[v] = True

  for to in edges[v]:

    if not visited[to]:

      dfs(to, visited, edges, order)

  order.append(v)



def search_strongly_connection(v, visited, reverse_edges, parent):

  visited[v] = True

  for to in reverse_edges[v]:

    if not visited[to]:

      parent[to] = v

      search_strongly_connection(to, visited, reverse_edges, parent)



def find(parent, x):

  if parent[x] == x:

    return x

  tmp = find(parent, parent[x])

  parent[x] = tmp

  return tmp



v_num , e_num = list(map(int, input().split()))

edges = [[] for _ in range(v_num)]

reverse_edges = [[] for _ in range(v_num)]

for _ in range(e_num):

  s, t = list(map(int, input().split()))

  edges[s].append(t)

  reverse_edges[t].append(s)





order = []

visited = [False] * v_num

for v in range(v_num):

  if not visited[v]:

    dfs(v, visited, edges, order)

order.reverse()





visited = [False] * v_num

parent = [i for i in range(v_num)]

for v in order:

  if not visited[v]:

    search_strongly_connection(v, visited, reverse_edges, parent)



q_num = int(eval(input()))

for _ in range(q_num):

  u, v = list(map(int, input().split()))

  if find(parent, u) == find(parent, v):

    print((1))

  else:

    print((0))

"""

強連結成分分解

"""



import sys



sys.setrecursionlimit(1000000)



def dfs(v, visited, edges, order):

  visited[v] = True

  for to in edges[v]:

    if not visited[to]:

      dfs(to, visited, edges, order)

  order.append(v)



def search_strongly_connection(v, visited, reverse_edges, parent, num):

  visited[v] = True

  for to in reverse_edges[v]:

    if not visited[to]:

      parent[to] = num

      search_strongly_connection(to, visited, reverse_edges, parent, num)



v_num , e_num = list(map(int, input().split()))

edges = [[] for _ in range(v_num)]

reverse_edges = [[] for _ in range(v_num)]

for _ in range(e_num):

  s, t = list(map(int, input().split()))

  edges[s].append(t)

  reverse_edges[t].append(s)





order = []

visited = [False] * v_num

for v in range(v_num):

  if not visited[v]:

    dfs(v, visited, edges, order)

order.reverse()





visited = [False] * v_num

parent = [i for i in range(v_num)]

for v in order:

  num = v

  if not visited[v]:

    search_strongly_connection(v, visited, reverse_edges, parent, num)



q_num = int(eval(input()))

for _ in range(q_num):

  u, v = list(map(int, input().split()))

  if parent[u] == parent[v]:

    print((1))

  else:

    print((0))

"""
強連結成分分解
"""
import sys

sys.setrecursionlimit(1000000)


def dfs(v, visited, edges, order):
    visited[v] = True
    for to in edges[v]:
        if not visited[to]:
            dfs(to, visited, edges, order)
    order.append(v)


def search_strongly_connection(v, visited, reverse_edges, parent):
    visited[v] = True
    for to in reverse_edges[v]:
        if not visited[to]:
            parent[to] = v
            search_strongly_connection(to, visited, reverse_edges, parent)


def find(parent, x):
    if parent[x] == x:
        return x
    tmp = find(parent, parent[x])
    parent[x] = tmp
    return tmp


v_num, e_num = list(map(int, input().split()))
edges = [[] for _ in range(v_num)]
reverse_edges = [[] for _ in range(v_num)]
for _ in range(e_num):
    s, t = list(map(int, input().split()))
    edges[s].append(t)
    reverse_edges[t].append(s)
order = []
visited = [False] * v_num
for v in range(v_num):
    if not visited[v]:
        dfs(v, visited, edges, order)
order.reverse()
visited = [False] * v_num
parent = [i for i in range(v_num)]
for v in order:
    if not visited[v]:
        search_strongly_connection(v, visited, reverse_edges, parent)
q_num = int(eval(input()))
for _ in range(q_num):
    u, v = list(map(int, input().split()))
    if find(parent, u) == find(parent, v):
        print((1))
    else:
        print((0))

"""
強連結成分分解
"""
import sys

sys.setrecursionlimit(1000000)


def dfs(v, visited, edges, order):
    visited[v] = True
    for to in edges[v]:
        if not visited[to]:
            dfs(to, visited, edges, order)
    order.append(v)


def search_strongly_connection(v, visited, reverse_edges, parent, num):
    visited[v] = True
    for to in reverse_edges[v]:
        if not visited[to]:
            parent[to] = num
            search_strongly_connection(to, visited, reverse_edges, parent, num)


v_num, e_num = list(map(int, input().split()))
edges = [[] for _ in range(v_num)]
reverse_edges = [[] for _ in range(v_num)]
for _ in range(e_num):
    s, t = list(map(int, input().split()))
    edges[s].append(t)
    reverse_edges[t].append(s)
order = []
visited = [False] * v_num
for v in range(v_num):
    if not visited[v]:
        dfs(v, visited, edges, order)
order.reverse()
visited = [False] * v_num
parent = [i for i in range(v_num)]
for v in order:
    num = v
    if not visited[v]:
        search_strongly_connection(v, visited, reverse_edges, parent, num)
q_num = int(eval(input()))
for _ in range(q_num):
    u, v = list(map(int, input().split()))
    if parent[u] == parent[v]:
        print((1))
    else:
        print((0))

-def search_strongly_connection(v, visited, reverse_edges, parent): +def search_strongly_connection(v, visited, reverse_edges, parent, num): -            parent[to] = v -            search_strongly_connection(to, visited, reverse_edges, parent) - - -def find(parent, x): -    if parent[x] == x: -        return x -    tmp = find(parent, parent[x]) -    parent[x] = tmp -    return tmp +            parent[to] = num +            search_strongly_connection(to, visited, reverse_edges, parent, num) +    num = v -        search_strongly_connection(v, visited, reverse_edges, parent) +        search_strongly_connection(v, visited, reverse_edges, parent, num) -    if find(parent, u) == find(parent, v): +    if parent[u] == parent[v]: