#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int INF = 2e9;
const long long INF64 = 1e18;
const long double EPS = 1e-16;
const long double PI = acos(-1);
const long long MD = 1551513443;
const long long T = 25923;
const int N = 100015;
const int M = 25;
const int DEBUG = 0;
const int MAGIC = 320;
int n, h[N], maxi[N], dp_down[N], dp_up[N], up[20][N], max_up[20][N],
    max_down[20][N], tin[N], tout[N], timer, ppiopipoiuyio[N], dd[N];
vector<int> g[N];
vector<int> dp[N];
vector<pair<int, int> > b[N];
void dfs0(int v, int p) {
  dp[v].resize(int((g[v]).size()));
  for (int i = 0; i < int(int((g[v]).size())); i++) {
    int u = g[v][i];
    if (u == p) continue;
    h[u] = h[v] + 1;
    dfs0(u, v);
    dp[v][i] = maxi[u] + 1;
    maxi[v] = max(maxi[v], maxi[u] + 1);
    b[v].push_back(make_pair(maxi[u] + 1, u));
  }
  sort((b[v]).begin(), (b[v]).end());
  reverse((b[v]).begin(), (b[v]).end());
}
void dfs1(int v, int p, int d1) {
  dd[v] = d1;
  int k = int((g[v]).size());
  int s1[k], s2[k];
  for (int i = 0; i < int(k); i++) s1[i] = s2[i] = dp[v][i];
  for (int i = 1; i < k; i++) s1[i] = max(s1[i], s1[i - 1]);
  for (int i = k - 2; i >= 0; i--) s2[i] = max(s2[i], s2[i + 1]);
  for (int i = 0; i < int(k); i++) {
    int u = g[v][i];
    if (u == p) continue;
    int d_new = d1;
    if (i > 0) d_new = max(d_new, s1[i - 1]);
    if (i + 1 < k) d_new = max(d_new, s2[i + 1]);
    dfs1(u, v, d_new + 1);
  }
  for (int i = 0; i < int(k); i++) {
    int u = g[v][i];
    if (u == p) continue;
    int ans = 0;
    if (i) ans = s1[i - 1];
    if (i + 1 < k) ans = max(ans, s2[i + 1]);
    dp_down[u] = ans - h[v];
    dp_up[u] = ans + h[v];
  }
}
void dfs2(int v, int p) {
  ppiopipoiuyio[v] = p;
  tin[v] = timer++;
  up[0][v] = p;
  max_up[0][v] = dp_up[v];
  max_down[0][v] = dp_down[v];
  for (int i = 1; i < 20; i++) {
    up[i][v] = up[i - 1][up[i - 1][v]];
    max_up[i][v] = max(max_up[i - 1][v], max_up[i - 1][up[i - 1][v]]);
    max_down[i][v] = max(max_down[i - 1][v], max_down[i - 1][up[i - 1][v]]);
  }
  for (int u : g[v])
    if (u != p) dfs2(u, v);
  tout[v] = timer++;
}
inline bool check(int a, int b) {
  return (tin[a] <= tin[b] && tout[a] >= tin[b]);
}
inline int lca(int a, int b) {
  if (check(a, b)) return a;
  if (check(b, a)) return b;
  for (int i = 19; i >= 0; i--)
    if (!check(up[i][a], b)) a = up[i][a];
  return up[0][a];
}
inline int push_up(int v, int z) {
  for (int i = 19; i >= 0; i--)
    if ((1 << i) <= z) {
      v = up[i][v];
      z -= (1 << i);
    }
  return v;
}
inline pair<int, int> get_max(int s, int z) {
  pair<int, int> ans = make_pair(-INF, -INF);
  for (int i = 19; i >= 0; i--)
    if ((1 << i) <= z) {
      ans = make_pair(max(ans.first, max_down[i][s]),
                      max(ans.second, max_up[i][s]));
      s = up[i][s];
      z -= (1 << i);
    }
  return ans;
}
int mm = 0;
bool use[N];
void dfs_debug_lca(int v, int p, int h) {
  mm = max(mm, h);
  for (int u : g[v])
    if (u != p && !use[u]) dfs_debug_lca(u, v, h + 1);
}
inline int max_lca(int first, int second, int z) {
  int ans = dd[z];
  for (int i = 0; i < int(int((b[z]).size())); i++) {
    int u = b[z][i].second;
    if (u == ppiopipoiuyio[z]) continue;
    if (!check(u, first) && !check(u, second)) {
      ans = max(ans, maxi[u] + 1);
      break;
    }
  }
  if (DEBUG) {
    mm = 0;
    dfs_debug_lca(z, z, 0);
    assert(ans == mm);
  }
  return ans;
}
inline int get_ans(int first, int second, int z, int qq) {
  int ans = 0, s1 = h[first] - h[z] + 1, s2 = h[second] - h[z] + 1;
  if (first != z || first == second) ans = max(ans, maxi[first]);
  if (qq < s1)
    ans = max(ans, get_max(first, qq - 1).first + h[first]);
  else {
    ans = max(ans, get_max(first, s1 - 2).first + h[first]);
    int q = qq - s1;
    int vv = push_up(second, s2 - 1 - q - 1);
    ans = max(ans, get_max(vv, q).second - h[z] + s1 - 1);
  }
  return ans;
}
inline int solve(int first, int second) {
  int z = lca(first, second), ans = 0;
  if (DEBUG) {
    fill(use, use + n + 1, 0);
    int v;
    v = first;
    while (v != z) {
      use[v] = 1;
      v = ppiopipoiuyio[v];
    }
    v = second;
    while (v != z) {
      use[v] = 1;
      v = ppiopipoiuyio[v];
    }
    use[z] = 1;
  }
  int s1 = h[first] - h[z] + 1, s2 = h[second] - h[z] + 1;
  int s = s1 + s2 - 1;
  ans = max(ans, max_lca(first, second, z) + min(s1, s2) - 1);
  ans = max(ans, (s - 1) / 2);
  int q1 = s / 2, q2 = s - s / 2;
  ans = max(ans, get_ans(first, second, z, q1));
  ans = max(ans, get_ans(second, first, z, q2));
  return ans;
}
int main() {
  cin >> n;
  for (int i = 0; i < int(n - 1); i++) {
    int first, second;
    scanf("%d %d", &first, &second);
    g[first].push_back(second);
    g[second].push_back(first);
  }
  dfs0(1, 1);
  dfs1(1, 1, 0);
  dfs2(1, 1);
  int qq;
  cin >> qq;
  for (int u = 0; u < int(qq); u++) {
    int first, second;
    scanf("%d %d", &first, &second);
    printf("%d\n", solve(first, second));
  }
  return 0;
}

