Finally a Problem Without a Legend
Given three arrays of length .
We will construct an undirected weighted graph consisting of vertices such that an edge between two distinct vertices and exists if and only if is not a supermask of , and the weight of this edge will be .
You need to answer queries, each defined by two numbers and . For each query, you need to output the length of the shortest path from vertex to vertex , or if such a path does not exist. The length of a path is the sum of the weights of its edges, and the shortest path is the one with the minimal length.
Here, means bitwise OR operation, and means bitwise AND operation.
We consider an integer to be a supermask of an integer if and only if all bits which are turned on in are also turned on in , more formally if .
Input
The first line contains one integer — the size of the graph.
The second line contains integers .
The third line contains integers .
The fourth line contains integers .
The fifth line contains one integer .
The next lines contain a pair of distinct integers — vertices between which you need to find the shortest path.
Output
For each query, you need to output the length of the shortest path, or if such a path does not exist.
Examples
Note
The Graph for the first sample.
In the first query it is asked about the shortest path between vertex and , the optimal way is — — , the length is .
Scoring
( points): , ;
( points): , ;
( points): ;
( points): , , ;
( points): ;
( points): , ;
( points): no additional constraints.