Given the string S and m queries. The i-th query is given by the two strings xi and yi.

For each query, answer the longest substring of the string S, starting with xi and ending with yi.

For the string S, | S | represents the length of S. Also, the fact that the character string T is a substring of the character string S means that a certain integer i exists and Tj = Si + j is satisfied for 1 ≤ j ≤ | T |. Where Tj represents the jth character of T.

Constraints

* 1 ≤ | S | ≤ 2 x 105
* 1 ≤ m ≤ 105
* 1 ≤ | xi |, | yi |
* $ \ sum ^ m_ {i = 1} $ (| xi | + | yi |) ≤ 2 x 105
* S and xi, yi consist only of half-width lowercase letters.

Input

The input is given in the following format.


S
m
x1 y1
x2 y2
::
xm ym


* The character string S is given on the first line.
* The number of queries m is given in the second line.
* Of the m lines from the 3rd line, the i-th query string xi, yi is given on the i-th line, separated by spaces.

Output

Answer the maximum substring length in the following format.


len1
len2
::
lenm


Output the longest substring length leni that satisfies the condition for the i-th query on the i-th line of the m lines from the first line. If there is no such substring, output 0.

Examples

Input

abracadabra
5
ab a
a a
b c
ac ca
z z


Output

11
11
4
3
0


Input

howistheprogress
4
ist prog
s ss
how is
the progress


Output

9
12
5
11


Input

icpcsummertraining
9
mm m
icpc summer
train ing
summer mm
i c
i i
g g
train i
summer er


Output

2
10
8
0
4
16
1
6
6
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cassert>

using namespace std;
using ll = long long;


class SuffixArray
{
public:
    SuffixArray(const string& s) : s{s}, index1{static_cast<int>(s.size())}, suffix_array(index1 + 1), rank(index1 + 1), lcp(index1 + 1), buf(index1 + 1)
    {
        for (int i = 0; i <= index1; i++) {
            suffix_array[i] = i;
            rank[i] = i < index1 ? s[i] : -1;
        }
        auto comp = [&](const int i, const int j) {
            if (rank[i] != rank[j]) {
                return rank[i] < rank[j];
            } else {
                const int ri = (i + index2 <= index1) ? rank[i + index2] : -1;
                const int rj = (j + index2 <= index1) ? rank[j + index2] : -1;
                return ri < rj;
            }
        };
        for (index2 = 1; index2 <= index1; index2 *= 2) {
            sort(suffix_array.begin(), suffix_array.end(), comp);
            buf[suffix_array[0]] = 0;
            for (int i = 1; i <= index1; i++) {
                buf[suffix_array[i]] = buf[suffix_array[i - 1]] + (comp(suffix_array[i - 1], suffix_array[i]) ? 1 : 0);
            }
            for (int i = 0; i <= index1; i++) {
                rank[i] = buf[i];
            }
        }

        for (int i = 0; i <= index1; i++) {
            rank[suffix_array[i]] = i;
        }
        int h = 0;
        lcp[0] = 0;
        for (int i = 0; i < index1; i++) {
            {
                const int j = suffix_array[rank[i] - 1];
                if (h > 0) {
                    h--;
                }
                for (; j + h < index1 and i + h < index1; h++) {
                    if (s[j + h] != s[i + h]) {
                        break;
                    }
                }
                lcp[rank[i] - 1] = h;
            }
        }
    }
    bool contain(const string& t) const
    {
        int a = 0;
        int b = s.size();
        while (b - a > 1) {
            const int c = (a + b) / 2;
            if (s.compare(suffix_array[c], t.size(), t) < 0) {
                a = c;
            } else {
                b = c;
            }
        }
        return s.compare(suffix_array[b], t.size(), t) == 0;
    }

    int lower_bound(const string& suffix) const
    {
        int a = 0;
        int b = s.size() + 1;
        while (b - a > 1) {
            const int c = (a + b) / 2;
            if (s.compare(suffix_array[c], string::npos, suffix) < 0) {
                a = c;
            } else {
                b = c;
            }
        }
        return b;
    }

    int upper_bound(const string& suffix) const
    {
        int a = 0;
        int b = s.size() + 1;
        while (b - a > 1) {
            const int c = (a + b) / 2;
            if (s.compare(suffix_array[c], suffix.size(), suffix) <= 0) {
                a = c;
            } else {
                b = c;
            }
        }
        return b;
    }

    const vector<int>& getArray() const
    {
        return suffix_array;
    }

private:
    const string s;
    const int index1;
    int index2;
    vector<int> suffix_array;
    vector<int> rank;
    vector<int> lcp;
    vector<int> buf;
};


template <typename Base>
class SparseTable
{
public:
    using T = typename Base::T;
    using SemiLattice = Base;
    SparseTable(const vector<T>& val) : size(val.size()), lg2(size + 1, 0)
    {
        for (int i = 2; i <= size; i++) {
            lg2[i] = lg2[i / 2] + 1;
        }
        table.resize(size, vector<T>(lg2[size] + 1));
        for (int i = 0; i < size; i++) {
            table[i][0] = val[i];
        }
        for (int j = 0; j < lg2[size]; j++) {
            const int w = 1 << j;
            for (int i = 0; i <= size - (w << 1); i++) {
                T tl = table[i][j], tr = table[i + w][j];
                table[i][j + 1] = op(tl, tr);
            }
        }
    }

    T accumulate(const int l, const int r) const
    {
        assert(0 <= l and l < r and r <= size);
        const int j = lg2[r - l];
        return op(table[l][j], table[r - (1 << j)][j]);
    }


private:
    const int size;
    vector<int> lg2;
    vector<vector<T>> table;
    const SemiLattice op{};
};

struct Min {
    using T = int;
    T operator()(const T& a, const T& b) const
    {
        return min(a, b);
    }
};
struct Max {
    using T = int;
    T operator()(const T& a, const T& b) const
    {
        return max(a, b);
    }
};

int main()
{
    string s;
    cin >> s;
    SuffixArray sa(s);
    // for (const int i : sa.getArray()) {
    //     cerr << s.substr(i, s.size() - i) << endl;
    // }
    SparseTable<Min> stmin{sa.getArray()};
    SparseTable<Max> stmax{sa.getArray()};

    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        string x, y;
        cin >> x >> y;
        const int xl = sa.lower_bound(x);
        const int xr = sa.upper_bound(x);
        const int yl = sa.lower_bound(y);
        const int yr = sa.upper_bound(y);
        if (xl < xr and yl < yr) {
            const int lower = stmin.accumulate(xl, xr);
            const int upper = stmax.accumulate(yl, yr);
            if (lower + x.size() <= upper + y.size()) {
                cout << upper - lower + y.size() << endl;
            } else {
                cout << 0 << endl;
            }
        } else {
            cout << 0 << endl;
        }
    }
    return 0;
}