Limits 1s, 512 MB

You will be given a list LL consisting of NN non-negative integers in a decimal number system. We all know that, in the decimal number system, there are 10 digits (0,1,2,3,4,5,6,7,8,9)(0,1,2,3,4,5,6,7,8,9). Their order is defined as (0<1<2<3<4<5<6<7<8<9)(0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9).

In this problem, you will be given some queries. In each query, you will be given a modified order of the 10 digits as a list PP and an integer, XX. Here, PP denotes the modified order of digits as P[0]<P[1]<P[2]<P[3]<P[4]<P[5]<P[6]<P[7]<P[8]<P[9]P[0] < P[1] < P[2] < P[3] < P[4] < P[5] < P[6] < P[7] < P[8] < P[9]. The answer to the query is the number of integers that are less than the given integer XX in list LL (applying the modified digit order described by PP). The order of digit 00 will not be modified.

Input

First line of the input contains a single integer NN(1N105)(1 \leq N \leq 10^5). The next line will contain NN space-separated integers L0,L1,,LN1L_{0}, L_{1}, … , L_{N-1} which are the elements of the list LL and 0Li10180 \leq L_{i} \leq 10^{18}.

After that, there will be an integer Q(1Q105)Q(1 \leq Q \leq 10^5) denoting the number of queries. Each of the queries will contain 1111 space-separated integers. The first 1010 integers will denote the list, PP. The 11th11^\text{th}integer will denote the value of XX (0X1018)(0 \leq X \leq 10^{18}). It is guaranteed that P[0]=0P[0] = 0 for each of the queries.

Output

For each test case, print the number of integers that are less than the given integer XX in list LL applying modified digit priority. Print the answer in a new line.

Sample

InputOutput
3
10 20 30
1
0 9 8 7 6 5 4 3 2 1 10
2

Given,
List L=[10,20,30]L = [10,20,30]
Then, there were Q=1Q = 1 queries are asked.
We can see that the query gives us a list P=[0,9,8,7,6,5,4,3,2,1]P = [0,9,8,7,6,5,4,3,2,1] and X=10X = 10. From description of the problem, we know the relation given below.

P[0]<P[1]<P[2]<P[3]<P[4]<P[5]<P[6]<P[7]<P[8]<P[9]P[0] < P[1] < P[2] < P[3] < P[4] < P[5] < P[6] < P[7] < P[8] < P[9]

It yields the relation between digits 1,21, 2 and 33 as below:

3<2<13 < 2 < 1

So, in the modified order of digits, 3030 and 2020 are smaller than 1010.


Submit

Login to submit.

Statistics

78% Solution Ratio
tasmeemrezaEarliest, Jan '23
arafraihan7Fastest, 0.1s
Tanbin_HasanLightest, 46 MB
Faisal_NirjhorShortest, 968B
Toph uses cookies. By continuing you agree to our Cookie Policy.