#include <bits/stdc++.h>
using namespace std;
template <typename T, bool maximum_mode = false>
struct RMQ {
  int n = 0, levels = 0;
  vector<T> values;
  vector<vector<int>> range_low;
  RMQ(const vector<T> &_values = {}) {
    if (!_values.empty()) build(_values);
  }
  static int largest_bit(int x) { return 31 - __builtin_clz(x); }
  int better_index(int a, int b) const {
    return (values[a] < values[b]) ^ maximum_mode ? a : b;
  }
  void build(const vector<T> &_values) {
    values = _values;
    n = values.size();
    levels = largest_bit(n) + 1;
    range_low.resize(levels);
    for (int k = 0; k < levels; k++) range_low[k].resize(n - (1 << k) + 1);
    for (int i = 0; i < n; i++) range_low[0][i] = i;
    for (int k = 1; k < levels; k++)
      for (int i = 0; i <= n - (1 << k); i++)
        range_low[k][i] = better_index(range_low[k - 1][i],
                                       range_low[k - 1][i + (1 << (k - 1))]);
  }
  int query_index(int a, int b) const {
    assert(0 <= a && a < b && b <= n);
    int level = largest_bit(b - a);
    return better_index(range_low[level][a],
                        range_low[level][b - (1 << level)]);
  }
  T query_value(int a, int b) const { return values[query_index(a, b)]; }
};
struct LCA {
  int n = 0;
  vector<vector<int>> adj;
  vector<int> parent, depth;
  vector<int> euler, first_occurrence;
  vector<int> tour_start, tour_end, tour_list;
  RMQ<int> rmq;
  LCA(int _n = 0) { init(_n); }
  LCA(const vector<vector<int>> &_adj) { init(_adj); }
  void init(int _n) {
    n = _n;
    adj.assign(n, {});
    parent.resize(n);
    depth.resize(n);
    first_occurrence.resize(n);
    tour_start.resize(n);
    tour_end.resize(n);
    tour_list.resize(n);
  }
  void init(const vector<vector<int>> &_adj) {
    init(_adj.size());
    adj = _adj;
  }
  void add_edge(int a, int b) {
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  int tour;
  void dfs(int node, int par) {
    parent[node] = par;
    depth[node] = par < 0 ? 0 : depth[par] + 1;
    first_occurrence[node] = euler.size();
    euler.push_back(node);
    tour_list[tour] = node;
    tour_start[node] = tour++;
    if (par >= 0)
      adj[node].erase(find(adj[node].begin(), adj[node].end(), par));
    for (int child : adj[node]) {
      dfs(child, node);
      euler.push_back(node);
    }
    tour_end[node] = tour;
  }
  void build() {
    tour = 0;
    parent.assign(n, -1);
    for (int i = 0; i < n; i++)
      if (parent[i] < 0) {
        dfs(i, -1);
        euler.push_back(-1);
      }
    assert((int)euler.size() == 2 * n);
    vector<int> euler_depths;
    for (int node : euler)
      euler_depths.push_back(node < 0 ? node : depth[node]);
    rmq.build(euler_depths);
  }
  int get_lca(int a, int b) const {
    a = first_occurrence[a];
    b = first_occurrence[b];
    if (a > b) swap(a, b);
    return euler[rmq.query_index(a, b + 1)];
  }
  bool is_ancestor(int a, int b) const {
    return tour_start[a] <= tour_start[b] && tour_start[b] < tour_end[a];
  }
  bool on_path(int x, int a, int b) const {
    int anc = get_lca(a, b);
    return is_ancestor(anc, x) && (is_ancestor(x, a) || is_ancestor(x, b));
  }
  int get_dist(int a, int b) const {
    return depth[a] + depth[b] - 2 * depth[get_lca(a, b)];
  }
  int child_ancestor(int a, int b) const {
    assert(a != b);
    assert(is_ancestor(a, b));
    assert(!adj[a].empty());
    int low = 0, high = (int)adj[a].size() - 1;
    while (low < high) {
      int mid = (low + high) / 2;
      if (tour_start[b] < tour_end[adj[a][mid]])
        high = mid;
      else
        low = mid + 1;
    }
    int child = adj[a][low];
    assert(is_ancestor(child, b));
    return child;
  }
};
const int INF = 1e9 + 5;
struct path_query {
  int y, index;
};
int N, M, Q;
LCA lca;
vector<vector<int>> bus_lca;
vector<vector<int>> bus_endpoint;
vector<int> single_bus;
vector<vector<int>> bus_jump;
vector<vector<path_query>> queries_at;
vector<int> answers;
vector<set<int>> tour_sets;
int largest_bit(int x) { return 31 - __builtin_clz(x); }
int higher(int a, int b) { return lca.depth[a] < lca.depth[b] ? a : b; }
void bus_dfs(int node) {
  single_bus[node] = node;
  for (int anc : bus_lca[node])
    single_bus[node] = higher(single_bus[node], anc);
  for (int child : lca.adj[node]) {
    bus_dfs(child);
    single_bus[node] = higher(single_bus[node], single_bus[child]);
  }
}
void build_bus_jump() {
  bus_jump.assign(largest_bit(N) + 1, vector<int>(N));
  bus_jump[0] = single_bus;
  for (int k = 0; k < largest_bit(N); k++)
    for (int i = 0; i < N; i++)
      bus_jump[k + 1][i] = bus_jump[k][bus_jump[k][i]];
}
int min_buses_to_ancestor(int node, int anc) {
  if (node == anc) return 0;
  assert(lca.is_ancestor(anc, node));
  assert(lca.depth[node] > lca.depth[anc]);
  int buses = 0;
  for (int i = largest_bit(N); i >= 0; i--) {
    int candidate = bus_jump[i][node];
    if (lca.depth[candidate] > lca.depth[anc]) {
      node = candidate;
      buses += 1 << i;
    }
  }
  assert(lca.depth[node] > lca.depth[anc]);
  node = single_bus[node];
  buses++;
  return lca.depth[node] <= lca.depth[anc] ? buses : INF;
}
int get_kth_bus_ancestor(int node, int k) {
  for (int i = 0; 1 << i <= k; i++)
    if (k >> i & 1) node = bus_jump[i][node];
  return node;
}
void merge_into(set<int> &to, set<int> &from) {
  if (from.size() > to.size()) swap(from, to);
  for (auto &x : from) to.insert(x);
}
bool contains_any_in_range(set<int> &s, int start, int end) {
  return s.lower_bound(start) != s.lower_bound(end);
}
void path_dfs(int node) {
  for (int end : bus_endpoint[node])
    tour_sets[node].insert(lca.tour_start[end]);
  for (int child : lca.adj[node]) {
    path_dfs(child);
    merge_into(tour_sets[node], tour_sets[child]);
  }
  for (path_query &pq : queries_at[node])
    if (contains_any_in_range(tour_sets[node], lca.tour_start[pq.y],
                              lca.tour_end[pq.y]))
      answers[pq.index]--;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N;
  lca.init(N);
  for (int i = 1; i < N; i++) {
    int p;
    cin >> p;
    p--;
    lca.add_edge(p, i);
  }
  lca.build();
  bus_lca.assign(N, {});
  bus_endpoint.assign(N, {});
  cin >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    int anc = lca.get_lca(a, b);
    bus_lca[a].push_back(anc);
    bus_lca[b].push_back(anc);
    bus_endpoint[a].push_back(b);
    bus_endpoint[b].push_back(a);
  }
  single_bus.resize(N);
  bus_dfs(0);
  build_bus_jump();
  cin >> Q;
  queries_at.assign(N, {});
  answers.resize(Q);
  for (int i = 0; i < Q; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    int anc = lca.get_lca(u, v);
    int u_buses = min_buses_to_ancestor(u, anc);
    int v_buses = min_buses_to_ancestor(v, anc);
    if (u_buses >= INF || v_buses >= INF) {
      answers[i] = -1;
    } else {
      answers[i] = u_buses + v_buses;
      if (u_buses > 0 && v_buses > 0) {
        int x = get_kth_bus_ancestor(u, u_buses - 1);
        int y = get_kth_bus_ancestor(v, v_buses - 1);
        queries_at[x].push_back({y, i});
      }
    }
  }
  tour_sets.assign(N, {});
  path_dfs(0);
  for (int answer : answers) cout << answer << '\n';
}