### 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;
const long long MOD = 1e9 + 7;
const int INF = 2e9;
const long long INF64 = 1e18;
const long double EPS = 1e-16;
const long double PI = acos(-1);
const long long MD = 1551513443;
const long long T = 25923;
const int N = 100015;
const int M = 25;
const int DEBUG = 0;
const int MAGIC = 320;
int n, h[N], maxi[N], dp_down[N], dp_up[N], up[20][N], max_up[20][N],
    max_down[20][N], tin[N], tout[N], timer, ppiopipoiuyio[N], dd[N];
vector<int> g[N];
vector<int> dp[N];
vector<pair<int, int> > b[N];
void dfs0(int v, int p) {
  dp[v].resize(int((g[v]).size()));
  for (int i = 0; i < int(int((g[v]).size())); i++) {
    int u = g[v][i];
    if (u == p) continue;
    h[u] = h[v] + 1;
    dfs0(u, v);
    dp[v][i] = maxi[u] + 1;
    maxi[v] = max(maxi[v], maxi[u] + 1);
    b[v].push_back(make_pair(maxi[u] + 1, u));
  }
  sort((b[v]).begin(), (b[v]).end());
  reverse((b[v]).begin(), (b[v]).end());
}
void dfs1(int v, int p, int d1) {
  dd[v] = d1;
  int k = int((g[v]).size());
  int s1[k], s2[k];
  for (int i = 0; i < int(k); i++) s1[i] = s2[i] = dp[v][i];
  for (int i = 1; i < k; i++) s1[i] = max(s1[i], s1[i - 1]);
  for (int i = k - 2; i >= 0; i--) s2[i] = max(s2[i], s2[i + 1]);
  for (int i = 0; i < int(k); i++) {
    int u = g[v][i];
    if (u == p) continue;
    int d_new = d1;
    if (i > 0) d_new = max(d_new, s1[i - 1]);
    if (i + 1 < k) d_new = max(d_new, s2[i + 1]);
    dfs1(u, v, d_new + 1);
  }
  for (int i = 0; i < int(k); i++) {
    int u = g[v][i];
    if (u == p) continue;
    int ans = 0;
    if (i) ans = s1[i - 1];
    if (i + 1 < k) ans = max(ans, s2[i + 1]);
    dp_down[u] = ans - h[v];
    dp_up[u] = ans + h[v];
  }
}
void dfs2(int v, int p) {
  ppiopipoiuyio[v] = p;
  tin[v] = timer++;
  up[0][v] = p;
  max_up[0][v] = dp_up[v];
  max_down[0][v] = dp_down[v];
  for (int i = 1; i < 20; i++) {
    up[i][v] = up[i - 1][up[i - 1][v]];
    max_up[i][v] = max(max_up[i - 1][v], max_up[i - 1][up[i - 1][v]]);
    max_down[i][v] = max(max_down[i - 1][v], max_down[i - 1][up[i - 1][v]]);
  }
  for (int u : g[v])
    if (u != p) dfs2(u, v);
  tout[v] = timer++;
}
inline bool check(int a, int b) {
  return (tin[a] <= tin[b] && tout[a] >= tin[b]);
}
inline int lca(int a, int b) {
  if (check(a, b)) return a;
  if (check(b, a)) return b;
  for (int i = 19; i >= 0; i--)
    if (!check(up[i][a], b)) a = up[i][a];
  return up[0][a];
}
inline int push_up(int v, int z) {
  for (int i = 19; i >= 0; i--)
    if ((1 << i) <= z) {
      v = up[i][v];
      z -= (1 << i);
    }
  return v;
}
inline pair<int, int> get_max(int s, int z) {
  pair<int, int> ans = make_pair(-INF, -INF);
  for (int i = 19; i >= 0; i--)
    if ((1 << i) <= z) {
      ans = make_pair(max(ans.first, max_down[i][s]),
                      max(ans.second, max_up[i][s]));
      s = up[i][s];
      z -= (1 << i);
    }
  return ans;
}
int mm = 0;
bool use[N];
void dfs_debug_lca(int v, int p, int h) {
  mm = max(mm, h);
  for (int u : g[v])
    if (u != p && !use[u]) dfs_debug_lca(u, v, h + 1);
}
inline int max_lca(int first, int second, int z) {
  int ans = dd[z];
  for (int i = 0; i < int(int((b[z]).size())); i++) {
    int u = b[z][i].second;
    if (u == ppiopipoiuyio[z]) continue;
    if (!check(u, first) && !check(u, second)) {
      ans = max(ans, maxi[u] + 1);
      break;
    }
  }
  if (DEBUG) {
    mm = 0;
    dfs_debug_lca(z, z, 0);
    assert(ans == mm);
  }
  return ans;
}
inline int get_ans(int first, int second, int z, int qq) {
  int ans = 0, s1 = h[first] - h[z] + 1, s2 = h[second] - h[z] + 1;
  if (first != z || first == second) ans = max(ans, maxi[first]);
  if (qq < s1)
    ans = max(ans, get_max(first, qq - 1).first + h[first]);
  else {
    ans = max(ans, get_max(first, s1 - 2).first + h[first]);
    int q = qq - s1;
    int vv = push_up(second, s2 - 1 - q - 1);
    ans = max(ans, get_max(vv, q).second - h[z] + s1 - 1);
  }
  return ans;
}
inline int solve(int first, int second) {
  int z = lca(first, second), ans = 0;
  if (DEBUG) {
    fill(use, use + n + 1, 0);
    int v;
    v = first;
    while (v != z) {
      use[v] = 1;
      v = ppiopipoiuyio[v];
    }
    v = second;
    while (v != z) {
      use[v] = 1;
      v = ppiopipoiuyio[v];
    }
    use[z] = 1;
  }
  int s1 = h[first] - h[z] + 1, s2 = h[second] - h[z] + 1;
  int s = s1 + s2 - 1;
  ans = max(ans, max_lca(first, second, z) + min(s1, s2) - 1);
  ans = max(ans, (s - 1) / 2);
  int q1 = s / 2, q2 = s - s / 2;
  ans = max(ans, get_ans(first, second, z, q1));
  ans = max(ans, get_ans(second, first, z, q2));
  return ans;
}
int main() {
  cin >> n;
  for (int i = 0; i < int(n - 1); i++) {
    int first, second;
    scanf("%d %d", &first, &second);
    g[first].push_back(second);
    g[second].push_back(first);
  }
  dfs0(1, 1);
  dfs1(1, 1, 0);
  dfs2(1, 1);
  int qq;
  cin >> qq;
  for (int u = 0; u < int(qq); u++) {
    int first, second;
    scanf("%d %d", &first, &second);
    printf("%d\n", solve(first, second));
  }
  return 0;
}

```