#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl
 
using namespace std;
 
typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
 
const int MAX_N = 100005;
 
class suffixarray{
public:
    int sz,index1,index2;
    vector<int> rnk,tmp,sa,lcp;
    string recs;
    suffixarray(string& s){
        recs = s;
        sz = (int)s.size();
        rnk.resize(sz+1),tmp.resize(sz+1);
        make_sa();
    }
    void make_sa(){
        index1 = sz;
        sa.resize(index1+1);
        rep(i,index1+1){
            sa[i] = i;
            rnk[i] = i<index1?recs[i]:-1;
        }
        auto comp = [&](int i,int j){
            if(rnk[i] != rnk[j]){
                return rnk[i] < rnk[j];
            }else{
                int ri = (i+index2<=index1)?rnk[i+index2]:-1;
                int rj = (j+index2<=index1)?rnk[j+index2]:-1;
                return ri < rj;
            }
        };
        for(index2=1;index2<=index1;index2*=2){
            sort(sa.begin(),sa.end(),comp);
            tmp[sa[0]] = 0;
            for(int i=1;i<=index1;i++){
                tmp[sa[i]] = tmp[sa[i-1]]+(comp(sa[i-1],sa[i])?1:0);
            }
            rep(i,index1+1){
                rnk[i] = tmp[i];
            }
        }
    }
    //lcp[i]???sa[i]??¨sa[i+1]???????????±?????\???????????\???
    //??????????????????lca??????????????????????????????????????????sa???unzip????????°??????
    void make_lcp(){
        lcp.resize(sz+1);
        rep(i,sz+1){
            rnk[sa[i]] = i;
        }
        int h = 0;
        lcp[0] = 0;
        rep(i,sz){
            int j = sa[rnk[i]-1];
            if(h > 0){
                h--;
            }
            for(;j+h<sz&&i+h<sz;h++){
                if(recs[j+h] != recs[i+h]){
                    break;
                }
            }
            lcp[rnk[i]-1] = h;
        }
    }
    int stsearch(string& cri);
    int edsearch(string& cri);
};
 
template<typename V> class segtree {
private:
    int n,sz;
    vector<V> node;
public:
    segtree(vector<V>& v){
        sz = (int)v.size();
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1);
        rep(i,sz){
            node[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; i--){
            node[i] = min(node[i*2+1],node[i*2+2]);
        }
    }
    void update(int k,V a)
    {
        k += n-1;
        node[k] = a;
        while(k>0){
            k = (k-1)/2;
            node[k] = min(node[2*k+1],node[2*k+2]);
        }
    }
    V query(int a,int b,int k=0,int l=0,int r=-1)
    {
        if(r < 0) r = n;
        if(r <= a || b <= l){
            return numeric_limits<V>::max();
        }
        if(a <= l && r <= b){
            return node[k];
        }else{
            V vl = query(a,b,2*k+1,l,(l+r)/2);
            V vr = query(a,b,2*k+2,(l+r)/2,r);
            return min(vl,vr);
        }
    }
    void print()
    {
        rep(i,sz){
            cout << query(i,i+1) << " ";
        }
        cout << endl;
    }
};
 
int suffixarray::stsearch(string& cri)
{
    int l=0,r=sz+1;
    while(r-l>1){
        int mid = (l+r)/2;
        if(sz-sa[mid] < len(cri)){
            string t = recs.substr(sa[mid],sz-sa[mid]);
            if(t <= cri){
                l = mid;
            }else{
                r = mid;
            }
        }else{
            string t = recs.substr(sa[mid],len(cri));
            if(t < cri){
                l = mid;
            }else{
                r = mid;
            }
        }
    }
    return r;
}
 
int suffixarray::edsearch(string& cri)
{
    int l=0,r=sz+1;
    while(r-l>1){
        int mid = (l+r)/2;
        if(sz-sa[mid] < len(cri)){
            string t = recs.substr(sa[mid],sz-sa[mid]);
            if(t < cri){
                l = mid;
            }else{
                r = mid;
            }
        }else{
            string t = recs.substr(sa[mid],len(cri));
            if(t <= cri){
                l = mid;
            }else{
                r = mid;
            }
        }
    }
    return r;
}
 
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    suffixarray sa(s);
    vi tmp(len(sa.sa));
    rep(i,len(tmp)){
        tmp[i] = -sa.sa[i];
    }
    // rep(i,len(sa.sa)){
    //     cout << i << " " << s.substr(sa.sa[i],len(s)-sa.sa[i]) << "\n";
    // }
    segtree<int> s1(sa.sa),s2(tmp);
    int m;
    cin >> m;
    rep(i,m){
        string x,y;
        cin >> x >> y;
        int r1 = sa.stsearch(x),r2 = sa.edsearch(x);
        int r3 = sa.stsearch(y),r4 = sa.edsearch(y);
        // cout << r1 << " " << r2 << " " << r3 << " " << r4 << "\n";
        if(r1 >= r2 || r3 >= r4){
            cout << "0\n";
            continue;
        }
        int v1 = s1.query(r1,r2),v2 = -s2.query(r3,r4);
        if(v1+len(x)-len(y) > v2){
            cout << "0\n";
        }else{
            cout << v2+len(y)-v1 << "\n";
        }
    }
    return 0;
}


Measured memory_bytes: 11080