#include<bits/stdc++.h>
#define rep(i,a) for(int (i)=0; (i)<(a); (i)++)
#define rrep(i,a) for(int (i)=a-1; (i)>=(0); (i)--)
#define rep2(i,a,b) for(int (i)=(a); (i)<(b); (i)++)
#define MOD 1000000007
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
typedef long long ll;

using namespace std;

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n,u,v; cin >> n >> u >> v; u--; v--;
  vector<vector<ll>> G(n);
  rep(i,n-1){
    ll a,b; cin >> a >> b;
    a--; b--;
    G[a].push_back(b); G[b].push_back(a);
  }
  queue<ll> q;
  q.push(u);
  vector<ll> d_u(n, 100000000);
  d_u[u] = 0;
  while(!q.empty()){
    ll now = q.front(); q.pop();
    for(ll next:G[now]){
      if(d_u[next] !=  100000000) continue;
      d_u[next] = d_u[now] + 1;
      q.push(next);
    }
  }
  q.push(v);
  vector<ll> d_v(n, 100000000);
  d_v[v] = 0;;
  while(!q.empty()){
    ll now = q.front(); q.pop();
    for(ll next:G[now]){
      if(d_v[next] !=  100000000) continue;
      d_v[next] = d_v[now] + 1;
      q.push(next);
    }
  }
  ll ans = 0;
  rep(i,n){
    if(d_u[i] < d_v[i]) ans = max(ans,d_v[i]-1);
  }
  cout << ans;

  return 0;
}
//#define _GRIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;

#define int long long

class SCC {
    private:
        int V;
        int E;

        vector<int> G[100001];
        vector<int> rG[100001];
        vector<int> vs;
        bool used[100001];
        int cmp[100001];

        auto dfs(int v) -> void {
            used[v] = true;
            for (int i = 0; i < G[v].size(); ++i)
                if (!used[G[v][i]])
                    dfs(G[v][i]);

            vs.push_back(v);
        }

        auto rdfs(int v, int k) -> void{
            used[v] = true;
            cmp[v] = k;
            for (int i = 0; i < rG[v].size(); i++)
                if (!used[rG[v][i]])
                    rdfs(rG[v][i], k);
        }

    public:
        SCC(int V, int E)
            : V(V), E(E) { }

        auto scc() -> int {
            fill(used, used + V, false);
            vs.clear();
            for (int v = 0; v < V; v++) {
                if (!used[v]) dfs(v);
            }
            fill(used, used + V, false);
            int k = 0;
            for (int i = vs.size() - 1; i >= 0; i--)
                if (!used[vs[i]])
                    rdfs(vs[i], k++);

            return k;
        }

        auto add_edge(int s, int t) {
            G[s].push_back(t);
            rG[t].push_back(s);
        }

        auto get_cmp(int a) -> int {
            return cmp[a];
        }

        auto is_union_cmp(int a, int b) -> bool {
            return cmp[a] == cmp[b];
        }
};



auto main() -> signed {
    int a, b;
    int V, E;
    cin >> V >> E;
    SCC scc(V, E);
    for (int i = 0; i < E; i++) {
        cin >> a >> b;
        scc.add_edge(a, b);
    }
    int k = scc.scc();

    int Q;
    cin >> Q;
    for (int i = 0; i < Q; i++) {
        cin >> a >> b;
        cout << scc.is_union_cmp(a, b) << endl;
    }
    return 0;
}