本题采用圆方树做法,vcc缩点也是可以的
//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define p_queue priority_queue
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
//前向星
int n, m;
int head[200005], cnt;
int rect;
int yf_head[200005], yf_cnt;
struct e{
int t, next;
}edge[200005*2], yf_edge[200005*2];
void add(int f, int t)
{
edge[cnt].t = t;
edge[cnt].next = head[f];
head[f] = cnt ++;
}
void yf_add(int f, int t)
{
yf_edge[yf_cnt].t = t;
yf_edge[yf_cnt].next = yf_head[f];
yf_head[f] = yf_cnt ++;
}
int dfn[200005], low[200005] , fa[200005], vec[200005], times;
int dep[200005], vis[200005], bz[200005][30];
stack <int> vs, es;
void init()
{
while(!vs.empty())
vs.pop();
while(!es.empty())
es.pop();
mem(head,-1);
mem(yf_head,-1);
mem(dep,0);
mem(dfn, 0);
mem(low,0);
mem(fa,0);
mem(vec,0);
mem(vis,0);
mem(bz,0);
times = cnt = yf_cnt = 0;
rect = n;
}
void tarjan(int u)
{
dfn[u] = low[u] = ++ times;
vs.push(u);
for(int i = head[u] ; i != -1 ; i = edge[i].next)
{
int v = edge[i].t;
if(!dfn[v])
{
fa[v] = u;
es.push((i + 2) / 2);
tarjan(v);
low[u] = min(low[u] , low[v]);
if(low[v] >= dfn[u])
{
++ rect;
while(1)
{
int x = vs.top();
vs.pop();
yf_add(rect , x);
yf_add(x, rect);
if(x == v) break;
}
yf_add(rect, u);
yf_add(u, rect);
while(1)
{
int id = es.top();
es.pop();
vec[id] = rect;
if(id == (i + 2) / 2 ) break;
}
}
}
else if(v != fa[u])
{
low[u] = min(low[u], dfn[v]);
if(dfn[v] < dfn[u]) es.push((i+2)/2);
}
}
}
void dfs(int u)
{
vis[u] = 1;
for(int i = yf_head[u] ; i != -1; i = yf_edge[i].next)
{
int v = yf_edge[i].t;
if(vis[v]) continue;
bz[v][0] = u;
dep[v] = dep[u] + 1;
dfs(v);
}
}
void deal()
{
for(int i = 1 ; i <= 20 ; i ++)
{
for(int j = 1 ; j <= rect ; j ++)
{
bz[j][i] = bz[bz[j][i-1]][i-1];
}
}
}
int LCA(int x, int y) {
if (dep[x] < dep[y])
swap(x, y);
for (int i = 20; i >= 0; i--)
if (dep[bz[x][i]] >= dep[y])
x = bz[x][i];
if (x == y) return x;
for (int i = 20; i >= 0; i--)
if (bz[x][i] ^ bz[y][i])
x = bz[x][i], y = bz[y][i];
return bz[x][0];
}
int main(void)
{
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("odata.out", "w", stdout);
#endif
while(scanf("%d %d",&n , &m) != EOF)
{
if(n == 0 && m == 0)
break;
init();
int u, v;
for (int i = 1; i <= m; i++)
{
scanf("%d %d", &u , &v);
add(u, v);
add(v, u);
}
for(int i = 1 ; i <= n ; i ++)
{
if(!dfn[i])
{
tarjan(i);
dep[i] = 1;
dfs(i);
}
}
deal();
int q;
scanf("%d",&q);
while(q --)
{
scanf("%d %d", &u , &v);
int x = vec[u] , y = vec[v];
int lca = LCA(x,y);
printf("%d\n",(dep[x] + dep[y] - 2 * dep[lca])/2);
}
}
}