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>
 
#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;
}