### 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, bool maximum_mode = false>
struct RMQ {
  int n = 0, levels = 0;
  vector<T> values;
  vector<vector<int>> range_low;
  RMQ(const vector<T> &_values = {}) {
    if (!_values.empty()) build(_values);
  }
  static int largest_bit(int x) { return 31 - __builtin_clz(x); }
  int better_index(int a, int b) const {
    return (values[a] < values[b]) ^ maximum_mode ? a : b;
  }
  void build(const vector<T> &_values) {
    values = _values;
    n = values.size();
    levels = largest_bit(n) + 1;
    range_low.resize(levels);
    for (int k = 0; k < levels; k++) range_low[k].resize(n - (1 << k) + 1);
    for (int i = 0; i < n; i++) range_low[0][i] = i;
    for (int k = 1; k < levels; k++)
      for (int i = 0; i <= n - (1 << k); i++)
        range_low[k][i] = better_index(range_low[k - 1][i],
                                       range_low[k - 1][i + (1 << (k - 1))]);
  }
  int query_index(int a, int b) const {
    assert(0 <= a && a < b && b <= n);
    int level = largest_bit(b - a);
    return better_index(range_low[level][a],
                        range_low[level][b - (1 << level)]);
  }
  T query_value(int a, int b) const { return values[query_index(a, b)]; }
};
struct LCA {
  int n = 0;
  vector<vector<int>> adj;
  vector<int> parent, depth;
  vector<int> euler, first_occurrence;
  vector<int> tour_start, tour_end, tour_list;
  RMQ<int> rmq;
  LCA(int _n = 0) { init(_n); }
  LCA(const vector<vector<int>> &_adj) { init(_adj); }
  void init(int _n) {
    n = _n;
    adj.assign(n, {});
    parent.resize(n);
    depth.resize(n);
    first_occurrence.resize(n);
    tour_start.resize(n);
    tour_end.resize(n);
    tour_list.resize(n);
  }
  void init(const vector<vector<int>> &_adj) {
    init(_adj.size());
    adj = _adj;
  }
  void add_edge(int a, int b) {
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  int tour;
  void dfs(int node, int par) {
    parent[node] = par;
    depth[node] = par < 0 ? 0 : depth[par] + 1;
    first_occurrence[node] = euler.size();
    euler.push_back(node);
    tour_list[tour] = node;
    tour_start[node] = tour++;
    if (par >= 0)
      adj[node].erase(find(adj[node].begin(), adj[node].end(), par));
    for (int child : adj[node]) {
      dfs(child, node);
      euler.push_back(node);
    }
    tour_end[node] = tour;
  }
  void build() {
    tour = 0;
    parent.assign(n, -1);
    for (int i = 0; i < n; i++)
      if (parent[i] < 0) {
        dfs(i, -1);
        euler.push_back(-1);
      }
    assert((int)euler.size() == 2 * n);
    vector<int> euler_depths;
    for (int node : euler)
      euler_depths.push_back(node < 0 ? node : depth[node]);
    rmq.build(euler_depths);
  }
  int get_lca(int a, int b) const {
    a = first_occurrence[a];
    b = first_occurrence[b];
    if (a > b) swap(a, b);
    return euler[rmq.query_index(a, b + 1)];
  }
  bool is_ancestor(int a, int b) const {
    return tour_start[a] <= tour_start[b] && tour_start[b] < tour_end[a];
  }
  bool on_path(int x, int a, int b) const {
    int anc = get_lca(a, b);
    return is_ancestor(anc, x) && (is_ancestor(x, a) || is_ancestor(x, b));
  }
  int get_dist(int a, int b) const {
    return depth[a] + depth[b] - 2 * depth[get_lca(a, b)];
  }
  int child_ancestor(int a, int b) const {
    assert(a != b);
    assert(is_ancestor(a, b));
    assert(!adj[a].empty());
    int low = 0, high = (int)adj[a].size() - 1;
    while (low < high) {
      int mid = (low + high) / 2;
      if (tour_start[b] < tour_end[adj[a][mid]])
        high = mid;
      else
        low = mid + 1;
    }
    int child = adj[a][low];
    assert(is_ancestor(child, b));
    return child;
  }
};
const int INF = 1e9 + 5;
struct path_query {
  int y, index;
};
int N, M, Q;
LCA lca;
vector<vector<int>> bus_lca;
vector<vector<int>> bus_endpoint;
vector<int> single_bus;
vector<vector<int>> bus_jump;
vector<vector<path_query>> queries_at;
vector<int> answers;
vector<set<int>> tour_sets;
int largest_bit(int x) { return 31 - __builtin_clz(x); }
int higher(int a, int b) { return lca.depth[a] < lca.depth[b] ? a : b; }
void bus_dfs(int node) {
  single_bus[node] = node;
  for (int anc : bus_lca[node])
    single_bus[node] = higher(single_bus[node], anc);
  for (int child : lca.adj[node]) {
    bus_dfs(child);
    single_bus[node] = higher(single_bus[node], single_bus[child]);
  }
}
void build_bus_jump() {
  bus_jump.assign(largest_bit(N) + 1, vector<int>(N));
  bus_jump[0] = single_bus;
  for (int k = 0; k < largest_bit(N); k++)
    for (int i = 0; i < N; i++)
      bus_jump[k + 1][i] = bus_jump[k][bus_jump[k][i]];
}
int min_buses_to_ancestor(int node, int anc) {
  if (node == anc) return 0;
  assert(lca.is_ancestor(anc, node));
  assert(lca.depth[node] > lca.depth[anc]);
  int buses = 0;
  for (int i = largest_bit(N); i >= 0; i--) {
    int candidate = bus_jump[i][node];
    if (lca.depth[candidate] > lca.depth[anc]) {
      node = candidate;
      buses += 1 << i;
    }
  }
  assert(lca.depth[node] > lca.depth[anc]);
  node = single_bus[node];
  buses++;
  return lca.depth[node] <= lca.depth[anc] ? buses : INF;
}
int get_kth_bus_ancestor(int node, int k) {
  for (int i = 0; 1 << i <= k; i++)
    if (k >> i & 1) node = bus_jump[i][node];
  return node;
}
void merge_into(set<int> &to, set<int> &from) {
  if (from.size() > to.size()) swap(from, to);
  for (auto &x : from) to.insert(x);
}
bool contains_any_in_range(set<int> &s, int start, int end) {
  return s.lower_bound(start) != s.lower_bound(end);
}
void path_dfs(int node) {
  for (int end : bus_endpoint[node])
    tour_sets[node].insert(lca.tour_start[end]);
  for (int child : lca.adj[node]) {
    path_dfs(child);
    merge_into(tour_sets[node], tour_sets[child]);
  }
  for (path_query &pq : queries_at[node])
    if (contains_any_in_range(tour_sets[node], lca.tour_start[pq.y],
                              lca.tour_end[pq.y]))
      answers[pq.index]--;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> N;
  lca.init(N);
  for (int i = 1; i < N; i++) {
    int p;
    cin >> p;
    p--;
    lca.add_edge(p, i);
  }
  lca.build();
  bus_lca.assign(N, {});
  bus_endpoint.assign(N, {});
  cin >> M;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    int anc = lca.get_lca(a, b);
    bus_lca[a].push_back(anc);
    bus_lca[b].push_back(anc);
    bus_endpoint[a].push_back(b);
    bus_endpoint[b].push_back(a);
  }
  single_bus.resize(N);
  bus_dfs(0);
  build_bus_jump();
  cin >> Q;
  queries_at.assign(N, {});
  answers.resize(Q);
  for (int i = 0; i < Q; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    int anc = lca.get_lca(u, v);
    int u_buses = min_buses_to_ancestor(u, anc);
    int v_buses = min_buses_to_ancestor(v, anc);
    if (u_buses >= INF || v_buses >= INF) {
      answers[i] = -1;
    } else {
      answers[i] = u_buses + v_buses;
      if (u_buses > 0 && v_buses > 0) {
        int x = get_kth_bus_ancestor(u, u_buses - 1);
        int y = get_kth_bus_ancestor(v, v_buses - 1);
        queries_at[x].push_back({y, i});
      }
    }
  }
  tour_sets.assign(N, {});
  path_dfs(0);
  for (int answer : answers) cout << answer << '\n';
}

```