#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int> > G, P, dp1, dp2, far, son;
vector<long long int> first, last, level;
int L, cnt = 0;
void dfs(int v, int p, int l) {
  first[v] = cnt++;
  level[v] = l;
  P[v][0] = p;
  for (int i = 1; i <= L; ++i) P[v][i] = P[P[v][i - 1]][i - 1];
  vector<vector<long long int> > V;
  for (int i = 0; i < G[v].size(); ++i) {
    int u = G[v][i];
    if (u == p) continue;
    dfs(u, v, l + 1);
    V.push_back({-far[u][0], u});
  }
  sort(V.begin(), V.end());
  if (V.size() > 0) {
    far[v][0] = 1 - V[0][0];
    son[v][0] = V[0][1];
  }
  if (V.size() > 1) {
    far[v][1] = 1 - V[1][0];
    son[v][1] = V[1][1];
  }
  if (V.size() > 2) {
    far[v][2] = 1 - V[2][0];
    son[v][2] = V[2][1];
  }
  last[v] = cnt++;
}
bool ancestor(int u, int v) {
  return (first[u] <= first[v] and last[u] >= last[v]);
}
int LCA(int u, int v) {
  if (ancestor(u, v)) return u;
  if (ancestor(v, u)) return v;
  for (int i = L; i >= 0; --i) {
    if (not ancestor(P[u][i], v)) u = P[u][i];
  }
  return P[u][0];
}
int find_up(int u, int d) {
  for (int i = L; i >= 0; --i) {
    int pw = 1 << i;
    if (pw > d) continue;
    u = P[u][i];
    d -= pw;
  }
  return u;
}
long long int rec1(int v, int i) {
  long long int& ans = dp1[v][i];
  if (ans != -1) return ans;
  if (not i) {
    int ans = 1;
    if (son[P[v][0]][0] == v)
      ans += far[P[v][0]][1];
    else
      ans += far[P[v][0]][0];
    return ans;
  }
  ans = rec1(P[v][i - 1], i - 1) + (1 << (i - 1));
  ans = max(ans, rec1(v, i - 1));
  return ans;
}
long long int rec2(int v, int i) {
  long long int& ans = dp2[v][i];
  if (ans != -1) return ans;
  if (not i) {
    int ans = 0;
    if (son[P[v][0]][0] == v)
      ans += far[P[v][0]][1];
    else
      ans += far[P[v][0]][0];
    ans = max(ans, 1);
    return ans;
  }
  ans = rec2(P[v][i - 1], i - 1);
  ans = max(ans, rec2(v, i - 1) + (1 << (i - 1)));
  return ans;
}
long long int dist1(int v, int h) {
  long long int ans = 0;
  int d = 0;
  for (int i = L; i >= 0; --i) {
    if ((1 << i) > h) continue;
    ans = max(ans, d + rec1(v, i));
    v = P[v][i];
    d += (1 << i);
    h -= (1 << i);
  }
  return ans;
}
long long int dist2(int v, int h) {
  long long int ans = 0;
  for (int i = L; i >= 0; --i) {
    if ((1 << i) > h) continue;
    ans = max(ans, h - (1 << i) + rec2(v, i));
    v = P[v][i];
    h -= (1 << i);
  }
  return ans;
}
void solve_middle(int x, int y, int l) {
  long long int d = level[x] + level[y] - 2 * level[l];
  int prevx = find_up(x, level[x] - level[l] - 1);
  int prevy = find_up(y, level[y] - level[l] - 1);
  long long int rooted = far[l][0];
  if (son[l][0] == prevx or son[l][0] == prevy) {
    rooted = far[l][1];
    if (son[l][1] == prevx or son[l][1] == prevy) rooted = far[l][2];
  }
  rooted = max(rooted, dist1(l, level[l]));
  long long int ans1 = max(far[x][0], max(dist1(x, d / 2 - 1), d / 2 + rooted));
  long long int ans2 = max(far[y][0], max(dist1(y, d / 2 - 1), d / 2 + rooted));
  cout << max(ans1, ans2) << '\n';
}
void solve_ancestor(int x, int y) {
  long long int d = level[x] - level[y];
  int mx = find_up(x, d / 2);
  int my = find_up(x, (d / 2) + 1);
  long long int ans1 = max(far[x][0], dist1(x, level[x] - level[mx]));
  long long int ans2 = max(dist1(y, level[y]), dist2(my, level[my] - level[y]));
  int prevy = find_up(x, level[x] - level[y] - 1);
  long long int root = far[y][0];
  if (son[y][0] == prevy) root = far[y][1];
  ans2 = max(ans2, root);
  int prevxx = find_up(x, level[x] - level[my] - 1);
  long long int root2 = far[my][0];
  if (son[my][0] == prevxx) root2 = far[my][1];
  ans2 = max(ans2, root2 + level[my] - level[y]);
  cout << max(ans1, ans2) << '\n';
}
void solve_lca(int x, int y, int l) {
  long long int d = level[x] + level[y] - 2 * level[l];
  int mx = find_up(x, d / 2), my = l;
  int prevx = mx, prevy = find_up(y, level[y] - level[l] - 1);
  long long int ans1 = max(far[x][0], dist1(x, level[x] - level[mx]));
  long long int ans2 = max(far[y][0], dist1(y, level[y] - level[l] - 1));
  long long int root = far[l][0];
  if (son[l][0] == prevx or son[l][0] == prevy) {
    root = far[l][1];
    if (son[l][1] == prevx or son[l][1] == prevy) root = far[l][2];
  }
  root = max(root, dist1(l, level[l]));
  ans2 = max(ans2, root + level[y] - level[l]);
  cout << max(ans1, ans2) << '\n';
}
void solve_normal(int x, int y, int l) {
  long long int d = level[x] + level[y] - 2 * level[l];
  int mx = find_up(x, d / 2);
  int my = find_up(x, (d + 1) / 2);
  long long int ans1 = max(far[x][0], dist1(x, level[x] - level[mx]));
  long long int ans2 = max(far[y][0], dist1(y, level[y] - level[l] - 1));
  int prevx = find_up(x, level[x] - level[l] - 1);
  int prevy = find_up(y, level[y] - level[l] - 1);
  long long int root = far[l][0];
  if (son[l][0] == prevx or son[l][0] == prevy) {
    root = far[l][1];
    if (son[l][1] == prevx or son[l][1] == prevy) root = far[l][2];
  }
  root = max(root, dist1(l, level[l]));
  root = max(root, 1 + dist2(my, level[my] - level[prevx]));
  long long int prevxx = find_up(x, level[x] - level[my] - 1);
  long long int root2 = far[my][0];
  if (son[my][0] == prevxx) root2 = far[my][1];
  root = max(root, root2 + level[my] - level[l]);
  ans2 = max(ans2, root + level[y] - level[l]);
  cout << max(ans1, ans2) << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  L = ceil(log2(n));
  G = vector<vector<long long int> >(n);
  first = last = level = vector<long long int>(n, -1);
  far = son = vector<vector<long long int> >(n, vector<long long int>(3, 0));
  P = dp1 = dp2 =
      vector<vector<long long int> >(n, vector<long long int>(L + 1, -1));
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(0, 0, 0);
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    if (level[y] > level[x]) swap(x, y);
    int l = LCA(x, y);
    if (y == l) {
      solve_ancestor(x, y);
      continue;
    }
    if (level[x] == level[y]) {
      solve_middle(x, y, l);
      continue;
    }
    if (level[x] == level[y] + 1) {
      solve_lca(x, y, l);
      continue;
    }
    solve_normal(x, y, l);
  }
}

