Problem10319--CF EDU - Suffix Array - Step 5 - C. Sorting Substrings

10319: CF EDU - Suffix Array - Step 5 - C. Sorting Substrings

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 512 MiB

Description

Given the string $s$ and a set of its substrings. Substrings are defined by pairs of numbers $l_i,r_i$, the position of the left end and the right end of the substring. You need to sort substrings in lexicographic order. If substrings are equal as words, then use the pairs themselves as an additional sorting key (i.e., first compare by $l$, then by $r$).

Input

The first line contains a non-empty line $s$. Its length does not exceed $4⋅10^5$, it can contain any characters with codes from 33 to 127.

The second line contains $n\ (1≤n≤4⋅10^5)$, the number of substrings in the set. 

The next $n$ lines contain a pairs of integers $l_i,r_i\ (1≤l_i≤r_i≤|s|)$, where $|s|$ is the length of the string $s$. It is possible that a set of substrings contains the same pairs.

Output

Print $n$ lines, each line contains a description of the substring in the same format as in the input. The order of the substrings is specified in the problem statement.

Sample 1 Input

abacaba
6
4 7
1 2
1 1
3 4
5 7
1 3

Sample 1 Output

1 1
1 2
1 3
5 7
3 4
4 7

Sample 2 Input

a
1
1 1

Sample 2 Output

1 1

Sample 3 Input

aa
5
1 1
1 2
2 2
1 1
2 2

Sample 3 Output

1 1
1 1
2 2
2 2
1 2

HINT

Source/Category