#include<bits/stdc++.h>
#define MAX_N 2000005
#define rank Ajgrsf
using namespace std;
typedef long long ll;
const int N = 1<<18;
const ll INF= INT_MAX; 

class RMQ{
public:
  ll n;
  vector<ll>dat;
  RMQ(){dat.resize(2*N-1),n=N;for(int i=0;i<n;i++) dat[i]=INF;}
  
  void update(int k,ll a){
    k+=n-1;
    dat[k]=a;
    while(k>0){
      k=(k-1)/2;
      dat[k]=min(dat[k*2+1],dat[k*2+2]);
    }
  }

  ll query(int a,int b,int k=0,int l=0,int r=N){
    if(r<=a||b<=l)return INF;
    if(a<=l&&r<=b)return dat[k];
    ll vl=query(a,b,k*2+1,l,(l+r)/2);
    ll vr=query(a,b,k*2+2,(l+r)/2,r);
    return min(vl,vr);
  }
};


class SA{
public:
  int Len,n;
  vector<int> rank;
  vector<int> tmp;
  vector<int> sa;
  SA(){
    rank.resize(MAX_N+1);
    tmp.resize(MAX_N+1);
    sa.resize(MAX_N+1);
  }
  
  //(rank[i],rank[i+k])??¨(rank[j],rank[j+k])?????????
  bool compare_sa(int i,int j){
    if(rank[i]!=rank[j])return rank[i]<rank[j];
    int ri=i+Len<=n?rank[i+Len]:-1;
    int rj=j+Len<=n?rank[j+Len]:-1;
    return ri<rj;
  }

  //?????????S?????\?°??????????????§????
  void construct_sa(string S){
    n=S.length();
    //?????????????????????????????????????????????????????????
    for(int i=0;i<=n;i++){
      sa[i]=i;
      rank[i]=i<n?S[i]:-1;
    }
    //k????????????????????????????????????????????¨???????????????2k????????§?????????
    for(Len=1;Len<=n;Len*=2){
      sort(sa.begin(),sa.begin()+n+1,[&] (int i,int j){
          if(rank[i]!=rank[j])return rank[i]<rank[j];
          int ri=i+Len<=n?rank[i+Len]:-1;
          int rj=j+Len<=n?rank[j+Len]:-1;
          return ri<rj;
        });
      //?????£??????tmp????¬?????????????????¨?????????????rank?????£??\
      tmp[sa[0]]=0;
      for(int i=1;i<=n;i++)
        tmp[sa[i]]=tmp[sa[i-1]]+(compare_sa(sa[i-1],sa[i])?1:0);
      for(int i=0;i<=n;i++)rank[i]=tmp[i];
    }
  }

  //S??????????????????T????????????????????????????´¢??????
  bool contain(string S,string T){
    int left=0,right=S.length();
    while(right-left>1){
      int mid=(left+right)/2;
      if(S.compare(sa[mid],T.length(),T)<0)left=mid;
      else right=mid;
    }
    return S.compare(sa[right],T.length(),T)==0;
  }
};




int Lower(string &S,vector<int> &sa,string &T){
  int L=0,M,R=S.size();
  while(L<R){
    M = (L+R)/2;
    if(S.compare(sa[M],T.length(),T)>=0) R = M;
    else L = M+1;
  }  
  return L;
}

int Upper(string &S,vector<int> &sa,string &T){
  int L=0,M,R=S.size();
  while(L<R){
    M = (L+R)/2;
    if(S.compare(sa[M],T.length(),T)>0) R = M;
    else L = M+1;
  }  
  return L;
}


int A[MAX_N],B[MAX_N];
int main(){
  string S,rS;
  int n;
  cin>>S; rS = S;
  reverse(rS.begin(),rS.end());
  S+='{';rS+='{';
  n = S.size();
  SA A,B;
  A.construct_sa(S);
  B.construct_sa(rS);

  RMQ AT,BT;
  for(int i=0;i<=n;i++){
    AT.update(i,A.sa[i]);
    BT.update(i,B.sa[i]);
  }

  int q;  
  cin>>q;
  while(q--){
    string a,b;
    cin>>a>>b;
    reverse(b.begin(),b.end());
    if(!A.contain(S,a)||!B.contain(rS,b)){printf("0\n");continue;}
    int l = AT.query(Lower(S,A.sa,a),Upper(S,A.sa,a));
    int r = n-BT.query(Lower(rS,B.sa,b),Upper(rS,B.sa,b))-b.size()-1;
    if(l+a.size()>r+b.size())printf("0\n");
    else printf("%d\n",r-l+(int)b.size());
  }
  return 0;
}

Measured memory_bytes: 58924