跳到主要内容
图灵 OJTURING / ONLINE JUDGE

#F1986. DZY Loves Strings

    ID: 1992 传统题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>二分哈希字符串双指针算法思想数据结构挑战难度共享题库Codeforces英文题面题目来源题面语言

DZY Loves Strings

题目描述

D. DZY Loves Strings
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ai, the second one has name bi. 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 ai and bi, or point out that such substring doesn't exist.

A substring of s is a string slsl+1... sr for some integers l,r (1≤lr≤|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 ai and bi (1≤|ai|,|bi|≤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.

Examples
Input
xudyhduxyz
3
xyz xyz
dyh xyz
dzy xyz
Output
3
8
-1
Input
abcabd
3
a c
ab abc
ab d
Output
2
3
3
Input
baabcabaaa
2
abca baa
aa aba
Output
6
4
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.


题目来源:fps-www.educg.net-codeforce-1-2833.xml.zip;FPS 共享题包,题包内第 1571 题。保留原作者与原赛事署名。