Codeforces Round 483 (Div. 1) [Thanks, Botan Investments and Victor Shaburov!]
E
NN country
Codeforces
2800
原题
← D→
翻译中…

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 \le n \le 2 \cdot 10^5$) — the number of cities.

翻译中…

The second line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i \lt i$), where $p_i$ means that cities $p_i$ and $i$ are connected by road.

翻译中…

The third line contains a single integer $m$ ($1 \le m \le 2 \cdot 10^5$) — the number of bus routes.

翻译中…

Each of the next $m$ lines contains $2$ integers $a$ and $b$ ($1 \le a, b \le n$, $a \neq 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 \le q \le 2 \cdot 10^5$) — the number of questions you are interested in.

翻译中…

Each of the next $q$ lines contains $2$ integers $v$ and $u$ ($1 \le v, u \le n$, $v \neq 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.

翻译中…

Note

翻译中…

image Routes for first sample are marked on the picture.

翻译中…

样例

Input #1
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
1
3
-1
1
2
3
InputCopy OutputCopy
Input #2
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 #2
1
-1
-1
1
-1
1
InputCopy OutputCopy
题目信息
题号
983E
时间限制
3 s
内存限制
256 MB
标签
二分
数据结构
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。