Skip to content
c++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 1e4 + 5, M = 200005;  // M 开至无向边数的 2 倍以上
int n, m, q;
struct Edge {
    int u, v;
} e[M];
int head[N], to[M], nxt[M], cnt = 2;
int dfn[N], low[N], tot;
int stk[N], top;
int bel[N], ebcc_cnt;

void add(int u, int v) { to[cnt] = v, nxt[cnt] = head[u], head[u] = cnt++; }

void tarjan(int u, int in_edge) {
    dfn[u] = low[u] = ++tot;
    stk[++top] = u;
    for (int i = head[u]; i; i = nxt[i]) {
        if (i == (in_edge ^ 1)) continue;
        int v = to[i];
        if (!dfn[v]) {
            tarjan(v, i);
            low[u] = min(low[u], low[v]);
        } else {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (dfn[u] == low[u]) {
        ++ebcc_cnt;
        do {
            bel[stk[top]] = ebcc_cnt;
        } while (stk[top--] != u);
    }
}
struct HLD {
    int n;
    std::vector<std::vector<int>> adj;
    std::vector<int> siz, dep, top, son, parent;

    explicit HLD(int n) : n(n), adj(n + 1), siz(n + 1), dep(n + 1), top(n + 1), son(n + 1), parent(n + 1) {}

    void add(int u, int v) {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    void dfs1(int u) {
        siz[u] = 1;
        dep[u] = dep[parent[u]] + 1;
        for (int v : adj[u]) {
            if (v == parent[u]) continue;
            parent[v] = u;
            dfs1(v);
            siz[u] += siz[v];
            if (siz[v] > siz[son[u]]) son[u] = v;
        }
    }

    void dfs2(int u, int up) {
        top[u] = up;
        if (son[u]) dfs2(son[u], up);
        for (int v : adj[u]) {
            if (v == parent[u] || v == son[u]) continue;
            dfs2(v, v);
        }
    }

    void work(int root = 1) {
        dfs1(root);
        dfs2(root, root);
    }

    int lca(int u, int v) const {
        while (top[u] != top[v]) {
            if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
            u = parent[top[u]];
        }
        return dep[u] < dep[v] ? u : v;
    }

    ll calc(int u, int v) const { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }
};
std::string decToBin(long long n) {
    if(n == 0) return "0";
    string s;
    unsigned long long x = n;
    while(x){
        s += char('0'+(x&1));
        x >>=1;
    }
    reverse(s.begin(),s.end());
    return s;
}
void fc() {
    std::cin >> n >> m;
    std::set<std::pair<int,int>>s;
    for (int i = 0; i < m; i++) {
        std::cin >> e[i].u >> e[i].v;
        int u=e[i].u,v=e[i].v;
        if(u>v)std::swap(u,v);
        if(s.count({u,v}))continue;
        s.insert({u,v});
        add(e[i].u, e[i].v);
        add(e[i].v, e[i].u);
    }

    for (int i = 1; i <= n; i++) {
        if (!dfn[i]) tarjan(i, 0);
    }

    HLD hld(ebcc_cnt);
    for (int i = 0; i < m; i++) {
        int u = bel[e[i].u], v = bel[e[i].v];
        if (u == v) continue;
        hld.add(u, v);
    }

    hld.work();

    std::cin >> q;
    while (q--) {
        int u, v;
        std::cin >> u >> v;
        std::cout << decToBin(hld.calc(bel[u], bel[v])+1) << "\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // std::cin>>t;
    while (t--) fc();
    return 0;
}