#pragma GCC optimize ("Ofast") #include using namespace std; using LL=long long; using ULL=unsigned long long; #define rep(i,n) for(int i=0;i<(n);i++) struct LCA_RMQ{ struct RMQ_Node{ int d,i; bool operator<(const RMQ_Node& r) const { return d V; vector R; void construct(){ N=1; while(N=1; i--) V[i]=min(V[i*2],V[i*2+1]); } int lca(int u,int v){ if(u==v) return u; int l=R[u], r=R[v]; if(l>r) swap(l,r); r++; RMQ_Node ans=V[l]; while(l> E; int Root; vector D; LCA_RMQ lca_rmq; vector P; public: rooted_tree(int n){ N=n; E.resize(n); } int add_node(){ int idx=N; E.push_back({}); N++; return idx; } void add_edge(int parent,int child){ E[parent].push_back(child); } void dfs_lca(int p){ lca_rmq.R[p]=lca_rmq.V.size(); lca_rmq.V.push_back({D[p],p}); for(int e:E[p]) if(P[p]!=e){ P[e]=p; D[e]=D[p]+1; dfs_lca(e); lca_rmq.V.push_back({D[p],p}); } } void init_for_lca(int root){ Root=root; P.assign(N,-1); D.assign(N,-1); D[Root] = 0; lca_rmq.R.resize(N); dfs_lca(0); lca_rmq.construct(); } int distance(int u,int v){ int g=lca_rmq.lca(u,v); return D[u]+D[v]-D[g]*2; } }; struct graph_original{ private: int N; vector> E; vector dfsP; vector dfsD; vector condition_a; vector dfsI; private: int dfs1(int p){ dfsI.push_back(p); int backedge = dfsD[p]; for(int e:E[p]){ if(dfsD[e]!=-1){ backedge = min(backedge,dfsD[e]); continue; } dfsD[e] = dfsD[p]+1; dfsP[e] = p; int res = dfs1(e); if(res>=dfsD[p]) condition_a[e]=1; backedge = min(backedge,res); } return backedge; } public: void construct_dfsTree(int root){ dfsP.assign(N,-1); dfsD.assign(N,-1); condition_a.assign(N,0); dfsD[root] = 0; dfs1(root); } public: graph_original(int n){ N=n; E.resize(N); } void add_edge(int u,int v){ E[u].push_back(v); E[v].push_back(u); } rooted_tree construct_tree(int root){ rooted_tree res(N); construct_dfsTree(0); int p=-1; struct QueueNode { int v,p; }; vector Q; Q.push_back({0,-1}); while(Q.size()){ auto q = Q.back(); Q.pop_back(); if(q.p != -1) res.add_edge(q.p,q.v); for(int e:E[q.v]){ if(dfsD[e] != dfsD[q.v]+1) continue; QueueNode nx = {e,q.p}; if(condition_a[e]){ nx.p = res.add_node(); res.add_edge(q.v,nx.p); } Q.push_back(nx); } } res.init_for_lca(root); return res; } }; int main(){ int N,M; scanf("%d%d",&N,&M); graph_original G(N); rep(i,M){ int u,v; scanf("%d%d",&u,&v); u--; v--; G.add_edge(u,v); } rooted_tree T = G.construct_tree(0); int Q; cin>>Q; rep(i,Q){ int x,y; scanf("%d%d",&x,&y); x--; y--; int Dxy = T.distance(x,y); printf("%d\n",max(0,Dxy/2-1)); } return 0; }