This page is still under construction.

Parts of this page are still being built. What you see may change.

Hongjun Likes Strings

Time limit2sMemory limit512 MB

Summary
For each of up to 100000 queries, find the shortest substring of a fixed string S that contains both given short patterns A and B, allowing overlap.
Level

Hard8 of 10

Topics
String, String matching, Prefix sum, Binary search
Solved
No attempts yet

Problem

Hongjun likes strings, so he keeps making up problems about them.

Here is one of them. Given a string SS and two strings AA and BB, find the shortest contiguous substring of SS that contains both AA and BB as substrings. The occurrence of AA and the occurrence of BB are allowed to overlap.

Hongjun is smart, so he solved that at once. Then he thought of a harder version, in which the two strings AA and BB arrive as QQ separate questions. He could not find a fast way to answer them, so he decided that short AA and BB would let him solve it quickly.

Help Hongjun and write a program that answers every question.

Input

The first line contains a string SS of length at most 50 00050\,000.

The second line contains the number of questions QQ, an integer with 0≤Q≤100 0000 \le Q \le 100\,000.

Each of the next QQ lines contains two strings AA and BB separated by a space. The length of AA and the length of BB are between 11 and 44.

SS, AA, and BB consist of lowercase English letters only.

Output

Print the answers to the QQ questions, one per line. On each line print the minimum length of a contiguous substring of SS that contains both AA and BB as substrings. If no such contiguous substring exists, print −1-1.

Examples2

  1. Example 1

    Input
    xudyhduxyz
    3
    xyz xyz
    dyh xyz
    dzy xyz
    
    Expected output
    3
    8
    -1
    
  2. Example 2

    Input
    aaaa
    4
    a a
    aa aaa
    aaaa a
    b a
    
    Expected output
    1
    3
    4
    -1