Three Permutations
时间限制:C/C++/Rust/Pascal 2秒,其他语言4秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

You are given three permutations a,b,c of length n. Recall that a permutation is a sequence of n integers from 1 to n, in which each number occurs exactly once.
There are three integers x,y and z. Initially, all of them equal to 1. After each second, x,y,z become a_y,b_z,c_x respectively and simultaneously (note that the subscripts are y,z,x}).
You need to answer multiple independent queries. For each query, you are given three integers x',y' and z'. Tell Walk Alone the minimum time needed for tuple (x,y,z) to become (x',y',z') if possible.

输入描述:

The first contains an integer n\ (1\le n\le 10^5), indicating the length of the permutations.
Each of the next three lines contains n integers. The i-th number of the second line, the third line and the fourth line indicate a_i,b_i and c_i\ (1 \le a_i,b_i,c_i \le n), respectively.
The next line contains an integer Q\ (1\le Q\le 10^5), indicating the number of queries.
Each of the next Q lines contains three integers x',y' and z'\ (1\le x',y',z'\le n).

输出描述:

For each query, output the minimum time needed for (x,y,z) to become (x',y',z') in a line, or -1 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

输出

复制
0
1
2
3
4
示例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

输出

复制
-1
89