Codeforces Round 254 (Div. 1)
D
DZY Loves Strings
Codeforces
2500
原题
← CE →
翻译中…

DZY loves strings, and he enjoys collecting them.

翻译中…

In China, many people like to use strings containing their names' initials, for example: xyz, jcvb, dzy, dyh.

翻译中…

Once DZY found a lucky string $*s*$. A lot of pairs of good friends came to DZY when they heard about the news. The first member of the $*i*$-th pair has name $*a*_{*i*}$, the second one has name $*b*_{*i*}$. Each pair wondered if there is a substring of the lucky string containing both of their names. If so, they want to find the one with minimum length, which can give them good luck and make their friendship last forever.

翻译中…

Please help DZY for each pair find the minimum length of the substring of $*s*$ that contains both $*a*_{*i*}$ and $*b*_{*i*}$, or point out that such substring doesn't exist.

翻译中…

A substring of $*s*$ is a string $*s*_{*l*}*s*_{*l* + 1}... *s*_{*r*}$ for some integers $*l*, *r*$ $(1 ≤ *l* ≤ *r* ≤ |*s*|)$. The length of such the substring is $(*r* - *l* + 1)$.

翻译中…

A string $*p*$ contains some another string $*q*$ if there is a substring of $*p*$ equal to $*q*$.

翻译中…

Input

翻译中…

The first line contains a string $*s*$ $(1 ≤ |*s*| ≤ 50000)$.

翻译中…

The second line contains a non-negative integer $*q*$ $(0 ≤ *q* ≤ 100000)$ — the number of pairs. Each of the next $*q*$ lines describes a pair, the line contains two space-separated strings $*a*_{*i*}$ and $*b*_{*i*}$ $(1 ≤ |*a*_{*i*}|, |*b*_{*i*}| ≤ 4)$.

翻译中…

It is guaranteed that all the strings only consist of lowercase English letters.

翻译中…

Output

翻译中…

For each pair, print a line containing a single integer — the minimum length of the required substring. If there is no such substring, output -1.

翻译中…

Note

翻译中…

The shortest substrings in the first sample are: xyz, dyhduxyz.

翻译中…

The shortest substrings in the second sample are: ca, abc and abd.

翻译中…

The shortest substrings in the third sample are: baabca and abaa.

翻译中…

样例

Input #1
xudyhduxyz
3
xyz xyz
dyh xyz
dzy xyz
Output #1
3
8
-1
InputCopy OutputCopy
Input #2
abcabd
3
a c
ab abc
ab d
Output #2
2
3
3
InputCopy OutputCopy
Input #3
baabcabaaa
2
abca baa
aa aba
Output #3
6
4
InputCopy OutputCopy
题目信息
题号
444D
时间限制
3 s
内存限制
256 MB
标签
二分
哈希
字符串
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。