#include<bits/stdc++.h>
 
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
 
using namespace std;
 
const int IINF = INT_MAX;
const int MAX_N = 2100000;
 
typedef pair<int,int> ii;
 
class RMQ{
private:
  int n,N;
  vector<ii> dat;
public:
 
  void init(int n_){
    n = 1;
    N = n_;
    while(n<n_) n *= 2;
    dat.clear();
    dat.resize(2*n-1);
    rep(i,2*n-1) dat[i] = ii(IINF,-IINF);
  }
 
  ii _build(int cur,int L,int R,const vector<int> &buf){
    if( !( 0 <= cur && cur < 2*n-1 ) ) return ii(IINF,-IINF);
    if( L == R-1 ) {
      if( L >= N ) return ii(IINF,-IINF);
      dat[cur] = ii(buf[L],buf[L]);
    } else {
      ii vl = _build(cur*2+1,L,(L+R)/2,buf);
      ii vr = _build(cur*2+2,(L+R)/2,R,buf);
      dat[cur] = ii(min(vl.first,vr.first),max(vl.second,vr.second));
    }
    return dat[cur];
  }
 
  void build(const vector<int> &buf){ _build(0,0,n,buf); }
 
  ii query(int a,int b,int k,int l,int r) {
    if( r <= a || b <= l ) return ii(IINF,-IINF);
    else if( a <= l && r <= b ) return dat[k];
    ii vl = query(a,b,k*2+1,l,(l+r)/2);
    ii vr = query(a,b,k*2+2,(l+r)/2,r);
    return ii(min(vl.first,vr.first),max(vl.second,vr.second));
  }
 
  ii _query(int a,int b){ return query(a,b,0,0,n); }
 
};
 
 
struct CompareSA{
  int k;
  const vector<int> &rank;
  CompareSA(const int k,const vector<int> &rank) : k(k), rank(rank){}
  bool operator()(int i,int j) {
    if( rank[i] != rank[j] ) return rank[i] < rank[j];
    int ri = i + k < (int)rank.size() ? rank[i+k] : -1;
    int rj = j + k < (int)rank.size() ? rank[j+k] : -1;
    return ri < rj;
  }
};
 
vector<int> build_sa(const string &s){
  int n = s.size();
  vector<int> sa(n+1), rank(n+1), tmp(n+1);
  rep(i,sa.size()){
    sa[i] = i;
    rank[i] = i < n ? s[i] : -1;
  }
  for(int k=1;k<=n;k*=2){
    CompareSA comp(k,rank);
    sort(sa.begin(),sa.end(),comp);
    tmp[sa[0]] = 0;
    for(int i=1;i<=n;i++) tmp[sa[i]] = tmp[sa[i-1]] + comp(sa[i-1],sa[i]);
    rank = tmp;
  }
  return sa;
}
 
vector<int> build_lcp(const string &s,vector<int> sa){
  int n = s.size(), h = 0;
  vector<int> rank(n+1), lcp(n+1);
  rep(i,n+1) rank[sa[i]] = i;
  rep(i,n) {
    int j = sa[rank[i]-1];
    if( h > 0 ) h--;
    for(; j * h < n && i * h < n; h++ ) if( sa[j+h] != sa[i+h] ) break;
    lcp[rank[i]-1] = h;
  }
  return lcp;
}
 
int find(const string &s,const vector<int> &sa,const string &t){
  int l = -1, r = sa.size();
  while( l + 1 < r ){
    int mid = ( l + r ) / 2;
    if( s.compare(sa[mid],t.size(),t) >= 0 )  r = mid;
    else                                      l = mid;
  }
  return r;
}
 
string S,x,y;
int m;
 
int main(){
  cin >> S;
  cin >> m;
  vector<int> sa = build_sa(S);
  RMQ rmq;
  rmq.init(sa.size());
  rmq.build(sa);
  rep(_,m){
    cin >> x >> y;
    int s1 = find(S,sa,x);
    int s2 = find(S,sa,x+(char)('z'+1));
    int t1 = find(S,sa,y);
    int t2 = find(S,sa,y+(char)('z'+1));
    ii res1 = rmq._query(s1,s2);
    ii res2 = rmq._query(t1,t2);
    if( res1.first == IINF || res2.second == -IINF ) {
      puts("0");
      continue;
    }
    int mini = res1.first;
    int maxi = res2.second + (int)y.size() -1;    
    if( ( mini <= res2.second && res2.second <= mini+(int)x.size()-1 ) && 
	( mini <= maxi && maxi < mini + (int)x.size()-1 )) {
      puts("0");
      continue;
    }
    if( mini > maxi ) puts("0");
    else {
      printf("%d\n",maxi-mini+1);
    }
  }
 
  return 0;
}

Measured memory_bytes: 6716