#include <bits/stdc++.h>
using namespace std;
long long n, m, par[((long long)20)][((long long)101 * 1000)],
    dis[((long long)101 * 1000)], strt[((long long)101 * 1000)],
    fnsh[((long long)101 * 1000)], _cnt, ans[((long long)101 * 1000)],
    r_strt[((long long)101 * 1000)];
vector<long long> e[((long long)101 * 1000)];
vector<pair<long long, long long> > qu[((long long)101 * 1000)];
long long seg[4 * ((long long)101 * 1000)], lazy[4 * ((long long)101 * 1000)];
void pre_dfs(long long x) {
  r_strt[_cnt] = x;
  strt[x] = _cnt++;
  for (int i = 1; i < ((long long)20); i++)
    par[i][x] = par[i - 1][par[i - 1][x]];
  for (auto u : e[x])
    if (u != par[0][x]) par[0][u] = x, dis[u] = dis[x] + 1, pre_dfs(u);
  fnsh[x] = _cnt;
}
long long iPar(long long x, long long num) {
  for (int i = 0; i < ((long long)20); i++)
    if ((num & (1 << i))) x = par[i][x];
  return x;
}
long long lca(long long x, long long y) {
  if (dis[x] < dis[y]) swap(x, y);
  x = iPar(x, dis[x] - dis[y]);
  if (x == y) return x;
  for (int i = ((long long)20) - 1; i >= 0; i--)
    if (par[i][x] != par[i][y]) x = par[i][x], y = par[i][y];
  return par[0][x];
}
void shift(long long id) {
  lazy[id * 2] += lazy[id];
  seg[id * 2] += lazy[id];
  lazy[id * 2 + 1] += lazy[id];
  seg[id * 2 + 1] += lazy[id];
  lazy[id] = 0;
}
void build(long long xl = 0, long long xr = n, long long id = 1) {
  if (xl == xr - 1) {
    seg[id] = dis[r_strt[xl]];
    return;
  }
  long long mid = (xl + xr) / 2;
  build(xl, mid, id * 2);
  build(mid, xr, id * 2 + 1);
  seg[id] = max(seg[id * 2], seg[id * 2 + 1]);
}
void update(long long ql, long long qr, long long val, long long xl = 0,
            long long xr = n, long long id = 1) {
  if (qr <= xl || xr <= ql) return;
  if (ql <= xl && xr <= qr) {
    seg[id] += val;
    lazy[id] += val;
    return;
  }
  long long mid = (xl + xr) / 2;
  shift(id);
  update(ql, qr, val, xl, mid, id * 2);
  update(ql, qr, val, mid, xr, id * 2 + 1);
  seg[id] = max(seg[id * 2], seg[id * 2 + 1]);
}
long long query(long long ql, long long qr, long long xl = 0, long long xr = n,
                long long id = 1) {
  if (qr <= xl || xr <= ql) return 0;
  if (ql <= xl && xr <= qr) return seg[id];
  long long mid = (xl + xr) / 2;
  shift(id);
  return max(query(ql, qr, xl, mid, id * 2),
             query(ql, qr, mid, xr, id * 2 + 1));
}
void dfs(long long x) {
  for (auto u : qu[x]) {
    if (u.second < 0)
      ans[-u.second] = max(ans[-u.second], max(query(0, strt[u.first]),
                                               query(fnsh[u.first], n)));
    else
      ans[u.second] = max(ans[u.second], query(strt[u.first], fnsh[u.first]));
  }
  for (auto u : e[x])
    if (u != par[0][x]) {
      update(0, n, 1);
      update(strt[u], fnsh[u], -2);
      dfs(u);
      update(0, n, -1);
      update(strt[u], fnsh[u], +2);
    }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    long long v, u;
    cin >> v >> u;
    e[v].push_back(u);
    e[u].push_back(v);
  }
  pre_dfs(1);
  cin >> m;
  for (int i = 1; i <= m; i++) {
    long long v, u;
    cin >> v >> u;
    long long p = lca(v, u);
    if (dis[v] < dis[u]) swap(v, u);
    long long len = dis[v] + dis[u] - 2 * dis[p];
    if (dis[v] == dis[u])
      qu[v].push_back(
          {iPar(u, (dis[u] - dis[p]) - (len / 2 - (dis[v] - dis[p])) - 1), -i});
    else
      qu[v].push_back({iPar(v, len / 2), i});
    qu[u].push_back(
        {iPar(v, (dis[v] - dis[p]) - (len / 2 - (dis[u] - dis[p])) - 1), -i});
  }
  build();
  dfs(1);
  for (int i = 1; i <= m; i++) cout << ans[i] << "\n";
  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;
