#include <bits/stdc++.h>   using namespace std;   const int INF = 0x60000000;   const int MINF = -1000000000;   const int mod = 99999997;   const long long cons = 100000;   const double pi = 3.141592653589793;   int lim;   int p[100005][20];   int depth[100005];   pair<int, int> dmax[100005][3];   int up[100005][20];   int down[100005][20];   vector<vector<int> > adj;   int n;   void dfs(int u, int prev) {     if (prev != -1) {       for (int i = 2; i >= 0; i--) {         if (dmax[prev][i].second == u) continue;         up[u][0] = dmax[prev][i].first + 1;         down[u][0] = dmax[prev][i].first;         break;       }     }     p[u][0] = prev;     for (int j = 0; j <= lim; j++) {       if (p[u][j] == -1) continue;       p[u][j + 1] = p[p[u][j]][j];       up[u][j + 1] = max(up[p[u][j]][j] + (1 << j), up[u][j]);       down[u][j + 1] = max(down[u][j] + (1 << j), down[p[u][j]][j]);     }     for (auto v : adj[u]) {       if (v == prev) continue;       depth[v] = depth[u] + 1;       dfs(v, u);     }   }   int getPar(int u, int dist) {     if (dist < 0) return -1;     for (int i = 0; i < lim; i++)       if (dist & (1 << i)) u = p[u][i];     return u;   }   int getLca(int u, int v) {     if (depth[u] > depth[v]) swap(u, v);     v = getPar(v, depth[v] - depth[u]);     if (u == v) return u;     for (int i = lim; i >= 0; i--) {       if (p[u][i] == p[v][i]) continue;       u = p[u][i];       v = p[v][i];     }     return p[u][0];   }   int getDist(int u, int v) {     int lca = getLca(u, v);     return depth[u] + depth[v] - 2 * depth[lca];   }   void buildDmax(int u, int prev) {     for (auto v : adj[u]) {       if (v == prev) continue;       buildDmax(v, u);       if (dmax[u][0].first < dmax[v][2].first + 1) {         dmax[u][0].first = dmax[v][2].first + 1;         dmax[u][0].second = v;       }       sort(dmax[u], dmax[u] + 3);     }   }   void build() {     for (lim = 0; (1 << lim) < n; lim++)       ;     buildDmax(1, -1);     dfs(1, -1);   }   int getUMax(int u, int d) {     if (d < 0) return -1e7;     int ret = 0;     int added = 0;     for (int i = 0; i < lim; i++) {       if (d & (1 << i)) {         ret = max(ret, up[u][i] + added);         added |= (1 << i);         u = p[u][i];       }     }     return ret;   }   int getDMax(int u, int d) {     if (d < 0) return -1e7;     int ret = 0;     int added = d;     for (int i = 0; i < lim; i++) {       if (d & (1 << i)) {         added ^= (1 << i);         ret = max(ret, down[u][i] + added + 1);         u = p[u][i];       }     }     return ret;   }   int query(int u, int v) {     if (depth[u] < depth[v]) swap(u, v);     int lca = getLca(u, v);     int d = getDist(u, v);     int center = getPar(u, (d - 1) / 2);     int vld = getDist(v, lca);     int ans = dmax[u][2].first;     if (lca != v) ans = max(ans, dmax[v][2].first);     ans = max(ans, getUMax(v, vld - 1));     ans = max(ans, getUMax(lca, depth[lca]) + vld);     ans = max(ans, getUMax(u, depth[u] - depth[center]));     ans = max(ans, getDMax(center, depth[center] - depth[lca] - 1) + vld);     for (int i = 2; i >= 0; i--) {       if (dmax[lca][i].second == getPar(v, depth[v] - depth[lca] - 1) ||           dmax[lca][i].second == getPar(u, depth[u] - depth[lca] - 1))         continue;       ans = max(ans, dmax[lca][i].first + vld);       break;     }     return ans;   }   int main() {     scanf( %d , &n);     adj.resize(n + 1);     for (int i = 0; i < n - 1; i++) {       int u, v;       scanf( %d%d , &u, &v);       adj[u].push_back(v);       adj[v].push_back(u);     }     build();     int q;     scanf( %d , &q);     while (q--) {       int u, v;       scanf( %d%d , &u, &v);       printf( %d n , query(u, v));     }     return 0;   }