import sys
input = sys.stdin.readline


class StronglyConnectedComponents:
    """与えられた有向グラフに対して強連結成分分解する
    label: 分解されたグラフの個数
    group: 強連結成分のグループ分け番号
    """
    def __init__(self, graph):
        self.n = len(graph)
        self.graph = graph
        self.out_degree = [len(graph[i]) for i in range(n)]
        self.label = 0
        self.group = [-1] * self.n

        # 逆辺を用いたグラフ(rev_graph)の構築
        self.rev_graph = [[] for i in range(self.n)]
        for a in range(self.n):
            for b in graph[a]:
                self.rev_graph[b].append(a)

        # graph上での帰りがけ順(post_order)の構築
        self.post_order = []
        self.used = [False] * self.n
        for v in range(self.n):
            if not self.used[v]:
                self._dfs(v)

        # 帰りがけ順の逆順で走査し、
        # 未訪問かつrev_graph上でたどり着ける頂点は強連結である
        for v in reversed(self.post_order):
            if self.group[v] == -1:
                self._rev_dfs(v, self.label)
                self.label += 1

    def _dfs(self, v):
        st1 = [v, 0]
        while st1:
            v, ind = st1[-2], st1[-1]
            if not ind and self.used[v]:
                st1.pop()
                st1.pop()
            else:
                self.used[v] = True
                if ind < self.out_degree[v]:
                    st1[-1] += 1
                    st1.append(graph[v][ind])
                    st1.append(0)
                else:
                    st1.pop()
                    self.post_order.append(st1.pop())

    def _rev_dfs(self, v, label):
        st = [v]
        self.group[v] = label
        while st:
            v = st.pop()
            for nxt_v in self.rev_graph[v]:
                if self.group[nxt_v] >= 0:
                    continue
                st.append(nxt_v)
                self.group[nxt_v] = label

    def construct(self):
        """縮約グラフ(red_graph)と縮約された頂点が含む元の頂点(red_comps)を求める"""
        self.red_graph = [set() for i in range(self.label)]
        self.red_comps = [[] for i in range(self.label)]

        for v in range(self.n):
            lb = self.group[v]
            for nxt_v in self.graph[v]:
                nxt_lb = self.group[nxt_v]
                if lb == nxt_lb:
                    continue
                self.red_graph[lb].add(nxt_lb)
            self.red_comps[lb].append(v)

        return self.red_graph, self.red_comps

    def tp_sort(self):
        """縮約グラフのトポロジカル順を求める"""
        red_n = len(self.red_graph)
        degree = [0] * red_n
        for v in range(red_n):
            for nxt_v in self.red_graph[v]:
                degree[nxt_v] += 1

        # 入次数が0の頂点を初期値状態とする
        res = [i for i in range(red_n) if degree[i] == 0]
        q = res[0:]
        while q:
            v = q.pop()
            for nxt_v in self.red_graph[v]:
                degree[nxt_v] -= 1
                if degree[nxt_v] == 0:
                    q.append(nxt_v)
                    res.append(nxt_v)
        return res


n, m = map(int, input().split())
info = [list(map(int, input().split())) for i in range(m)]
q = int(input())
query = [list(map(int, input().split())) for i in range(q)]

graph = [[] for i in range(n)]
for a, b in info:
    graph[a].append(b)

scc = StronglyConnectedComponents(graph)
group = scc.group

for u, v in query:
    if group[u] == group[v]:
        print(1)
    else:
        print(0)


Measured memory_bytes: 29280