You will be given a list consisting of non-negative integers in a decimal number system. We all know that, in the decimal number system, there are 10 digits . Their order is defined as .
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 and an integer, . Here, denotes the modified order of digits as . The answer to the query is the number of integers that are less than the given integer in list (applying the modified digit order described by ). The order of digit will not be modified.
First line of the input contains a single integer . The next line will contain space-separated integers which are the elements of the list and .
After that, there will be an integer denoting the number of queries. Each of the queries will contain space-separated integers. The first integers will denote the list, . The integer will denote the value of . It is guaranteed that for each of the queries.
For each test case, print the number of integers that are less than the given integer in list applying modified digit priority. Print the answer in a new line.
3 10 20 30 1 0 9 8 7 6 5 4 3 2 1 10
It yields the relation between digits and as below:
So, in the modified order of digits, and are smaller than .