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 <bits/stdc++.h>
using namespace std;

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf=1e9;
const int64_t inf64=1e18;
const double eps=1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
    	os << v << ",";
    }
    os << "]";
    return os;
}

template<class T> class segtree{
public:
    int n,size_;
    vector<T> dat;
    function<T(T,T)> fun_;
    T id_;
    segtree()=default;
    segtree(int size,function<T(T,T)> fun,T id,T initial_value){ init(size,fun,id,initial_value); }
    void init(int size,function<T(T,T)> fun,T id,T initial_value){
        size_=size;
        fun_=fun;
        id_=id;
        n=1;
        while(n<size) n*=2;
        dat.assign(2*n-1,id);
        for(int i=0; i<size; ++i) update(i,initial_value);
    }
    int size()const{ return size_; }
    void update(int k, T a) {
        k+=n-1; // leaf
        dat[k]=a;
        while(k>0) {
            k=(k-1)/2;
            dat[k]=fun_(dat[k*2+1],dat[k*2+2]);
        }
    }
    T at(int index){ return dat[index+n-1]; }
    void add(int k,T a){ update(k,at(k)+a); }
    T query(int a,int b) { return query(a,b,0,0,n); }
    T query(int a,int b,int k,int l,int r) {
        if(r<=a or b<=l) return id_;
        if(a<=l and r<=b) return dat[k];
        int m=(l+r)/2;
        return fun_(query(a,b,k*2+1,l,m),query(a,b,k*2+2,m,r));
    }
};

vector<int> suffix_array(const string &s){
    const int n=s.size();
    vector<int> rank(n+1),sa(n+1),tmp(n+1);
    for(int i=0; i<=n; ++i){
        sa[i]=i;
        rank[i]=i<n?s[i]:-1;
    }
    for(int k=1; k<=n; k*=2){
        auto compare=[&](int i,int j){
            if(rank[i]!=rank[j]) return rank[i]<rank[j];
            else{
                int ri=i+k<=n?rank[i+k]:-1;
                int rj=j+k<=n?rank[j+k]:-1;
                return ri<rj;
            }
        };
        sort(sa.begin(),sa.end(),compare);
        tmp[sa[0]]=0;
        for(int i=1; i<=n; ++i){
            tmp[sa[i]]=tmp[sa[i-1]]+(compare(sa[i-1],sa[i])?1:0);
        }
        for(int i=0; i<=n; ++i){
            rank[i]=tmp[i];
        }
    }
    return sa;
}

int lower_bound(string &s,vector<int> &sa,string &x){
    int lb=-1,ub=sa.size()-1;
    while(ub-lb>1){
        int m=(lb+ub)/2;
        if(s.compare(sa[m],x.size(),x)>=0) ub=m;
        else lb=m;
    }
    return ub;
}

int upper_bound(string &s,vector<int> &sa,string &x){
    int lb=0,ub=sa.size();
    while(ub-lb>1){
        int m=(lb+ub)/2;
        if(s.compare(sa[m],x.size(),x)>0) ub=m;
        else lb=m;
    }
    return ub;
}

int min_index(string &s,vector<int> &sa,string &x,segtree<int> &seg){
    int l=lower_bound(s,sa,x),r=upper_bound(s,sa,x);
    return seg.query(l,r);
}

void solve(){
    string s1,s2;
    int m;
    cin >> s1 >> m;
    s2=s1;
    reverse(s2.begin(),s2.end());
    vector<int> sa1=suffix_array(s1),sa2=suffix_array(s2);
    segtree<int> seg1(sa1.size(),[](int x,int y){return min(x,y);},inf,inf),seg2(sa1.size(),[](int x,int y){return min(x,y);},inf,inf);
    rep(i,0,sa1.size()){
        seg1.update(i,sa1[i]);
        seg2.update(i,sa2[i]);
    }
    rep(i,0,m){
        string x,y;
        cin >> x >> y;
        reverse(y.begin(),y.end());
        if(s1.compare(sa1.back(),x.size(),x)<0 or s2.compare(sa2.back(),y.size(),y)<0){
            cout << 0 << endl;
            continue;
        }
        int pre=min_index(s1,sa1,x,seg1),suf=s1.size()-min_index(s2,sa2,y,seg2);
        if(pre+x.size()>suf) cout << 0 << endl;
        else cout << max(0,suf-pre) << endl;
    }
}

int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(10);
    solve();
    return 0;
}