### Prompt

Your task is to create a CPP solution to the following problem:

The Finals of the "Russian Code Cup" 2214 will be held in n hotels. Two hotels (let's assume that they are the main hotels), will host all sorts of events, and the remaining hotels will accommodate the participants. The hotels are connected by n - 1 roads, you can get from any hotel to any other one.

The organizers wonder what is the minimum time all the participants need to get to the main hotels, if each participant goes to the main hotel that is nearest to him and moving between two hotels connected by a road takes one unit of time.

The hosts consider various options for the location of the main hotels. For each option help the organizers to find minimal time.

Input

The first line contains an integer n (2 ≤ n ≤ 100000) — the number of hotels. The next n - 1 lines contain two integers each — the numbers of the hotels that have a road between them. Consider hotels are numbered from 1 to n.

The next line contains an integer m (1 ≤ m ≤ 100000) — the number of queries. The following m lines contains two distinct integers each — the numbers of the hotels we assume to be the main.

Output

For each request of the organizers print a single integer — the time that all participants need to reach the main hotels.

Examples

Input

3
2 3
3 1
3
2 1
2 3
3 1


Output

1
1
1


Input

4
1 4
1 2
2 3
3
1 4
1 3
2 3


Output

2
1
2

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long int> > G, P, dp1, dp2, far, son;
vector<long long int> first, last, level;
int L, cnt = 0;
void dfs(int v, int p, int l) {
  first[v] = cnt++;
  level[v] = l;
  P[v][0] = p;
  for (int i = 1; i <= L; ++i) P[v][i] = P[P[v][i - 1]][i - 1];
  vector<vector<long long int> > V;
  for (int i = 0; i < G[v].size(); ++i) {
    int u = G[v][i];
    if (u == p) continue;
    dfs(u, v, l + 1);
    V.push_back({-far[u][0], u});
  }
  sort(V.begin(), V.end());
  if (V.size() > 0) {
    far[v][0] = 1 - V[0][0];
    son[v][0] = V[0][1];
  }
  if (V.size() > 1) {
    far[v][1] = 1 - V[1][0];
    son[v][1] = V[1][1];
  }
  if (V.size() > 2) {
    far[v][2] = 1 - V[2][0];
    son[v][2] = V[2][1];
  }
  last[v] = cnt++;
}
bool ancestor(int u, int v) {
  return (first[u] <= first[v] and last[u] >= last[v]);
}
int LCA(int u, int v) {
  if (ancestor(u, v)) return u;
  if (ancestor(v, u)) return v;
  for (int i = L; i >= 0; --i) {
    if (not ancestor(P[u][i], v)) u = P[u][i];
  }
  return P[u][0];
}
int find_up(int u, int d) {
  for (int i = L; i >= 0; --i) {
    int pw = 1 << i;
    if (pw > d) continue;
    u = P[u][i];
    d -= pw;
  }
  return u;
}
long long int rec1(int v, int i) {
  long long int& ans = dp1[v][i];
  if (ans != -1) return ans;
  if (not i) {
    int ans = 1;
    if (son[P[v][0]][0] == v)
      ans += far[P[v][0]][1];
    else
      ans += far[P[v][0]][0];
    return ans;
  }
  ans = rec1(P[v][i - 1], i - 1) + (1 << (i - 1));
  ans = max(ans, rec1(v, i - 1));
  return ans;
}
long long int rec2(int v, int i) {
  long long int& ans = dp2[v][i];
  if (ans != -1) return ans;
  if (not i) {
    int ans = 0;
    if (son[P[v][0]][0] == v)
      ans += far[P[v][0]][1];
    else
      ans += far[P[v][0]][0];
    ans = max(ans, 1);
    return ans;
  }
  ans = rec2(P[v][i - 1], i - 1);
  ans = max(ans, rec2(v, i - 1) + (1 << (i - 1)));
  return ans;
}
long long int dist1(int v, int h) {
  long long int ans = 0;
  int d = 0;
  for (int i = L; i >= 0; --i) {
    if ((1 << i) > h) continue;
    ans = max(ans, d + rec1(v, i));
    v = P[v][i];
    d += (1 << i);
    h -= (1 << i);
  }
  return ans;
}
long long int dist2(int v, int h) {
  long long int ans = 0;
  for (int i = L; i >= 0; --i) {
    if ((1 << i) > h) continue;
    ans = max(ans, h - (1 << i) + rec2(v, i));
    v = P[v][i];
    h -= (1 << i);
  }
  return ans;
}
void solve_middle(int x, int y, int l) {
  long long int d = level[x] + level[y] - 2 * level[l];
  int prevx = find_up(x, level[x] - level[l] - 1);
  int prevy = find_up(y, level[y] - level[l] - 1);
  long long int rooted = far[l][0];
  if (son[l][0] == prevx or son[l][0] == prevy) {
    rooted = far[l][1];
    if (son[l][1] == prevx or son[l][1] == prevy) rooted = far[l][2];
  }
  rooted = max(rooted, dist1(l, level[l]));
  long long int ans1 = max(far[x][0], max(dist1(x, d / 2 - 1), d / 2 + rooted));
  long long int ans2 = max(far[y][0], max(dist1(y, d / 2 - 1), d / 2 + rooted));
  cout << max(ans1, ans2) << '\n';
}
void solve_ancestor(int x, int y) {
  long long int d = level[x] - level[y];
  int mx = find_up(x, d / 2);
  int my = find_up(x, (d / 2) + 1);
  long long int ans1 = max(far[x][0], dist1(x, level[x] - level[mx]));
  long long int ans2 = max(dist1(y, level[y]), dist2(my, level[my] - level[y]));
  int prevy = find_up(x, level[x] - level[y] - 1);
  long long int root = far[y][0];
  if (son[y][0] == prevy) root = far[y][1];
  ans2 = max(ans2, root);
  int prevxx = find_up(x, level[x] - level[my] - 1);
  long long int root2 = far[my][0];
  if (son[my][0] == prevxx) root2 = far[my][1];
  ans2 = max(ans2, root2 + level[my] - level[y]);
  cout << max(ans1, ans2) << '\n';
}
void solve_lca(int x, int y, int l) {
  long long int d = level[x] + level[y] - 2 * level[l];
  int mx = find_up(x, d / 2), my = l;
  int prevx = mx, prevy = find_up(y, level[y] - level[l] - 1);
  long long int ans1 = max(far[x][0], dist1(x, level[x] - level[mx]));
  long long int ans2 = max(far[y][0], dist1(y, level[y] - level[l] - 1));
  long long int root = far[l][0];
  if (son[l][0] == prevx or son[l][0] == prevy) {
    root = far[l][1];
    if (son[l][1] == prevx or son[l][1] == prevy) root = far[l][2];
  }
  root = max(root, dist1(l, level[l]));
  ans2 = max(ans2, root + level[y] - level[l]);
  cout << max(ans1, ans2) << '\n';
}
void solve_normal(int x, int y, int l) {
  long long int d = level[x] + level[y] - 2 * level[l];
  int mx = find_up(x, d / 2);
  int my = find_up(x, (d + 1) / 2);
  long long int ans1 = max(far[x][0], dist1(x, level[x] - level[mx]));
  long long int ans2 = max(far[y][0], dist1(y, level[y] - level[l] - 1));
  int prevx = find_up(x, level[x] - level[l] - 1);
  int prevy = find_up(y, level[y] - level[l] - 1);
  long long int root = far[l][0];
  if (son[l][0] == prevx or son[l][0] == prevy) {
    root = far[l][1];
    if (son[l][1] == prevx or son[l][1] == prevy) root = far[l][2];
  }
  root = max(root, dist1(l, level[l]));
  root = max(root, 1 + dist2(my, level[my] - level[prevx]));
  long long int prevxx = find_up(x, level[x] - level[my] - 1);
  long long int root2 = far[my][0];
  if (son[my][0] == prevxx) root2 = far[my][1];
  root = max(root, root2 + level[my] - level[l]);
  ans2 = max(ans2, root + level[y] - level[l]);
  cout << max(ans1, ans2) << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  L = ceil(log2(n));
  G = vector<vector<long long int> >(n);
  first = last = level = vector<long long int>(n, -1);
  far = son = vector<vector<long long int> >(n, vector<long long int>(3, 0));
  P = dp1 = dp2 =
      vector<vector<long long int> >(n, vector<long long int>(L + 1, -1));
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(0, 0, 0);
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    if (level[y] > level[x]) swap(x, y);
    int l = LCA(x, y);
    if (y == l) {
      solve_ancestor(x, y);
      continue;
    }
    if (level[x] == level[y]) {
      solve_middle(x, y, l);
      continue;
    }
    if (level[x] == level[y] + 1) {
      solve_lca(x, y, l);
      continue;
    }
    solve_normal(x, y, l);
  }
}

```