func_code_string: #include <bits/stdc++.h>   using namespace std;   bool debug = 1;   int n, m, k;   int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};   long long ln, lk, lm;   vector<int> mp[100005];   int r1, r2, fa[100005], s[100005], no[100005], cnt, d[100005], w[100005];   bool vis[100005];   int bfs(int x) {     queue<int> q;     q.push(x);     fa[x] = 0;     vis[x] = 1;     while (!q.empty()) {       x = q.front();       q.pop();       for (int c : mp[x])         if (!vis[c]) {           fa[c] = x;           vis[c] = 1;           q.push(c);         }     }     for (int(i) = 0; (i) < (int)(100005); (i)++) vis[i] = 0;     return x;   }   void dfs(int x, int dp, int fa, int col) {     d[x] = dp;     no[x] = col;     w[col] = max(w[col], dp);     for (int c : mp[x])       if (c != fa && !vis[c]) {         dfs(c, dp + 1, x, col);       }   }   int T[2][100005][20];   void init() {     for (int(i) = 1; (i) <= (int)(cnt); (i)++) {       T[0][i][0] = w[i] + i;       T[1][i][0] = w[i] - i;     }     for (int j = 1; (1 << j) <= cnt; j++) {       for (int i = 1; i + (1 << j) - 1 <= cnt; i++) {         for (int(x) = 0; (x) < (int)(2); (x)++)           T[x][i][j] = max(T[x][i][j - 1], T[x][i + (1 << (j - 1))][j - 1]);       }     }   }   int qym(int l, int r, int p) {     if (l > r) return -(1 << 30);     int x = log(r - l + 1) / log(2);     int y = max(T[p][l][x], T[p][r - (1 << x) + 1][x]);     return y;   }   int main() {     scanf( %d , &n);     for (int i = 0, u, v; i < n - 1; i++) {       scanf( %d%d , &u, &v);       mp[u].push_back(v);       mp[v].push_back(u);     }     r1 = bfs(1);     r2 = bfs(r1);     for (int(i) = 0; (i) < (int)(100005); (i)++) vis[i] = 0;     for (int i = r2; i; i = fa[i]) {       vis[i] = 1;       s[++cnt] = i;       no[i] = cnt;     }     for (int(i) = 1; (i) <= (int)(cnt); (i)++) dfs(s[i], 0, 0, i);     init();     scanf( %d , &m);     for (int i = 0, x, y, ans; i < m; i++) {       scanf( %d%d , &x, &y);       int l = no[x], r = no[y];       if (r < l) {         swap(l, r);         swap(x, y);       }       if (l == r) {         ans = max(l - 1, cnt - r) + min(d[x], d[y]);       } else {         int mid2 = l + r - d[x] + d[y];         if (mid2 <= l * 2) {           ans = d[y] + max(r - 1, cnt - r);         } else if (mid2 >= r * 2) {           ans = d[x] + max(l - 1, cnt - l);         } else {           int mid = mid2 / 2;           int L = max(l - 1, qym(l + 1, mid, 0) - l) + d[x];           int R = max(cnt - r, qym(mid + 1, r - 1, 1) + r) + d[y];           ans = max(L, R);         }       }       printf( %d n , ans);     }     return 0;   }   