StrCartesian

면접 대비

시간 제한13초메모리 제한2048 MB

요약
두 문자열 집합의 모든 n*m개 연결 조합을 사전순으로 정렬한 뒤, k번째 원소의 인덱스 쌍을 답한다.
난이도

어려움10점 중 8점

유형
문자열, 정렬, 이분 탐색, 트라이
정답자
아직 제출이 없습니다

문제

Given are two sets of strings A=a_1,a_2,…,a_nA = \\{a\_1, a\_2, \ldots, a\_n\\} and B=b_1,b_2,…,b_mB = \\{b\_1, b\_2, \ldots, b\_m\\}. Define a sequence of n⋅mn \cdot m pairwise concatenations of a_ia\_i and b_jb\_j: S=(a_1b_1,a_1b_2,…,a_1b_m,a_2b_1,a_2b_2,…,a_2b_m,…,a_nb_1,a_nb_2,…,a_nb_m).S=(a\_1 b\_1, a\_1 b\_2, \ldots, a\_1 b\_m, a\_2 b\_1, a\_2 b\_2, \ldots, a\_2 b\_m, \ldots, a\_n b\_1, a\_n b\_2, \ldots, a\_n b\_m)\text{.}

Now sort the sequence SS lexicographically, and let the sorted sequence be C=(c_1,c_2,…,c_n⋅m)C = (c\_1, c\_2, \ldots, c\_{n \cdot m}).

We want to know the sequence CC, but it is too large. So we make qq queries to your program, and the ii-th query asks for c_k_ic\_{k\_i}.

However, c_k_ic\_{k\_i} is still too long to output. If the answer equals c=a_f+b_sc = a\_f + b\_s, then your program only needs to output the pair (f,s)(f, s).

입력

The first line contains two integers nn and mm (1≤n,m≤5⋅1041 \le n, m \le 5 \cdot 10^4), the sizes of sets AA and set BB.

The following nn lines contain nn distinct non-empty strings a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n.

The total length of strings in set AA does not exceed 10610^6.

The following mm lines contain mm distinct non-empty strings b_1,b_2,…,b_mb\_1, b\_2, \ldots, b\_m.

The total length of strings in set BB does not exceed 10610^6.

All strings consist of lowercase English letters.

The next line contains one integer qq (1≤q≤10001 \le q \le 1000), the number of queries.

In the following qq lines, the ii-th line contains an integer k_ik\_i (1≤k_i≤n⋅m1 \le k\_i \le n \cdot m), specifying that the query asks for the k_ik\_i-th element of CC.

출력

Print qq lines. The ii-th line must contain two integers f_if\_i and s_is\_i (1≤f_i≤n1 \le f\_i \le n; 1≤s_i≤m1 \le s\_i \le m) specifying that the answer c_k_ic\_{k\_i} equals to a_f_ib_s_ia\_{f\_i} b\_{s\_i}. If there are multiple correct answers, your program may output any one of them.

예제1

  1. 예제 1

    입력
    2 3
    a
    ab
    a
    aa
    ba
    2
    3
    4
    
    예상 출력
    2 1
    1 3