gadfadar4

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

Task

We are given three natural numbers NN, DD, and KK, followed by NN natural numbers of exactly KK digits each. Each of the NN numbers is divided into XX groups of DD digits each (the last group may not always be exactly DD digits).

On the next line, the number QQ is given, and on each of the following QQ lines, there are XX digits separated by spaces. Let these be c1,c2,cXc_1, c_2, \cdots c_X. We define the power of a number in the sequence as the number of groups ii (1iX1 \leq i \leq X) for which cic_i appears at least once in the ii-th group of that number. For each of the QQ lines, output the position of the first number in the sequence that has the maximum power, as well as the maximum power. If the maximum power is 00, print 1-1.

Input data

The first line contains three natural numbers NN, DD, and KK. The next NN lines contain numbers of KK digits, one per line. The next line contains the number QQ. The next QQ lines each contain XX digits separated by spaces.

Output data

The QQ lines will contain at most two numbers, representing the position of the first element with the maximum power and the maximum power, separated by a space. If the maximum power is 00, print only 1-1.

Constraints and clarifications

  • 2N10002 \leq N \leq 1000;
  • 2K20002 \leq K \leq 2000;
  • 1Q10001 \leq Q \leq 1000;
  • 1DK1 \leq D \leq K
  • Numbers can also start with the digit 00.
  • The sequence of NN numbers is indexed from 11.
# Points Constraints
11 1212 2N,K202 \leq N, K \leq 20, 1Q101 \leq Q \leq 10
22 66 2N,K2002 \leq N, K \leq 200, 1Q501 \leq Q \leq 50
33 1717 2N,K6002 \leq N, K \leq 600, 1Q3001 \leq Q \leq 300
44 1818 2N,K4002 \leq N, K \leq 400, 1Q10001 \leq Q \leq 1000
55 88 No additional constraints
66 4040 No additional constraints

Example 1

stdin

3 3 10
1234581659
2870454321
9561840782
3
4 3 9 5
5 7 3 2
2 4 9 2

stdout

-1
3 2
1 2

Explanation

The nn numbers divided into groups are:

123  458  165  9123 \ \ 458 \ \ 165 \ \ 9

287  045  432  1287 \ \ 045 \ \ 432 \ \ 1

956  184  078  2956 \ \ 184 \ \ 078 \ \ 2

For the first query, no number contains the digit 44 in the first group, the digit 33 in the second group, the digit 99 in the third group, or the digit 55 in the last group, so 1-1 is displayed.

For the second query, only the third number contains the digit 55 in the first group, no number contains the digit 77 in the second group, only the second number contains the digit 33 in the third group, and only the third number contains the digit 22 in the last group, so 3 23 \ 2 is displayed, as the maximum is 22 and it occurs at the third number.

For the third query, only the first number contains the digit 22 in the first group, all three numbers contain the digit 44 in the second group, no number contains the digit 99 in the third group, and only the last number contains the digit 22 in the last group, so 1 21 \ 2 is displayed, as the maximum 22 appears at the first and third numbers, but the first number is displayed first.

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