Azugand

Time limit: 2s Memory limit: 256MB Input: Output:

Azugand, the city from the famous Prahovand Valley, is known for its peculiar structure: it has NN intersections, numbered from 11 to NN, all of which have an assigned value ViV_i. Two distinct intersections have a street between them if and only if the bitwise AND (represented as &\&) of their values is non-zero.

Formally, you are given a graph with NN vertices, each vertex having a value ViV_i assigned to it. We have an edge between two distinct vertices ii and jj iff Vi & Vj0V_i\ \&\ V_j \neq 0.

Task

Andrei, a well-known child from the valley, wants to make a table with the shortest distances between two points of interest, but he is overwhelmed by the number of queries he wants to solve, so he asks for your help! You are given QQ queries, in which you have to compute cost(X,Y)\text{cost}(X,Y). We define cost(X,Y)\text{cost}(X,Y) as the minimum number of edges that are in the shortest path in the graph between vertices XX and YY. If we can’t reach vertex YY from vertex XX, then the value of cost(X,Y)\text{cost}(X,Y) is 1-1.

Input data

The first line of input contains two integers NN and QQ, the number of vertices in the graph, and the number of queries, respectively.

The next line contains NN integers V1,V2,,VNV_1, V_2, \dots, V_N, the values assigned to the vertices of the graph.

Each of the next QQ lines of the input contains two distinct integers XiX_i and YiY_i, the vertices for which you must compute cost(Xi,Yi)\text{cost}(X_i,Y_i).

Output data

Print QQ integers, each one on a separate line: the value of cost(Xi,Yi)\text{cost}(X_i, Y_i) for each query.

Constraints and clarifications

  • 1N200 0001 \leq N \leq 200 \ 000
  • 1Q200 0001 \leq Q \leq 200 \ 000
  • 0Vi<2200 \leq V_i < 2^{20} for each 1iN1 \leq i \leq N
  • 1XiYiN1 \leq X_i \neq Y_i \leq N for each 1iQ1 \leq i \leq Q
# Points Constraints
1 0 Examples
2 7 N500N \leq 500, Q500Q \leq 500
3 23 Q1Q \leq 1
4 21 Vi<25V_i < 25 for each 1iN1 \leq i \leq N
5 49 No additional constraints

Example 1

stdin

4 4
9 3 16 6
1 2
2 4
4 1
2 3

stdout

1
1
2
-1

Explanation

In the first query, V1=9V_1 = 9 and V2=3V_2 = 3, 9 & 3=19\ \&\ 3 = 1, so there is an edge between vertices 11 and 22, so the minimum distance is 11.

In the second query, V2=3V_2 = 3 and V4=6V_4 = 6, 3 & 6=23\ \&\ 6 = 2, so there is an edge between vertices 22 and 44, so the minimum distance is 11.

In the third query, V4=6V_4 = 6 and V1=9V_1 = 9, 6 & 9=06\ \&\ 9 = 0, so there is no edge between vertices 44 and 11, so the minimum distance is at least 22. For the path 4,2,14, 2, 1 the values are 6,3,96, 3, 9 respectively which means that there is an edge between vertices 44 and 22 also between 22 and 11, so there is a path of length 22 between vertices 44 and 11.

In the fourth query, V1 & V3=0V_1\ \&\ V_3 = 0, V2 & V3=0V_2\ \&\ V_3 = 0 and V4 & V3=0V_4\ \&\ V_3 = 0, which means that there are no edges connected to vertex 33, so there is no path between vertices 22 and 33.

Example 2

stdin

7 5
3072 5120 67584 73728 49152 24576 40960
2 5
7 3
1 6
5 6
7 2

stdout

5
2
3
1
4

Log in or sign up to be able to send submissions!