#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
template <typename T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
  gn(first);
  gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
  gn(first);
  gn(second);
  gn(z);
}
template <typename T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
template <typename T>
inline void println(T first) {
  print(first), putchar('\n');
}
template <typename T>
inline void printsp(T first) {
  print(first), putchar(' ');
}
template <class T, class T1>
inline void print(T first, T1 second) {
  printsp(first), println(second);
}
template <class T, class T1, class T2>
inline void print(T first, T1 second, T2 z) {
  printsp(first), printsp(second), println(z);
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
int head[200200], nxt[200200];
void add_edge(int u, int v) {
  nxt[v] = head[u];
  head[u] = v;
}
int p[200200][20], deep[200200];
struct LCA {
  void dfs(int u) {
    deep[u] = deep[p[u][0]] + 1;
    for (int i = 1; i < 18; i++) p[u][i] = p[p[u][i - 1]][i - 1];
    for (int v = head[u]; v; v = nxt[v]) {
      dfs(v);
    }
  }
  int k_th(int u, int k) {
    for (int i = 0; i < 18; i++) {
      if (k & (1 << i)) u = p[u][i];
    }
    return u;
  }
  int lca(int a, int b) {
    if (deep[a] > deep[b]) swap(a, b);
    b = k_th(b, deep[b] - deep[a]);
    if (a == b) return a;
    for (int i = 18; i--;) {
      if (p[a][0] == p[b][0]) break;
      if (p[a][i] != p[b][i]) a = p[a][i], b = p[b][i];
    }
    return p[a][0];
  }
} lca;
vector<int> adj[200200];
int sz[200200], son[200200];
int L[200200], R[200200], rec[200200], rid;
int T[200200][20];
void dfs(int u) {
  rec[++rid] = u;
  L[u] = rid;
  sz[u] = 1 + adj[u].size();
  for (int v = head[u]; v; v = nxt[v]) {
    dfs(v);
    sz[u] += sz[v];
    if (sz[son[u]] < sz[v]) son[u] = v;
    if (deep[T[u][0]] > deep[T[v][0]]) T[u][0] = T[v][0];
  }
  R[u] = rid;
}
void redfs_for_T(int u) {
  for (int i = 1; i < 18; i++) T[u][i] = T[T[u][i - 1]][i - 1];
  for (int v = head[u]; v; v = nxt[v]) redfs_for_T(v);
}
int calc(int u, int w, int &pos, int &step) {
  if (deep[T[u][17]] > deep[w]) {
    step = 0x3f3f3f3f;
    return 0x3f3f3f3f;
  }
  if (u == w) {
    pos = u;
    step = 0;
    return 0;
  }
  int ans = 0;
  for (int i = 18; i--;) {
    if (deep[T[u][i]] > deep[w]) u = T[u][i], ans += (1 << i);
  }
  pos = u;
  step = ans;
  return ans + 1;
}
int ans[200200];
vector<pair<pair<int, int>, int> > query[200200];
int B[200200];
void update(int u, int v) {
  while (u <= rid) {
    B[u] += v;
    u += u & -u;
  }
}
int calc(int u) {
  int ans = 0;
  while (u) {
    ans += B[u];
    u -= u & -u;
  }
  return ans;
}
void redfs(int u) {
  for (int v = head[u]; v; v = nxt[v]) {
    if (v == son[u]) continue;
    redfs(v);
    for (int i = L[v]; i <= R[v]; i++) {
      int w = rec[i];
      for (int j = 0; j < adj[w].size(); j++) {
        update(L[adj[w][j]], -1);
      }
    }
  }
  if (son[u]) redfs(son[u]);
  for (int v = head[u]; v; v = nxt[v]) {
    if (v == son[u]) continue;
    for (int i = L[v]; i <= R[v]; i++) {
      int w = rec[i];
      for (int j = 0; j < adj[w].size(); j++) {
        update(L[adj[w][j]], 1);
      }
    }
  }
  for (int j = 0; j < adj[u].size(); j++) update(L[adj[u][j]], 1);
  for (int j = 0; j < query[u].size(); j++) {
    int tt = query[u][j].first.first;
    if (calc(R[tt]) - calc(L[tt] - 1)) {
      smin(ans[query[u][j].second], query[u][j].first.second + 1);
    }
  }
}
int main() {
  int n, m, q, u, v;
  gn(n);
  for (int i = 2; i <= n; i++) {
    gn(p[i][0]);
    add_edge(p[i][0], i);
  }
  lca.dfs(1);
  gn(m);
  for (int i = 1; i <= n; i++) T[i][0] = i;
  for (int i = 0; i < m; i++) {
    gn(u, v);
    adj[u].push_back(v);
    adj[v].push_back(u);
    int w = lca.lca(u, v);
    if (deep[T[u][0]] > deep[w]) T[u][0] = w;
    if (deep[T[v][0]] > deep[w]) T[v][0] = w;
  }
  dfs(1);
  redfs_for_T(1);
  gn(q);
  for (int i = 0; i < q; i++) {
    gn(u, v);
    ans[i] = 0x3f3f3f3f;
    int w = lca.lca(u, v);
    int pos1, pos2, step1, step2, mn1, mn2;
    mn1 = calc(u, w, pos1, step1);
    mn2 = calc(v, w, pos2, step2);
    smin(ans[i], mn1 + mn2);
    if (step1 + step2 < 0x3f3f3f3f) {
      query[pos1].push_back(
          pair<pair<int, int>, int>(pair<int, int>(pos2, step1 + step2), i));
      query[pos2].push_back(
          pair<pair<int, int>, int>(pair<int, int>(pos1, step1 + step2), i));
    }
  }
  redfs(1);
  for (int i = 0; i < q; i++) {
    if (ans[i] < 0x3f3f3f3f)
      println(ans[i]);
    else
      puts("-1");
  }
  return 0;
}

### Prompt

Generate a cpp solution to the following problem:

In the NN country, there are n cities, numbered from 1 to n, and n - 1 roads, connecting them. There is a roads path between any two cities.

There are m bidirectional bus routes between cities. Buses drive between two cities taking the shortest path with stops in every city they drive through. Travelling by bus, you can travel from any stop on the route to any other. You can travel between cities only by bus.

You are interested in q questions: is it possible to get from one city to another and what is the minimum number of buses you need to use for it?

Input

The first line contains a single integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of cities.

The second line contains n - 1 integers p_2, p_3, …, p_n (1 ≤ p_i < i), where p_i means that cities p_i and i are connected by road.

The third line contains a single integer m (1 ≤ m ≤ 2 ⋅ 10^5) — the number of bus routes.

Each of the next m lines contains 2 integers a and b (1 ≤ a, b ≤ n, a ≠ b), meaning that there is a bus route between cities a and b. It is possible that there is more than one route between two cities.

The next line contains a single integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of questions you are interested in.

Each of the next q lines contains 2 integers v and u (1 ≤ v, u ≤ n, v ≠ u), meaning that you are interested if it is possible to get from city v to city u and what is the minimum number of buses you need to use for it.

Output

Print the answer for each question on a separate line. If there is no way to get from one city to another, print -1. Otherwise print the minimum number of buses you have to use.

Examples

Input

7
1 1 1 4 5 6
4
4 2
5 4
1 3
6 7
6
4 5
3 5
7 2
4 5
3 2
5 3


Output

1
3
-1
1
2
3


Input

7
1 1 2 3 4 1
4
4 7
3 5
7 6
7 6
6
4 6
3 1
3 2
2 7
6 3
5 3


Output

1
-1
-1
1
-1
1

Note

<image> Routes for first sample are marked on the picture. 

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
template <typename T>
inline void gn(T &first) {
  char c, sg = 0;
  while (c = getchar(), (c > '9' || c < '0') && c != '-')
    ;
  for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
       c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
  if (sg) first = -first;
}
template <class T, class T1>
inline void gn(T &first, T1 &second) {
  gn(first);
  gn(second);
}
template <class T, class T1, class T2>
inline void gn(T &first, T1 &second, T2 &z) {
  gn(first);
  gn(second);
  gn(z);
}
template <typename T>
inline void print(T first) {
  if (first < 0) {
    putchar('-');
    return print(-first);
  }
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
template <typename T>
inline void println(T first) {
  print(first), putchar('\n');
}
template <typename T>
inline void printsp(T first) {
  print(first), putchar(' ');
}
template <class T, class T1>
inline void print(T first, T1 second) {
  printsp(first), println(second);
}
template <class T, class T1, class T2>
inline void print(T first, T1 second, T2 z) {
  printsp(first), printsp(second), println(z);
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
int head[200200], nxt[200200];
void add_edge(int u, int v) {
  nxt[v] = head[u];
  head[u] = v;
}
int p[200200][20], deep[200200];
struct LCA {
  void dfs(int u) {
    deep[u] = deep[p[u][0]] + 1;
    for (int i = 1; i < 18; i++) p[u][i] = p[p[u][i - 1]][i - 1];
    for (int v = head[u]; v; v = nxt[v]) {
      dfs(v);
    }
  }
  int k_th(int u, int k) {
    for (int i = 0; i < 18; i++) {
      if (k & (1 << i)) u = p[u][i];
    }
    return u;
  }
  int lca(int a, int b) {
    if (deep[a] > deep[b]) swap(a, b);
    b = k_th(b, deep[b] - deep[a]);
    if (a == b) return a;
    for (int i = 18; i--;) {
      if (p[a][0] == p[b][0]) break;
      if (p[a][i] != p[b][i]) a = p[a][i], b = p[b][i];
    }
    return p[a][0];
  }
} lca;
vector<int> adj[200200];
int sz[200200], son[200200];
int L[200200], R[200200], rec[200200], rid;
int T[200200][20];
void dfs(int u) {
  rec[++rid] = u;
  L[u] = rid;
  sz[u] = 1 + adj[u].size();
  for (int v = head[u]; v; v = nxt[v]) {
    dfs(v);
    sz[u] += sz[v];
    if (sz[son[u]] < sz[v]) son[u] = v;
    if (deep[T[u][0]] > deep[T[v][0]]) T[u][0] = T[v][0];
  }
  R[u] = rid;
}
void redfs_for_T(int u) {
  for (int i = 1; i < 18; i++) T[u][i] = T[T[u][i - 1]][i - 1];
  for (int v = head[u]; v; v = nxt[v]) redfs_for_T(v);
}
int calc(int u, int w, int &pos, int &step) {
  if (deep[T[u][17]] > deep[w]) {
    step = 0x3f3f3f3f;
    return 0x3f3f3f3f;
  }
  if (u == w) {
    pos = u;
    step = 0;
    return 0;
  }
  int ans = 0;
  for (int i = 18; i--;) {
    if (deep[T[u][i]] > deep[w]) u = T[u][i], ans += (1 << i);
  }
  pos = u;
  step = ans;
  return ans + 1;
}
int ans[200200];
vector<pair<pair<int, int>, int> > query[200200];
int B[200200];
void update(int u, int v) {
  while (u <= rid) {
    B[u] += v;
    u += u & -u;
  }
}
int calc(int u) {
  int ans = 0;
  while (u) {
    ans += B[u];
    u -= u & -u;
  }
  return ans;
}
void redfs(int u) {
  for (int v = head[u]; v; v = nxt[v]) {
    if (v == son[u]) continue;
    redfs(v);
    for (int i = L[v]; i <= R[v]; i++) {
      int w = rec[i];
      for (int j = 0; j < adj[w].size(); j++) {
        update(L[adj[w][j]], -1);
      }
    }
  }
  if (son[u]) redfs(son[u]);
  for (int v = head[u]; v; v = nxt[v]) {
    if (v == son[u]) continue;
    for (int i = L[v]; i <= R[v]; i++) {
      int w = rec[i];
      for (int j = 0; j < adj[w].size(); j++) {
        update(L[adj[w][j]], 1);
      }
    }
  }
  for (int j = 0; j < adj[u].size(); j++) update(L[adj[u][j]], 1);
  for (int j = 0; j < query[u].size(); j++) {
    int tt = query[u][j].first.first;
    if (calc(R[tt]) - calc(L[tt] - 1)) {
      smin(ans[query[u][j].second], query[u][j].first.second + 1);
    }
  }
}
int main() {
  int n, m, q, u, v;
  gn(n);
  for (int i = 2; i <= n; i++) {
    gn(p[i][0]);
    add_edge(p[i][0], i);
  }
  lca.dfs(1);
  gn(m);
  for (int i = 1; i <= n; i++) T[i][0] = i;
  for (int i = 0; i < m; i++) {
    gn(u, v);
    adj[u].push_back(v);
    adj[v].push_back(u);
    int w = lca.lca(u, v);
    if (deep[T[u][0]] > deep[w]) T[u][0] = w;
    if (deep[T[v][0]] > deep[w]) T[v][0] = w;
  }
  dfs(1);
  redfs_for_T(1);
  gn(q);
  for (int i = 0; i < q; i++) {
    gn(u, v);
    ans[i] = 0x3f3f3f3f;
    int w = lca.lca(u, v);
    int pos1, pos2, step1, step2, mn1, mn2;
    mn1 = calc(u, w, pos1, step1);
    mn2 = calc(v, w, pos2, step2);
    smin(ans[i], mn1 + mn2);
    if (step1 + step2 < 0x3f3f3f3f) {
      query[pos1].push_back(
          pair<pair<int, int>, int>(pair<int, int>(pos2, step1 + step2), i));
      query[pos2].push_back(
          pair<pair<int, int>, int>(pair<int, int>(pos1, step1 + step2), i));
    }
  }
  redfs(1);
  for (int i = 0; i < q; i++) {
    if (ans[i] < 0x3f3f3f3f)
      println(ans[i]);
    else
      puts("-1");
  }
  return 0;
}

```