CF BUDDY
← Problems·

1714D · Color with Occurrences

1600 · brute force, data structures, dp

Problem: You are given some text tt and a set of nn strings s1,s2,,sns_1, s_2, \dots, s_n.

In one step, you can choose any occurrence of any string sis_i in the text tt and color the corresponding characters of the text in red. For example, if t=bababat=\texttt{bababa} and s1=bas_1=\texttt{ba}, s2=abas_2=\texttt{aba}, you can get t=bababat=\color{red}{\texttt{ba}}\texttt{baba}, t=bababat=\texttt{b}\color{red}{\texttt{aba}}\texttt{ba} or t=bababat=\texttt{bab}\color{red}{\texttt{aba}} in one step.

You want to color all the letters of the text tt in red. When you color a letter in red again, it stays red.

In the example above, three steps are enough:

  • Let's color t[24]=s2=abat[2 \dots 4]=s_2=\texttt{aba} in red, we get t=bababat=\texttt{b}\color{red}{\texttt{aba}}\texttt{ba};
  • Let's color t[12]=s1=bat[1 \dots 2]=s_1=\texttt{ba} in red, we get t=bababat=\color{red}{\texttt{baba}}\texttt{ba};
  • Let's color t[46]=s2=abat[4 \dots 6]=s_2=\texttt{aba} in red, we get t=bababat=\color{red}{\texttt{bababa}}.

Each string sis_i can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.

Determine the minimum number of steps needed to color all letters tt in red and how to do it. If it is impossible to color all letters of the text tt in red, output -1.

Input Format: The first line of the input contains an integer qq (1q1001 \le q \le 100) —the number of test cases in the test.

The descriptions of the test cases follow.

The first line of each test case contains the text tt (1t1001 \le |t| \le 100), consisting only of lowercase Latin letters, where t|t| is the length of the text tt.

The second line of each test case contains a single integer nn (1n101 \le n \le 10) — the number of strings in the set.

This is followed by nn lines, each containing a string sis_i (1si101 \le |s_i| \le 10) consisting only of lowercase Latin letters, where si|s_i| — the length of string sis_i.

Output Format: For each test case, print the answer on a separate line.

If it is impossible to color all the letters of the text in red, print a single line containing the number -1.

Otherwise, on the first line, print the number mm — the minimum number of steps it will take to turn all the letters tt red.

Then in the next mm lines print pairs of indices: wjw_j and pjp_j (1jm1 \le j \le m), which denote that the string with index wjw_j was used as a substring to cover the occurrences starting in the text tt from position pjp_j. The pairs can be output in any order.

If there are several answers, output any of them.

Note: The first test case is explained in the problem statement.

In the second test case, it is impossible to color all the letters of the text in red.

Sample Cases

Case 1

Input

6
bababa
2
ba
aba
caba
2
bac
acab
abacabaca
3
aba
bac
aca
baca
3
a
c
b
codeforces
4
def
code
efo
forces
aaaabbbbcccceeee
4
eeee
cccc
aaaa
bbbb

Output

3
2 2
1 1
2 4
-1
4
1 1
2 6
3 3
3 7
4
3 1
1 2
2 3
1 4
2
4 5
2 1
4
3 1
4 5
2 9
1 13

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback