Problem: 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

Solution (in CPP):

Solution:
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;

const int ALPH = 256;
struct SuffixArray {
private:
    string s;
    vector<int> rec, cnt, ver;
    int len, cnt_size;
public:
    SuffixArray(string s_) : s(s_) {
        s += "$";
        len = s.length();
        cnt_size = max(ALPH, len); cnt.resize(cnt_size);
        rec = getArray();
        ver = getRevArray(rec);
    }

    vector<int> getArray() {
        vector<int> indices(len), prev_idx(len);
        vector<int> classes(len), prev_cls(len);
        vector<int> tmp_val(len);

        for(int i=0; i<len; i++) {
            cnt[ s[i] ]++;
        }
        for(int i=1; i<cnt_size; i++) {
            cnt[i] += cnt[i-1];
        }
        for(int i=len-1; i>=0; i--) {
            indices[ --cnt[ s[i] ] ] = i;
        }
        for(int i=1; i<len; i++) {
            int pena = (s[ indices[i] ] != s[ indices[i-1] ]);
            classes[ indices[i] ] = classes[ indices[i-1] ] + pena;
        }
        for(int k=0; (1 << k) < len; k++) {
            swap(indices, prev_idx);
            swap(classes, prev_cls);
            for(int i=0; i<len; i++) {
                prev_idx[i] = prev_idx[i] - (1 << k);
                if(prev_idx[i] < 0) prev_idx[i] += len;
                tmp_val[i] = prev_cls[ prev_idx[i] ];
            }

            fill(cnt.begin(), cnt.end(), (int)0);
            for(int i=0; i<len; i++) {
                cnt[ tmp_val[i] ]++;
            }
            for(int i=1; i<cnt_size; i++) {
                cnt[i] += cnt[i-1];
            }
            for(int i=len-1; i>=0; i--) {
                indices[ --cnt[ tmp_val[i] ] ] = prev_idx[i];
            }
            classes[ indices[0] ] = 0;
            for(int i=1; i<len; i++) {
                int shift = (1 << k);
                pair<int, int> pre(prev_cls[ indices[i-1] ], prev_cls[ (indices[i-1] + shift) % len ]);
                pair<int, int> cur(prev_cls[ indices[i  ] ], prev_cls[ (indices[i  ] + shift) % len ]);
                int pena = (pre != cur);
                classes[ indices[i] ] = classes[ indices[i-1] ] + pena;
            }
        }
        return indices;
    }

    vector<int> getRevArray(vector<int> vec) {
        int N = vec.size();
        vector<int> res(N);
        for(int i=0; i<N; i++) {
            res[ vec[i] ] = i;
        }
        return res;
    }

    int size() {
        return rec.size();
    }
    int getIdx(int idx) {
        return rec[idx];
    }
    int getIdxRev(int idx) {
        return ver[idx];
    }
    bool is_greater(string &p, int idx, bool chg = false) {
        if(chg) idx = getIdx(idx);
        int N = size(), M = p.size(), x, y;
        for(x=idx, y=0; x<N and y<M; x++, y++) {
            if(s[x] < p[y]) return true;
            if(s[x] > p[y]) return false;
        }
        return x >= N and y < M;
    }
    bool is_less(string &p, int idx, bool chg = false) {
        if(chg) idx = getIdx(idx);
        int N = size(), M = p.size(), x, y;
        for(x=idx, y=0; x<N and y<M; x++, y++) {
            if(s[x] > p[y]) return true;
            if(s[x] < p[y]) return false;
        }
        return y >= M and x < N;
    }
};

const int INF = 1 << 28;
const int SIZE = 1 << 18;
struct SegmentTreeMin {
    int node[2 * SIZE];
    SegmentTreeMin() {
        fill(node, node + 2*SIZE, INF);
    }
    void update(int x, int val) {
        x += SIZE - 1;
        node[x] = min(node[x], val);
        while(x > 0) {
            x = (x - 1) / 2;
            node[x] = min(node[2*x+1], node[2*x+2]);
        }
    }
    int query(int a, int b, int l=0, int r=SIZE, int k=0) {
        if(b <= l or r <= a) return INF;
        if(a <= l and r <= b) return node[k];
        int mid = (l + r) / 2;
        int vl = query(a, b, l, mid, 2*k+1);
        int vr = query(a, b, mid, r, 2*k+2);
        return min(vl, vr);
    }
};

