#include using ll = long long; using uint = unsigned int; using ull = unsigned long long; using ld = long double; template using max_heap = std::priority_queue; template using min_heap = std::priority_queue, std::greater>; constexpr int popcount(const ull v) { return v ? __builtin_popcountll(v) : 0; } constexpr int log2p1(const ull v) { return v ? 64 - __builtin_clzll(v) : 0; } constexpr int lsbp1(const ull v) { return __builtin_ffsll(v); } constexpr int clog(const ull v) { return v ? log2p1(v - 1) : 0; } constexpr ull ceil2(const ull v) { return 1ULL << clog(v); } constexpr ull floor2(const ull v) { return v ? (1ULL << (log2p1(v) - 1)) : 0ULL; } constexpr bool btest(const ull mask, const int ind) { return (mask >> ind) & 1ULL; } template bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } template constexpr T inf_v = std::numeric_limits::max() / 4; template constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; template constexpr T TEN(const int n) { return n == 0 ? T{1} : TEN(n - 1) * T{10}; } template struct fix : F { fix(F&& f) : F{std::forward(f)} {} template auto operator()(Args&&... args) const { return F::operator()(*this, std::forward(args)...); } }; template auto nd_array(int const (&szs)[n], const T x = T{}) { if constexpr (i == n) { return x; } else { return std::vector(szs[i], nd_array(szs, x)); } } class printer { public: printer(std::ostream& os_ = std::cout) : m_os{os_} { m_os << std::fixed << std::setprecision(15); } template int ln(const Args&... args) { return dump(args...), m_os << '\n', 0; } template int el(const Args&... args) { return dump(args...), m_os << std::endl, 0; } private: template void dump(const T& v) { m_os << v; } template void dump(const std::vector& vs) { for (int i = 0; i < (int)vs.size(); i++) { m_os << (i ? " " : ""), dump(vs[i]); } } template void dump(const std::vector>& vss) { for (int i = 0; i < (int)vss.size(); i++) { m_os << (0 <= i or i + 1 < (int)vss.size() ? "\n" : ""), dump(vss[i]); } } template int dump(const T& v, const Args&... args) { return dump(v), m_os << ' ', dump(args...), 0; } std::ostream& m_os; }; printer out; class range { private: struct itr { itr(const int start = 0, const int step = 1) : m_cnt{start}, m_step{step} {} bool operator!=(const itr& it) const { return m_cnt != it.m_cnt; } int& operator*() { return m_cnt; } itr& operator++() { return m_cnt += m_step, *this; } int m_cnt, m_step; }; int m_start, m_end, m_step; public: range(const int start, const int end, const int step = 1) : m_start{start}, m_end{end}, m_step{step} { assert(m_step != 0); if (m_step > 0) { m_end = m_start + std::max(m_step - 1, m_end - m_start + m_step - 1) / m_step * m_step; } if (m_step < 0) { m_end = m_start - std::max(-m_step - 1, m_start - m_end - m_step - 1) / (-m_step) * (-m_step); } } itr begin() const { return itr{m_start, m_step}; } itr end() const { return itr{m_end, m_step}; } }; range rep(const int end, const int step = 1) { return range(0, end, step); } range per(const int rend, const int step = -1) { return range(rend - 1, -1, step); } class scanner { public: scanner(std::istream& is_ = std::cin) : m_is{is_} { m_is.tie(nullptr), std::ios::sync_with_stdio(false); } template T val() { T v; return m_is >> v, v; } template T val(const T offset) { return val() - offset; } template std::vector vec(const int n) { return make_v(n, [this]() { return val(); }); } template std::vector vec(const int n, const T offset) { return make_v(n, [this, offset]() { return val(offset); }); } template std::vector> vvec(const int n0, const int n1) { return make_v>(n0, [this, n1]() { return vec(n1); }); } template std::vector> vvec(const int n0, const int n1, const T offset) { return make_v>(n0, [this, n1, offset]() { return vec(n1, offset); }); } template auto tup() { return std::tuple...>{val()...}; } template auto tup(const Args&... offsets) { return std::tuple...>{val(offsets)...}; } private: template std::vector make_v(const int n, F f) { std::vector ans; for (int i = 0; i < n; i++) { ans.push_back(f()); } return ans; } std::istream& m_is; }; scanner in; template class graph { public: graph(const int sz) : m_size{sz}, m_eis(sz) {} void add_edge(const int u, const int v, const T c, const bool bi = false) { const int ei = (int)m_us.size(); m_us.push_back(u), m_vs.push_back(v), m_cs.push_back(c); m_eis[u].push_back(ei); if (bi) { m_eis[v].push_back(ei); } } void add_edge(const int u, const int v, const bool bi = false) { add_edge(u, v, T{1}, bi); } std::tuple edge(const int u, const int i) const { return std::make_tuple(u, m_us[i] ^ m_vs[i] ^ u, m_cs[i]); } const std::vector& operator[](const int u) const { return m_eis[u]; } std::vector& operator[](const int u) { return m_eis[u]; } int size() const { return m_size; } friend std::ostream& operator<<(std::ostream& os, const graph& g) { for (int u = 0; u < g.size(); u++) { for (const int ei : g[u]) { const auto [from, to, cost] = g.edge(u, ei); os << "[" << ei << "]: " << from << "->" << to << "(" << cost << ")\n"; } } return os; } private: int m_size; std::vector m_us, m_vs; std::vector m_cs; std::vector> m_eis; }; template class hl_decomp { public: hl_decomp(graph& g, const int r = 0) : m_pars(g.size(), -1), m_tops{m_pars}, m_ins{m_pars}, m_ords{m_pars}, m_outs{m_pars} { const int N = g.size(); std::vector szs(N, 1); auto dfs1 = [&](auto self, const int u, const int p) -> int { m_pars[u] = p; for (int& ei : g[u]) { [[maybe_unused]] const auto& [from, to, cost] = g.edge(u, ei); if (p == to) { continue; } szs[u] += self(self, to, u); if (szs[std::get<1>(g.edge(u, g[u][0]))] < szs[to]) { std::swap(g[u][0], ei); } } return szs[u]; }; dfs1(dfs1, r, -1); m_tops[r] = r; auto dfs2 = [&](auto&& self, const int u, const int p, int& ind) -> void { m_ins[u] = ind++, m_ords[m_ins[u]] = u; for (const int ei : g[u]) { [[maybe_unused]] const auto& [from, to, cost] = g.edge(u, ei); if (to == p) { continue; } m_tops[to] = (ei == g[u][0] ? m_tops[u] : to); self(self, to, u, ind); } m_outs[u] = ind; }; int ind = 0; dfs2(dfs2, r, -1, ind); } int pos(const int v) const { return m_ins[v]; } int at(const int n) const { return m_ords[n]; } std::pair sub(const int v) const { return {m_ins[v], m_outs[v]}; } std::vector> path(int u, int v) const { using P = std::pair; std::vector