long long n, m, par[((long long)20)][((long long)101 * 1000)],
    dis[((long long)101 * 1000)], strt[((long long)101 * 1000)],
    fnsh[((long long)101 * 1000)], _cnt, ans[((long long)101 * 1000)],
    r_strt[((long long)101 * 1000)];
vector<long long> e[((long long)101 * 1000)];
vector<pair<long long, long long> > qu[((long long)101 * 1000)];
long long seg[4 * ((long long)101 * 1000)], lazy[4 * ((long long)101 * 1000)];
void pre_dfs(long long x) {
  r_strt[_cnt] = x;
  strt[x] = _cnt++;
  for (int i = 1; i < ((long long)20); i++)
    par[i][x] = par[i - 1][par[i - 1][x]];
  for (auto u : e[x])
    if (u != par[0][x]) par[0][u] = x, dis[u] = dis[x] + 1, pre_dfs(u);
  fnsh[x] = _cnt;
}
long long iPar(long long x, long long num) {
  for (int i = 0; i < ((long long)20); i++)
    if ((num & (1 << i))) x = par[i][x];
  return x;
}
long long lca(long long x, long long y) {
  if (dis[x] < dis[y]) swap(x, y);
  x = iPar(x, dis[x] - dis[y]);
  if (x == y) return x;
  for (int i = ((long long)20) - 1; i >= 0; i--)
    if (par[i][x] != par[i][y]) x = par[i][x], y = par[i][y];
  return par[0][x];
}
void shift(long long id) {
  lazy[id * 2] += lazy[id];
  seg[id * 2] += lazy[id];
  lazy[id * 2 + 1] += lazy[id];
  seg[id * 2 + 1] += lazy[id];
  lazy[id] = 0;
}
void build(long long xl = 0, long long xr = n, long long id = 1) {
  if (xl == xr - 1) {
    seg[id] = dis[r_strt[xl]];
    return;
  }
  long long mid = (xl + xr) / 2;
  build(xl, mid, id * 2);
  build(mid, xr, id * 2 + 1);
  seg[id] = max(seg[id * 2], seg[id * 2 + 1]);
}
void update(long long ql, long long qr, long long val, long long xl = 0,
            long long xr = n, long long id = 1) {
  if (qr <= xl || xr <= ql) return;
  if (ql <= xl && xr <= qr) {
    seg[id] += val;
    lazy[id] += val;
    return;
  }
  long long mid = (xl + xr) / 2;
  shift(id);
  update(ql, qr, val, xl, mid, id * 2);
  update(ql, qr, val, mid, xr, id * 2 + 1);
  seg[id] = max(seg[id * 2], seg[id * 2 + 1]);
}
long long query(long long ql, long long qr, long long xl = 0, long long xr = n,
                long long id = 1) {
  if (qr <= xl || xr <= ql) return 0;
  if (ql <= xl && xr <= qr) return seg[id];
  long long mid = (xl + xr) / 2;
  shift(id);
  return max(query(ql, qr, xl, mid, id * 2),
             query(ql, qr, mid, xr, id * 2 + 1));
}
void dfs(long long x) {
  for (auto u : qu[x]) {
    if (u.second < 0)
      ans[-u.second] = max(ans[-u.second], max(query(0, strt[u.first]),
                                               query(fnsh[u.first], n)));
    else
      ans[u.second] = max(ans[u.second], query(strt[u.first], fnsh[u.first]));
  }
  for (auto u : e[x])
    if (u != par[0][x]) {
      update(0, n, 1);
      update(strt[u], fnsh[u], -2);
      dfs(u);
      update(0, n, -1);
      update(strt[u], fnsh[u], +2);
    }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    long long v, u;
    cin >> v >> u;
    e[v].push_back(u);
    e[u].push_back(v);
  }
  pre_dfs(1);
  cin >> m;
  for (int i = 1; i <= m; i++) {
    long long v, u;
    cin >> v >> u;
    long long p = lca(v, u);
    if (dis[v] < dis[u]) swap(v, u);
    long long len = dis[v] + dis[u] - 2 * dis[p];
    if (dis[v] == dis[u])
      qu[v].push_back(
          {iPar(u, (dis[u] - dis[p]) - (len / 2 - (dis[v] - dis[p])) - 1), -i});
    else
      qu[v].push_back({iPar(v, len / 2), i});
    qu[u].push_back(
        {iPar(v, (dis[v] - dis[p]) - (len / 2 - (dis[u] - dis[p])) - 1), -i});
  }
  build();
  dfs(1);
  for (int i = 1; i <= m; i++) cout << ans[i] << "\n";
  return 0;
}

```