struct SegmentTreeMax {
    int node[2 * SIZE];
    SegmentTreeMax() {
        fill(node, node + 2*SIZE, -INF);
    }
    void update(int x, int val) {
        x += SIZE - 1;
        node[x] = max(node[x], val);
        while(x > 0) {
            x = (x - 1) / 2;
            node[x] = max(node[2*x+1], node[2*x+2]);
        }
    }
    int query(int a, int b, int l=0, int r=SIZE, int k=0) {
        if(b <= l or r <= a) return -INF;
        if(a <= l and r <= b) return node[k];
        int mid = (l + r) / 2;
        int vl = query(a, b, l, mid, 2*k+1);
        int vr = query(a, b, mid, r, 2*k+2);
        return max(vl, vr);
    }
};

SegmentTreeMin segMin;
SegmentTreeMax segMax;

int main() {
    string s; cin >> s;
    SuffixArray sa(s);
    int L = s.length();
    for(int i=0; i<s.length() + 1; i++) {
        int idx = sa.getIdx(i);
        segMin.update(i, idx);
        segMax.update(i, idx);
    }

    for(int i=0; i<s.length() + 1; i++) {
        int idx = sa.getIdx(i);
        int rev = sa.getIdx(i);
        // fprintf(stderr, "%2d %2d %s\n", i, rev, s.substr(idx).c_str());
    }

    // // fprintf(stderr, "pass\n");
  
    int N; cin >> N;
    char big = (char)('z' + 1);
    for(int i=0; i<N; i++) {
        string pref, suf; cin >> pref >> suf;
        int lp = pref.size(), ls = suf.size();
        int ansL, ansR;
        // // fprintf(stderr, "pass\n");
        {
            int l = 0, r = 0;
            {  
                int ub = L+2, lb = -1;
                while(ub - lb > 1) {
                    int mid = (ub + lb) / 2;
                    int m = sa.getIdx(mid);
                    // fprintf(stderr, "mid = %d, m = %d\n", mid, m);
                    if(sa.is_greater(pref, m)) {
                        lb = mid;
                    }
                    else {
                        ub = mid;
                    }
                }
                // fprintf(stderr, "i = %d, lb = %d, ub = %d\n", i, lb, ub);
                l = ub;
                if(l == L+2) {
                    cout << 0 << endl;
                    continue;
                }
            }

            {
                pref += big;
                int ub = L+2, lb = -1;
                while(ub - lb > 1) {
                    int mid = (ub + lb) / 2;
                    int m = sa.getIdx(mid);
                    if(sa.is_greater(pref, m)) {
                        lb = mid;
                    }
                    else {
                        ub = mid;
                    }
                }
                r = lb;
                if(r == -1) {
                    cout << 0 << endl;
                    continue;
                }
            }
            // fprintf(stderr, "i = %d, L: [%d, %d)\n", i, l, r+1);
            ansL = segMin.query(l, r+1);
        }

        {
            int l = 0, r = 0;
            {  
                int ub = L+2, lb = -1;
                while(ub - lb > 1) {
                    int mid = (ub + lb) / 2;
                    int m = sa.getIdx(mid);
                    if(sa.is_greater(suf, m)) {
                        lb = mid;
                    }
                    else {
                        ub = mid;
                    }
                }
                l = ub;
                if(l == L+2) {
                    cout << 0 << endl;
                    continue;
                }
            }

            {
                suf += big;
                int ub = L+2, lb = -1;
                while(ub - lb > 1) {
                    int mid = (ub + lb) / 2;
                    int m = sa.getIdx(mid);
                    if(sa.is_greater(suf, m)) {
                        lb = mid;
                    }
                    else {
                        ub = mid;
                    }
                }
                r = lb;
                if(r == -1) {
                    cout << 0 << endl;
                    continue;
                }
            }

            // fprintf(stderr, "i = %d, R: [%d, %d)\n", i, l, r+1);
            ansR = segMax.query(l, r+1);  
        }

        // // fprintf(stderr, "ansL = %lld, ansR = %lld\n", ansL, ansR);
        int pr = ansL + lp - 1;
        int sr = ansR + ls - 1;
        // fprintf(stderr, "[%d, %d], [%d, %d]\n", ansL, pr, ansR, sr);
        if(pr > sr or ansR < ansL) {
            cout << 0 << endl;
            continue;
        }
        else {
            cout << ansR - ansL + ls << endl;
        }
    }
    return 0;
}