Permutation

Given a permutation P and queries K, find the exponent T such that P^T is the K-th smallest among P^1 through P^(M-1) in lexicographic order.

Hard8MathCombinatoricsSortingImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

A permutation PP of size NN is an array P=[P1,P2,,PN]P = [P_1, P_2, \dots, P_N] with 1PiN1 \le P_i \le N and PiPjP_i \ne P_j for iji \ne j.

For permutations AA and BB of size NN, AA is smaller than BB when there is an index ii (1iN1 \le i \le N) such that Ai<BiA_i < B_i and Aj=BjA_j = B_j for all 1j<i1 \le j < i.

For permutations AA and BB of size NN, the product A×BA \times B is the permutation of size NN whose ii-th entry is (A×B)i=ABi(A \times B)_i = A_{B_i}.

For a permutation PP and a positive integer zz, the power PzP^z is defined by P1=PP^1 = P and Pz=Pz1×PP^z = P^{z-1} \times P for z>1z > 1.

You are given a permutation PP of size NN. Let MM be the smallest integer greater than 11 with PM=PP^M = P. Let A1,A2,,AM1A_1, A_2, \dots, A_{M-1} be P1,P2,,PM1P^1, P^2, \dots, P^{M-1} sorted in increasing lexicographic order, so A1<A2<<AM1A_1 < A_2 < \dots < A_{M-1}.

For instance, if P=[2,3,1,5,4]P = [2, 3, 1, 5, 4] then P1=[2,3,1,5,4]P^1 = [2, 3, 1, 5, 4], P2=[3,1,2,4,5]P^2 = [3, 1, 2, 4, 5], P3=[1,2,3,5,4]P^3 = [1, 2, 3, 5, 4], P4=[2,3,1,4,5]P^4 = [2, 3, 1, 4, 5], P5=[3,1,2,5,4]P^5 = [3, 1, 2, 5, 4], P6=[1,2,3,4,5]P^6 = [1, 2, 3, 4, 5] and P7=[2,3,1,5,4]P^7 = [2, 3, 1, 5, 4]. Hence M=7M = 7 and the sorted array is A=[P6,P3,P4,P1,P2,P5]A = [P^6, P^3, P^4, P^1, P^2, P^5].

You are also given QQ queries. The ii-th query contains an integer KiK_i. The answer is the integer TiT_i (1Ti<M1 \le T_i < M) with PTi=AKiP^{T_i} = A_{K_i}. Answer all queries.

Input

The first line contains two integers NN and QQ (1N1001 \le N \le 100, 1Q3000001 \le Q \le 300000), the size of the permutation and the number of queries. The second line contains NN integers P1,P2,,PNP_1, P_2, \dots, P_N (1PiN1 \le P_i \le N), the permutation. The input is guaranteed to be a permutation. Each of the next QQ lines contains one integer KiK_i (1Ki<M1 \le K_i < M), the query. Here MM is the smallest integer greater than 11 with PM=PP^M = P as defined above, and it is not given explicitly in the input.

Output

Print QQ lines, each containing one integer TiT_i, the answer to the ii-th query.

Hint

The permutation used in the visible input is the same as the permutation described in the statement.