#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
int n, m, a[1000005];
struct edge {
  int to, nxt;
} e[1000005 << 1];
int tot, head[1000005];
inline void adde(int u, int v) {
  e[++tot] = (edge){v, head[u]};
  head[u] = tot;
}
int s, t;
int fa[1000005], d[1000005], dis[1000005], cnt;
int dep[1000005], mx[1000005], top[1000005];
bool vis[1000005];
void dfs(int u, int pa) {
  fa[u] = pa;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == pa) continue;
    dis[v] = dis[u] + 1;
    dfs(v, u);
  }
}
void dfs2(int u, int p, int pa) {
  mx[p] = max(mx[p], dep[u]);
  top[u] = p;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == pa || vis[v]) continue;
    dep[v] = dep[u] + 1;
    dfs2(v, p, u);
  }
}
void find() {
  s = t = 1, dis[1] = 0;
  dfs(1, 0);
  for (register int i = (1); i <= (n); ++i)
    if (dis[i] > dis[t]) t = i;
  dis[t] = fa[t] = 0;
  dfs(t, 0);
  for (register int i = (1); i <= (n); ++i)
    if (dis[i] > dis[s]) s = i;
  int u = s;
  while (fa[u]) d[++cnt] = u, vis[u] = 1, u = fa[u];
  d[++cnt] = t;
  vis[t] = 1;
}
inline int p2(int x) { return 1 << x; };
int lg[1000005];
struct RMQ {
  int n, f[1000005][20];
  RMQ() { memset(f, -63, sizeof f); }
  void build() {
    for (register int j = (1); j <= (lg[n]); ++j)
      for (int i = 1; i + p2(j) <= n + 1; i++)
        f[i][j] = max(f[i][j - 1], f[i + p2(j - 1)][j - 1]);
  }
  inline int ask(int l, int r) {
    if (l > r) return -0x3f3f3f3f;
    int len = r - l + 1, k = lg[len];
    return max(f[l][k], f[r - p2(k) + 1][k]);
  }
} f1, f2;
int solve() {
  int u = read(), v = read();
  if (top[u] > top[v]) swap(u, v);
  if (top[u] == top[v])
    return min(dep[u], dep[v]) + max(cnt - top[u], top[u] - 1);
  int len = top[u] + top[v] - dep[u] + dep[v];
  if (top[u] * 2 >= len) return dep[v] + max(top[v] - 1, cnt - top[v]);
  if (top[v] * 2 <= len) return dep[u] + max(top[u] - 1, cnt - top[u]);
  int resx = max(f1.ask(1, len / 2) - top[u], top[u] - 1) + dep[u],
      resy = max(f2.ask(len / 2 + 1, cnt) + top[v], cnt - top[v]) + dep[v];
  return max(resx, resy);
}
signed main() {
  n = read();
  for (register int i = (2); i <= (n); ++i) {
    int u = read(), v = read();
    adde(u, v);
    adde(v, u);
  }
  find();
  for (register int i = (1); i <= (cnt); ++i) dfs2(d[i], i, 0);
  for (register int i = (2); i <= (cnt); ++i) lg[i] = lg[i >> 1] + 1;
  f1.n = f2.n = cnt;
  for (register int i = (1); i <= (cnt); ++i)
    f1.f[i][0] = mx[i] + i, f2.f[i][0] = mx[i] - i;
  f1.build();
  f2.build();
  m = read();
  while (m--) printf("%d\n", solve());
  return 0;
}

### Prompt

Your challenge is to write 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>
#pragma GCC optimize(3)
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0;
  bool f = 0;
  for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45);
  for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
  if (f) x = -x;
  return x;
}
int n, m, a[1000005];
struct edge {
  int to, nxt;
} e[1000005 << 1];
int tot, head[1000005];
inline void adde(int u, int v) {
  e[++tot] = (edge){v, head[u]};
  head[u] = tot;
}
int s, t;
int fa[1000005], d[1000005], dis[1000005], cnt;
int dep[1000005], mx[1000005], top[1000005];
bool vis[1000005];
void dfs(int u, int pa) {
  fa[u] = pa;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == pa) continue;
    dis[v] = dis[u] + 1;
    dfs(v, u);
  }
}
void dfs2(int u, int p, int pa) {
  mx[p] = max(mx[p], dep[u]);
  top[u] = p;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == pa || vis[v]) continue;
    dep[v] = dep[u] + 1;
    dfs2(v, p, u);
  }
}
void find() {
  s = t = 1, dis[1] = 0;
  dfs(1, 0);
  for (register int i = (1); i <= (n); ++i)
    if (dis[i] > dis[t]) t = i;
  dis[t] = fa[t] = 0;
  dfs(t, 0);
  for (register int i = (1); i <= (n); ++i)
    if (dis[i] > dis[s]) s = i;
  int u = s;
  while (fa[u]) d[++cnt] = u, vis[u] = 1, u = fa[u];
  d[++cnt] = t;
  vis[t] = 1;
}
inline int p2(int x) { return 1 << x; };
int lg[1000005];
struct RMQ {
  int n, f[1000005][20];
  RMQ() { memset(f, -63, sizeof f); }
  void build() {
    for (register int j = (1); j <= (lg[n]); ++j)
      for (int i = 1; i + p2(j) <= n + 1; i++)
        f[i][j] = max(f[i][j - 1], f[i + p2(j - 1)][j - 1]);
  }
  inline int ask(int l, int r) {
    if (l > r) return -0x3f3f3f3f;
    int len = r - l + 1, k = lg[len];
    return max(f[l][k], f[r - p2(k) + 1][k]);
  }
} f1, f2;
int solve() {
  int u = read(), v = read();
  if (top[u] > top[v]) swap(u, v);
  if (top[u] == top[v])
    return min(dep[u], dep[v]) + max(cnt - top[u], top[u] - 1);
  int len = top[u] + top[v] - dep[u] + dep[v];
  if (top[u] * 2 >= len) return dep[v] + max(top[v] - 1, cnt - top[v]);
  if (top[v] * 2 <= len) return dep[u] + max(top[u] - 1, cnt - top[u]);
  int resx = max(f1.ask(1, len / 2) - top[u], top[u] - 1) + dep[u],
      resy = max(f2.ask(len / 2 + 1, cnt) + top[v], cnt - top[v]) + dep[v];
  return max(resx, resy);
}
signed main() {
  n = read();
  for (register int i = (2); i <= (n); ++i) {
    int u = read(), v = read();
    adde(u, v);
    adde(v, u);
  }
  find();
  for (register int i = (1); i <= (cnt); ++i) dfs2(d[i], i, 0);
  for (register int i = (2); i <= (cnt); ++i) lg[i] = lg[i >> 1] + 1;
  f1.n = f2.n = cnt;
  for (register int i = (1); i <= (cnt); ++i)
    f1.f[i][0] = mx[i] + i, f2.f[i][0] = mx[i] - i;
  f1.build();
  f2.build();
  m = read();
  while (m--) printf("%d\n", solve());
  return 0;
}

```