#include <stdio.h>
#include <stdlib.h>
#define MAX_V 10001

typedef struct vertex {
    struct edge *head;
}vertex;

typedef struct edge {
    int to;
    struct edge *next;
}edge;

void add(vertex *a, int to);
void dfs(int i);
int rdfs(int i, int k);
int scc(void);
void init(void);

vertex g[MAX_V];
vertex rg[MAX_V];
int used[MAX_V];
int ret[MAX_V];
int num[MAX_V];
int v, e, dfs_num;

int main(void)
{
    int i, j, k, f, t, q;
    scanf("%d %d", &v, &e);
    for (i = 0; i < e; i++) {
        scanf("%d %d", &f, &t);
        add(&g[f], t);
        add(&rg[t], f);
    }
    init();
    scc();
    scanf("%d", &q);
    for (i = 0; i < q; i++) {
        scanf("%d %d", &f, &t);
        printf("%d\n", ret[f] == ret[t]);
    }
    return 0;
}

void init(void)
{
    int i;
    for (i = 0; i < v; i++) {
        used[i] = 0;
        ret[i] = 0;
        num[i] = 0;
    }
    dfs_num = 0;
}

int scc(void)
{
    int i, k = 0;
    for (i = 0; i < v; i++) {
        if (!used[i]) {
            dfs(i);
        }
    }
    for (i = 0; i < v; i++) used[i] = 0;
    for (i = v - 1; i >= 0; i--) {
        if (!used[num[i]]) {
            rdfs(num[i], k++);
        }
    }
    return k;
}

int rdfs(int i, int k)
{
    edge *pos = rg[i].head;
    used[i] = 1;
    ret[i] = k;
    while (pos) {
        if (!used[pos->to]) {
            rdfs(pos->to, k);
        }
        pos = pos->next;
    }
    return k;
}

void dfs(int i)
{
    edge *pos = g[i].head;
    used[i] = 1;
    while (pos) {
        if (!used[pos->to]) {
            dfs(pos->to);
        }
        pos = pos->next;
    }
    num[dfs_num++] = i;
}

void add(vertex *a, int to)
{
    edge *pos = a->head;
    edge *new_ = (edge *)malloc(sizeof(edge));
    new_->to = to;
    new_->next = NULL;
    if (!pos) {
        a->head = new_;
    } else {
        while (pos->next) {
            pos = pos->next;
        }
        pos->next = new_;
    }
}