#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <set>
#include <iostream>
using namespace std;
#define NMAX 32005
#define TMAX 100005
FILE * f = fopen("obiective.in", "r");
FILE * g = fopen("obiective.out", "w");
int n, m, t, x, y, u, indexx, ssize, nrctc, rez[TMAX], q[NMAX], ctc[NMAX], viz[NMAX], cost[NMAX], s[NMAX], idx[NMAX], lowlink[NMAX], onstack[NMAX];
set<int> l[NMAX], ls[NMAX], sol[NMAX];
struct locatie{
int x, y, ind;
} loc[TMAX];
int comp(locatie a, locatie b) {
return ctc[a.x] < ctc[b.x];
}
int exista_muchie(int x, int y, set<int> a[]) {
if(a[x].count(y)) return 1;
else return 0;
}
void citire_graf() {
int x, y;
fscanf(f, "%d%d", &n, &m);
for(int i = 1; i <= m; i++) {
fscanf(f, "%d%d", &x, &y);
l[x].insert(y);
}
}
void tarjan(int v) {
int x;
s[++ssize] = v; onstack[v] = 1;
idx[v] = lowlink[v] = ++indexx;
for(set<int>::iterator it = l[v].begin(); it != l[v].end(); ++it) {
int u = *it;
if(!idx[u]) {
tarjan(u);
lowlink[v] = min(lowlink[v], lowlink[u]);
}
else if(onstack[u]) lowlink[v] = min(lowlink[v], lowlink[u]);
}
if(idx[v] == lowlink[v]) {
nrctc++;
do {
x = s[ssize];
sol[nrctc].insert(x);
ctc[x] = nrctc;
onstack[x] = 0; ssize--;
} while(x != v);
}
}
void drum_ctc(int i, int j) {
for(set<int>::iterator it = sol[i].begin(); it != sol[i].end(); ++it) {
int x = *it;
for(set<int>::iterator it2 = sol[j].begin(); it2 != sol[j].end(); ++it2) {
int y = *it2;
if(exista_muchie(x, y, l)) {
ls[i].insert(j);
return;
}
if(exista_muchie(y, x, l)) {
ls[j].insert(i);
return;
}
}
}
}
void simplifica_graf() {
for(int i = 1; i < nrctc; i++)
for(int j = i + 1; j <= nrctc; j++) drum_ctc(i, j);
}
void bf(int x) {
int q[NMAX], qsize = 0;
memset(cost, 0, sizeof(cost));
memset(viz, 0, sizeof(viz));
q[++qsize] = x;
while(qsize) {
int v = q[qsize--];
if(!viz[v]) {
viz[v] = 1;
for(int i = 1; i <= nrctc; i++) {
if(exista_muchie(v, i, ls) && !viz[i]) {
cost[i] = cost[v];
q[++qsize] = i;
}
if(exista_muchie(i, v, ls) && !viz[i]) {
cost[i] = cost[v] + 1;
q[++qsize] = i;
}
}
}
}
}
int main() {
citire_graf();
for(int i = 1; i <= n; i++)
if(!idx[i]) tarjan(i);
simplifica_graf();
fscanf(f, "%d", &t);
for(int i = 1; i <= t; i++) {
fscanf(f, "%d%d", &x, &y);
loc[i].x = x; loc[i].y = y; loc[i].ind = i;
}
sort(loc + 1, loc + t + 1, comp);
int ux = 0;
for(int i = 1; i <= t; i++) {
x = loc[i].x; y = loc[i].y;
if(ctc[ux] != ctc[x]) bf(ctc[x]);
rez[loc[i].ind] = cost[ctc[y]];
ux = x;
}
for(int i = 1; i <= t; i++) fprintf(g, "%d\n", rez[i]);
fclose(f); fclose(g);
return 0;
}