#include using namespace std; struct biconnected_components{ int cnt; vector bcc; biconnected_components(vector>> &E){ int N = E.size(); vector next(N, -1); vector d(N, -1); vector imos(N, 0); for (int i = 0; i < N; i++){ if (d[i] == -1){ d[i] = 0; dfs1(E, next, d, imos, i); } } int M = 0; for (int i = 0; i < N; i++){ M += E[i].size(); } M /= 2; bcc = vector(M, -1); cnt = 0; for (int i = 0; i < N; i++){ if (d[i] == 0){ dfs2(E, d, imos, i, cnt); } } } void dfs1(vector>> &E, vector &next, vector &d, vector &imos, int v){ for (auto P : E[v]){ int w = P.second; if (d[w] == -1){ d[w] = d[v] + 1; next[v] = w; dfs1(E, next, d, imos, w); imos[v] += imos[w]; } else if (d[w] < d[v] - 1){ imos[v]++; imos[next[w]]--; } } } void dfs2(vector>> &E, vector &d, vector &imos, int v, int b){ for (auto P : E[v]){ int w = P.second; if (d[w] < d[v]){ bcc[P.first] = b; } else if (d[w] == d[v] + 1 && bcc[P.first] == -1){ if (imos[w] > 0){ bcc[P.first] = b; } else { bcc[P.first] = cnt; cnt++; } dfs2(E, d, imos, w, bcc[P.first]); } } } int operator [](int k){ return bcc[k]; } int count(){ return cnt; } }; struct block_cut_tree{ int V; vector cut; vector> G; vector> node; block_cut_tree(vector> &E){ int N = E.size(); int M = 0; vector>> E2(N); for (int i = 0; i < N; i++){ for (int j : E[i]){ if (j > i){ E2[i].push_back(make_pair(M, j)); E2[j].push_back(make_pair(M, i)); M++; } } } biconnected_components B(E2); vector art(N, false); int cnt = 0; for (int i = 0; i < N; i++){ for (auto P : E2[i]){ if (B[P.first] != B[E2[i][0].first]){ art[i] = true; } } if (art[i]){ cnt++; } } V = cnt + B.count(); cut = vector(V, false); G.resize(V); node.resize(V); int cnt2 = 0; vector used(B.count(), false); for (int i = 0; i < N; i++){ if (art[i]){ cut[cnt2] = true; node[cnt2].push_back(i); for (auto P : E2[i]){ int b = B[P.first]; if (!used[b]){ used[b] = true; G[cnt + b].push_back(cnt2); G[cnt2].push_back(cnt + b); node[cnt + b].push_back(i); } } for (auto P : E2[i]){ int b = B[P.first]; used[b] = false; } cnt2++; } else { if (!E2[i].empty()){ int b = B[E2[i][0].first]; node[cnt + b].push_back(i); } } } } }; struct heavy_light_decomposition{ vector p, sz, in, next; heavy_light_decomposition(vector &p, vector> &c): p(p){ int N = p.size(); sz = vector(N, 1); dfs1(c); in = vector(N); next = vector(N, 0); int t = 0; dfs2(c, t); } void dfs1(vector> &c, int v = 0){ for (int &w : c[v]){ dfs1(c, w); sz[v] += sz[w]; if (sz[w] > sz[c[v][0]]){ swap(w, c[v][0]); } } } void dfs2(vector> &c, int &t, int v = 0){ in[v] = t; t++; for (int w : c[v]){ if (w == c[v][0]){ next[w] = next[v]; } else { next[w] = w; } dfs2(c, t, w); } } int lca(int u, int v){ while (true){ if (in[u] > in[v]){ swap(u, v); } if (next[u] == next[v]){ return u; } v = p[next[v]]; } } }; int main(){ int N, M; cin >> N >> M; vector> E(N); for (int i = 0; i < M; i++){ int a, b; cin >> a >> b; a--; b--; E[a].push_back(b); E[b].push_back(a); } block_cut_tree G(E); vector id(N, -1); for (int i = 0; i < G.V; i++){ for (int j : G.node[i]){ if (id[j] == -1){ id[j] = i; } } } vector p(G.V, -1); vector> c(G.V); vector d(G.V, 0); if (G.cut[0]){ d[0]++; } queue q; q.push(0); while (!q.empty()){ int v = q.front(); q.pop(); for (int w : G.G[v]){ if (w != p[v]){ p[w] = v; c[v].push_back(w); d[w] = d[v]; if (G.cut[w]){ d[w]++; } q.push(w); } } } heavy_light_decomposition T(p, c); int Q; cin >> Q; for (int i = 0; i < Q; i++){ int x, y; cin >> x >> y; x--; y--; x = id[x]; y = id[y]; if (x == y){ cout << 0 << endl; } else { int z = T.lca(x, y); int ans = d[x] + d[y] - d[z] * 2; if (G.cut[z]){ ans++; } if (G.cut[x]){ ans--; } if (G.cut[y]){ ans--; } cout << ans << endl; } } }