head, tail; for (int pu = m_tops[u], pv = m_tops[v]; pu != pv;) { if (m_ins[pu] < m_ins[pv]) { tail.push_back({m_ins[pv], m_ins[v]}); v = m_pars[pv], pv = m_tops[v]; } else { head.push_back({m_ins[u], m_ins[pu]}); u = m_pars[pu], pu = m_tops[u]; } } head.push_back({m_ins[u], m_ins[v]}); std::reverse(tail.begin(), tail.end()); for (const auto& p : tail) { head.push_back(p); } return head; } friend std::ostream& operator<<(std::ostream& os, const hl_decomp& hld) { os << "ord = {"; for (const int v : hld.m_ords) { os << v << ","; } os << "}\ntops = {"; for (const int v : hld.m_tops) { os << v << ","; } return os << "}"; } private: std::vector m_pars, m_tops, m_ins, m_ords, m_outs; }; template class static_rsq { public: static_rsq() = default; void push_back(const T& v) { m_sums.push_back(m_sums.back() + v); } T operator()(const int l, const int r) const { return m_sums[r] - m_sums[l]; } private: std::vector m_sums{T{}}; }; using namespace std; class Articulation { public: struct bcnode; private: const int V; vector ord, low; bool check(const int u) { for (const int v : G[u]) { if (ord[v] < 0) return true; } return false; } void identify_block(const int u, const int v, bool* used, stack>& st, const vector& bcnode_id) { vector> block; vector cut_vertex; while (!st.empty()) { const pair p = st.top(); st.pop(), block.push_back(p); if (art[p.first] && !used[p.first]) { cut_vertex.push_back(p.first); used[p.first] = true; } if (art[p.second] && !used[p.second]) { cut_vertex.push_back(p.second); used[p.second] = true; } if (p == (pair){u, v}) break; } const int block_id = (int)bctree.size(); bctree.push_back(vector()); bcnodes.emplace_back(true, move(block)); for (const int ver : cut_vertex) { bctree[block_id].push_back(bcnode_id[ver]); bctree[bcnode_id[ver]].push_back(block_id); used[ver] = false; } } void dfs(const int u, const int p, int& tm, bool* used, stack>& st, vector& bcnode_id) { ord[u] = low[u] = tm++; bool multi_edge = false; for (const int v : G[u]) { if (ord[v] < 0) { st.push({u, v}); dfs(v, u, tm, used, st, bcnode_id); low[u] = min(low[u], low[v]); if (ord[u] <= low[v]) { if (!art[u] && (p >= 0 || check(u))) { art[u] = true; bcnode_id[u] = (int)bctree.size(); bctree.push_back(vector()); bcnodes.emplace_back(false, vector>{{u, u}}); } identify_block(u, v, used, st, bcnode_id); } } else if (v == p) { if (multi_edge) { st.push({u, v}); low[u] = min(low[u], ord[v]); } else { multi_edge = true; } } else if (ord[v] < ord[u]) { st.push({u, v}); low[u] = min(low[u], ord[v]); } } } public: vector> G; vector art; struct bcnode { bool isBlock; vector> component; friend std::ostream& operator<<(std::ostream& os, const bcnode& node) { os << (node.isBlock ? "Block" : "Cut") << ":["; for (const auto& [u, v] : node.component) { os << "<" << u << "," << v << ">"; } os << "]\n"; return os; } bcnode(bool _isBlock, vector>&& _component) : isBlock(_isBlock), component(move(_component)) {} }; vector> bctree; vector bcnodes; Articulation(const int node_size) : V(node_size), ord(V, -1), low(V), G(V), art(V, false) {} void add_edge(const int a, const int b) { G[a].push_back(b), G[b].push_back(a); } int solve() { int tm = 0; bool* used = new bool[V](); stack> st; vector bcnode_id(V, -1); for (int i = 0; i < V; ++i) { if (ord[i] < 0) dfs(i, -1, tm, used, st, bcnode_id); if (G[i].empty()) { bctree.push_back(vector()); bcnodes.emplace_back(true, vector>()); } } delete[] used; return (int)bctree.size(); } }; int main() { const auto [N, M] = in.tup(); std::vector us(M), vs(M); Articulation arts(N); for (const int i : rep(M)) { const auto [u, v] = in.tup(1, 1); arts.add_edge(u, v); } arts.solve(); static_cast(0); static_cast(0); static_cast(0); const int v = arts.bcnodes.size(); std::vector cs(N); std::vector> vss(v); for (const int i : rep(v)) { if (arts.bcnodes[i].isBlock) { const auto es = arts.bcnodes[i].component; for (const auto [u, v] : es) { if (not arts.art[u]) { cs[u] = i; } if (not arts.art[v]) { cs[v] = i; } } } else { cs[arts.bcnodes[i].component[0].first] = i; } } for (const int i : rep(N)) { vss[cs[i]].push_back(i); } static_cast(0); static_cast(0); graph g(v); for (const int i : rep(v)) { for (const int j : arts.bctree[i]) { g.add_edge(i, j); } } hl_decomp hld(g); static_rsq rsq; std::vector is(v), ws(v); for (const int i : rep(v)) { const int c = hld.at(i); int sum = 0; for (const int v : vss[c]) { sum += arts.art[v]; } rsq.push_back(sum); is[i] = c, ws[i] = sum; } static_cast(0); static_cast(0); const auto Q = in.val(); for (const int q : rep(Q)) { const auto [x, y] = in.tup(1, 1); const int cx = cs[x], cy = cs[y]; if (cx == cy) { out.ln(0); } else { const auto rs = hld.path(cx, cy); int num = 0; for (auto [l, r] : rs) { if (l > r) { std::swap(l, r); } r++; static_cast(0); num += rsq(l, r); } if (arts.art[x]) { num--; } if (arts.art[y]) { num--; } out.ln(num); } } return 0; }