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

Measured memory_bytes: 42808