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 <iostream>
#include <fstream>
#include <cassert>
#include <typeinfo>
#include <vector>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#include <random>
#include <complex>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<29;
const ll INF=1ll<<62;
const double pi=acos(-1);
const double eps=1e-7;
const ll mod=1e9+7;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

template <class T>
class RMQ{
	private:
	int n;
	vector<T> rmq;
	T Query_func(int a,int b,int k,int l,int r){
		if(r<=a||b<=l) return inf;
		if(a<=l&&r<=b) return rmq[k];
		int m=(l+r)/2;
		T vl=Query_func(a,b,k*2+1,l,m);
		T vr=Query_func(a,b,k*2+2,m,r);
		return (vl<vr)?vl:vr;
	}
	public:
	RMQ(int n_){
		n=1;
		while(n<n_) n*=2;
		rmq=vector<T>(2*n-1,inf);
	}
	void Update(int k,T x){
		k+=n-1;
		rmq[k]=x;
		while(k>0){
			k=(k-1)/2;
			rmq[k]=min(rmq[k*2+1],rmq[k*2+2]);
		}
	}
	T Query(int a,int b){
		return Query_func(a,b,0,0,n);
	}
};

int N,K;
vi Rank,Tmp;

bool Compare_SA(int i,int j){
	if(Rank[i]!=Rank[j]) return Rank[i]<Rank[j];
	int ri=i+K<=N?Rank[i+K]:-1,rj=j+K<=N?Rank[j+K]:-1;
	return ri<rj;
}

vi Suffix_Array(string s){
	N=s.size();
	vi sa(N+1);
	Rank=Tmp=vi(N+1);
	for(int i=0;i<=N;i++){
		sa[i]=i;
		Rank[i]=i<N?s[i]:-1;
	}
	for(K=1;K<=N;K*=2){
		sort(sa.begin(),sa.end(),Compare_SA);
		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);
		Rank=Tmp;
	}
	return sa;
}

vi LCP(string s,vi sa){
	int S=s.size(),h=0;
	vi rank(S+1),lcp(S);
	for(int i=0;i<=S;i++) rank[sa[i]]=i;
	for(int i=0;i<S;i++){
		if(h>0) h--;
		for(int j=sa[rank[i]-1];j+h<S&&i+h<S;h++) if(s[j+h]!=s[i+h]) break;
		lcp[rank[i]-1]=h;
	}
	return lcp;
}

string s;
int q,S;

vi f(vs a){
	string t=s;
	map<int,int> m;
	for(int i=0;i<q;i++){
		m[t.size()]=i;
		t+=a[i]+"_";
	}
	int T=t.size();
	vi res(q),sa=Suffix_Array(t),lcp=LCP(t,sa);
	RMQ<int> rmq1(T+1),rmq2(T);
	for(int i=0;i<=T;i++) rmq1.Update(i,sa[i]<S?sa[i]:inf);
	for(int i=0;i<T;i++) rmq2.Update(i,lcp[i]);
	for(int i=0;i<=T;i++) if(m.find(sa[i])!=m.end()){
		int I=m[sa[i]],l=i,r=T+1,S_=a[I].size();
		while(r-l>1){
			int mid=(l+r)/2;
			if(rmq2.Query(i,mid)>=S_) l=mid;
			else r=mid;
		}
		res[I]=rmq1.Query(i,l+1);
	}
	return res;
}

int main(){
	cin>>s>>q;
	S=s.size();
	vs a(q),b(q);
	for(int i=0;i<q;i++){
		cin>>a[i]>>b[i];
		reverse(b[i].begin(),b[i].end());
	}
	vi l=f(a),r;
	reverse(s.begin(),s.end());
	r=f(b);
	for(int i=0;i<q;i++){
		int len=max(S-r[i]-l[i],0);
		if(len<max(a[i].size(),b[i].size())) cout<<0<<endl;
		else cout<<len<<endl;
	}
}