#include <bits/stdc++.h>
int II() {
  int n;
  scanf("%d", &n);
  return n;
}
void II(int n) { printf("%d", n); }
void IIn(int n) { printf("%d\n", n); }
void IIb(int n) { printf("%d ", n); }
long long int LL() {
  long long int n;
  scanf("%lld", &n);
  return n;
}
void LL(long long int n) { printf("%lld", n); }
void LLn(long long int n) { printf("%lld\n", n); }
void LLb(long long int n) { printf("%lld ", n); }
char CC() { return getchar(); }
char CCa() {
  char c = getchar();
  while (c <= 32) c = getchar();
  return c;
}
void CC(char c) { putchar(c); }
void CCn(char c) {
  putchar(c);
  putchar(10);
}
void CCb(char c) {
  putchar(c);
  putchar(32);
}
void CCn() { putchar(10); }
void CCb() { putchar(32); }
void SS(char *s) { scanf("%s", s); }
void SSb(const char *s) { printf("%s ", s); }
void SSn(const char *s) { printf("%s\n", s); }
float FF() {
  float n;
  scanf("%f", &n);
  return n;
}
void FF(float n) { printf("%f", n); }
void FFn(float n) { printf("%f\n", n); }
void FFb(float n) { printf("%f ", n); }
void FF(double n, int prec) { std::cout << std::setprecision(prec) << n; }
void FFn(double n, int prec) {
  std::cout << std::setprecision(prec) << n;
  putchar(10);
}
void FFb(double n, int prec) {
  std::cout << std::setprecision(prec) << n;
  putchar(32);
}
using namespace std;
vector<int> leaders;
constexpr int MAXN = 100000;
vector<int> adj[MAXN];
int leaderid[MAXN];
int height[MAXN];
int myleader[MAXN];
int dis_from_leader[MAXN];
bool isleader[MAXN];
int leader_index[MAXN];
class segm_up {
 private:
 public:
  int l, r, val;
  segm_up *left, *right;
  segm_up() {}
  segm_up(int pl, int pr) {
    if (pl == pr) {
      l = r = pl;
      val = height[leaders[l]] + l;
    } else {
      l = pl;
      r = pr;
      int m = (l + r) / 2;
      left = new segm_up(l, m);
      right = new segm_up(m + 1, r);
      val = max(left->val, right->val);
    }
  }
  int iquery(int s, int e) {
    if (l > e || r < s) return 0;
    if (l >= s && r <= e) return val;
    return max(left->iquery(s, e), right->iquery(s, e));
  }
  int query(int s, int e) {
    if (s > e) return -1;
    return iquery(s, e) - s;
  }
};
class segm_down {
 private:
 public:
  int l, r, val;
  segm_down *left, *right;
  segm_down() {}
  segm_down(int pl, int pr) {
    if (pl == pr) {
      l = r = pl;
      val = height[leaders[l]] + leaders.size() - 1 - l;
    } else {
      l = pl;
      r = pr;
      int m = (l + r) / 2;
      left = new segm_down(l, m);
      right = new segm_down(m + 1, r);
      val = max(left->val, right->val);
    }
  }
  int iquery(int s, int e) {
    if (l > e || r < s) return 0;
    if (l >= s && r <= e) return val;
    return max(left->iquery(s, e), right->iquery(s, e));
  }
  int query(int s, int e) {
    if (s > e) return -1;
    return iquery(s, e) - leaders.size() + 1 + e;
  }
};
std::pair<int, int> dfsk(int d, int u, int f) {
  std::pair<int, int> ret(d, u);
  for (int &v : adj[u])
    if (v != f) ret = max(ret, dfsk(d + 1, v, u));
  return ret;
}
int dfsh(int u, int f) {
  for (int &v : adj[u])
    if (v != f) height[u] = max(height[u], dfsh(v, u));
  return ++height[u];
}
void dfsr(int u, int f, int d, int l) {
  myleader[u] = l;
  dis_from_leader[u] = d;
  height[u] = 0;
  for (int &v : adj[u])
    if (v != f) {
      dfsr(v, u, d + 1, l);
      height[u] = max(height[u], 1 + height[v]);
    }
}
void leaderchain(int u) {
  isleader[u] = true;
  leaders.push_back(u);
  myleader[u] = u;
  dis_from_leader[u] = 0;
  height[u] = 0;
  int bigchild = -1;
  for (int &v : adj[u])
    if (!isleader[v])
      if (bigchild < 0 || height[v] > height[bigchild]) bigchild = v;
  if (bigchild >= 0) leaderchain(bigchild);
  for (int &v : adj[u])
    if (!isleader[v]) {
      dfsr(v, u, 1, u);
      height[u] = max(height[u], 1 + height[v]);
    }
}
int main() {
  int N = II();
  for (int M = N - 1; M--;) {
    int u = II() - 1, v = II() - 1;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  int k = dfsk(0, 0, 0).second;
  dfsh(k, k);
  leaderchain(k);
  for (int x = 0; x < leaders.size(); x++) leaderid[leaders[x]] = x;
  segm_up goup(0, leaders.size() - 1);
  segm_down godown(0, leaders.size() - 1);
  int Q = II();
  while (Q--) {
    int a = II() - 1, b = II() - 1;
    if (myleader[a] == myleader[b]) {
      int l = leaderid[myleader[a]];
      IIn(max(l, (int)leaders.size() - 1 - l) +
          min(dis_from_leader[a], dis_from_leader[b]));
      continue;
    };
    int la = myleader[a], lb = myleader[b], pla = leaderid[la],
        plb = leaderid[lb];
    if (plb < pla) {
      swap(a, b);
      swap(la, lb);
      swap(pla, plb);
    }
    int d = dis_from_leader[a] + dis_from_leader[b] + plb - pla;
    d /= 2;
    int ans = 0;
    ans = max(ans, min(pla + dis_from_leader[a], plb + dis_from_leader[b]));
    ans = max(ans, min((int)leaders.size() - 1 - pla + dis_from_leader[a],
                       (int)leaders.size() - 1 - plb + dis_from_leader[b]));
    ans = max(ans, goup.query(pla + 1, pla + d - dis_from_leader[a]) +
                       dis_from_leader[a] + 1);
    ans = max(ans, godown.query(plb - d + dis_from_leader[b], plb - 1) +
                       dis_from_leader[b] + 1);
    IIn(ans);
  }
}

### 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>
int II() {
  int n;
  scanf("%d", &n);
  return n;
}
void II(int n) { printf("%d", n); }
void IIn(int n) { printf("%d\n", n); }
void IIb(int n) { printf("%d ", n); }
long long int LL() {
  long long int n;
  scanf("%lld", &n);
  return n;
}
void LL(long long int n) { printf("%lld", n); }
void LLn(long long int n) { printf("%lld\n", n); }
void LLb(long long int n) { printf("%lld ", n); }
char CC() { return getchar(); }
char CCa() {
  char c = getchar();
  while (c <= 32) c = getchar();
  return c;
}
void CC(char c) { putchar(c); }
void CCn(char c) {
  putchar(c);
  putchar(10);
}
void CCb(char c) {
  putchar(c);
  putchar(32);
}
void CCn() { putchar(10); }
void CCb() { putchar(32); }
void SS(char *s) { scanf("%s", s); }
void SSb(const char *s) { printf("%s ", s); }
void SSn(const char *s) { printf("%s\n", s); }
float FF() {
  float n;
  scanf("%f", &n);
  return n;
}
void FF(float n) { printf("%f", n); }
void FFn(float n) { printf("%f\n", n); }
void FFb(float n) { printf("%f ", n); }
void FF(double n, int prec) { std::cout << std::setprecision(prec) << n; }
void FFn(double n, int prec) {
  std::cout << std::setprecision(prec) << n;
  putchar(10);
}
void FFb(double n, int prec) {
  std::cout << std::setprecision(prec) << n;
  putchar(32);
}
using namespace std;
vector<int> leaders;
constexpr int MAXN = 100000;
vector<int> adj[MAXN];
int leaderid[MAXN];
int height[MAXN];
int myleader[MAXN];
int dis_from_leader[MAXN];
bool isleader[MAXN];
int leader_index[MAXN];
class segm_up {
 private:
 public:
  int l, r, val;
  segm_up *left, *right;
  segm_up() {}
  segm_up(int pl, int pr) {
    if (pl == pr) {
      l = r = pl;
      val = height[leaders[l]] + l;
    } else {
      l = pl;
      r = pr;
      int m = (l + r) / 2;
      left = new segm_up(l, m);
      right = new segm_up(m + 1, r);
      val = max(left->val, right->val);
    }
  }
  int iquery(int s, int e) {
    if (l > e || r < s) return 0;
    if (l >= s && r <= e) return val;
    return max(left->iquery(s, e), right->iquery(s, e));
  }
  int query(int s, int e) {
    if (s > e) return -1;
    return iquery(s, e) - s;
  }
};
class segm_down {
 private:
 public:
  int l, r, val;
  segm_down *left, *right;
  segm_down() {}
  segm_down(int pl, int pr) {
    if (pl == pr) {
      l = r = pl;
      val = height[leaders[l]] + leaders.size() - 1 - l;
    } else {
      l = pl;
      r = pr;
      int m = (l + r) / 2;
      left = new segm_down(l, m);
      right = new segm_down(m + 1, r);
      val = max(left->val, right->val);
    }
  }
  int iquery(int s, int e) {
    if (l > e || r < s) return 0;
    if (l >= s && r <= e) return val;
    return max(left->iquery(s, e), right->iquery(s, e));
  }
  int query(int s, int e) {
    if (s > e) return -1;
    return iquery(s, e) - leaders.size() + 1 + e;
  }
};
std::pair<int, int> dfsk(int d, int u, int f) {
  std::pair<int, int> ret(d, u);
  for (int &v : adj[u])
    if (v != f) ret = max(ret, dfsk(d + 1, v, u));
  return ret;
}
int dfsh(int u, int f) {
  for (int &v : adj[u])
    if (v != f) height[u] = max(height[u], dfsh(v, u));
  return ++height[u];
}
void dfsr(int u, int f, int d, int l) {
  myleader[u] = l;
  dis_from_leader[u] = d;
  height[u] = 0;
  for (int &v : adj[u])
    if (v != f) {
      dfsr(v, u, d + 1, l);
      height[u] = max(height[u], 1 + height[v]);
    }
}
void leaderchain(int u) {
  isleader[u] = true;
  leaders.push_back(u);
  myleader[u] = u;
  dis_from_leader[u] = 0;
  height[u] = 0;
  int bigchild = -1;
  for (int &v : adj[u])
    if (!isleader[v])
      if (bigchild < 0 || height[v] > height[bigchild]) bigchild = v;
  if (bigchild >= 0) leaderchain(bigchild);
  for (int &v : adj[u])
    if (!isleader[v]) {
      dfsr(v, u, 1, u);
      height[u] = max(height[u], 1 + height[v]);
    }
}
int main() {
  int N = II();
  for (int M = N - 1; M--;) {
    int u = II() - 1, v = II() - 1;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  int k = dfsk(0, 0, 0).second;
  dfsh(k, k);
  leaderchain(k);
  for (int x = 0; x < leaders.size(); x++) leaderid[leaders[x]] = x;
  segm_up goup(0, leaders.size() - 1);
  segm_down godown(0, leaders.size() - 1);
  int Q = II();
  while (Q--) {
    int a = II() - 1, b = II() - 1;
    if (myleader[a] == myleader[b]) {
      int l = leaderid[myleader[a]];
      IIn(max(l, (int)leaders.size() - 1 - l) +
          min(dis_from_leader[a], dis_from_leader[b]));
      continue;
    };
    int la = myleader[a], lb = myleader[b], pla = leaderid[la],
        plb = leaderid[lb];
    if (plb < pla) {
      swap(a, b);
      swap(la, lb);
      swap(pla, plb);
    }
    int d = dis_from_leader[a] + dis_from_leader[b] + plb - pla;
    d /= 2;
    int ans = 0;
    ans = max(ans, min(pla + dis_from_leader[a], plb + dis_from_leader[b]));
    ans = max(ans, min((int)leaders.size() - 1 - pla + dis_from_leader[a],
                       (int)leaders.size() - 1 - plb + dis_from_leader[b]));
    ans = max(ans, goup.query(pla + 1, pla + d - dis_from_leader[a]) +
                       dis_from_leader[a] + 1);
    ans = max(ans, godown.query(plb - d + dis_from_leader[b], plb - 1) +
                       dis_from_leader[b] + 1);
    IIn(ans);
  }
}

```