时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld
题目描述
You are given three permutations

of length

. Recall that a permutation is a sequence of

integers from

to

, in which each number occurs exactly once.
There are three integers

and

. Initially, all of them equal to

. After each second,

become

respectively and simultaneously (
note that the subscripts are 
}).
You need to answer multiple independent queries. For each query, you are given three integers

and

. Tell Walk Alone the minimum time needed for tuple
)
to become
)
if possible.
输入描述:
The first contains an integer
, indicating the length of the permutations.
Each of the next three lines contains
integers. The
-th number of the second line, the third line and the fourth line indicate
and
, respectively.
The next line contains an integer
, indicating the number of queries.
Each of the next
lines contains three integers
and
.
输出描述:
For each query, output the minimum time needed for
to become
in a line, or
if impossible.
示例1
输入
复制
5
2 1 5 4 3
3 1 4 2 5
5 4 2 1 3
5
1 1 1
2 3 5
5 5 4
3 2 3
1 4 2
示例2
输入
复制
10
10 9 3 7 6 2 8 5 1 4
8 9 10 6 2 4 1 7 3 5
10 1 3 5 9 6 2 7 4 8
2
9 7 2
2 9 7