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>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isIn(x,y,h,w) (x >= 0 && x < h && y >= 0 && y < w)

#define int long long
//using ll = long long;
using P = pair<int,int>;

ostream &operator<<(ostream &os,const P &p){ return os << "(" << p.first << "," << p.second << ")"; }
template<class T>
ostream &operator<<(ostream &os,const vector<T> &v){
	for(int i = 0;i < v.size();i++) os << (i ? "," : "[") << v[i];
	os << "]";
	return os;
}

template<class T> T &chmin(T &a,const T &b){ return a = min(a,b); }
template<class T> T &chmax(T &a,const T &b){ return a = max(a,b); }

const int INF=1e+18;
const double EPS=1e-9;
const int MOD=1000000007;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

struct SuffixArray{
	string s;
	vector<int> sa;

	int operator[](int i) const{ return sa[i]; }

	SuffixArray(const string &t) : s(t),sa(s.size() + 1){
		s += '$';
		int n = s.size();
		iota(sa.begin(),sa.end(),0);
		sort(sa.begin(),sa.end(),[&](int a,int b){ return s[a] < s[b]; });
		vector<int> rnk(s.begin(),s.end()),cnt(n),nxt(n);
		for(int k = 1;k < n;k <<= 1){
			nxt[sa[0]] = 0;
			for(int i = 1;i < n;i++){
				nxt[sa[i]] = (rnk[sa[i - 1]] == rnk[sa[i]]
				&& rnk[(sa[i - 1] + k / 2) % n] == rnk[(sa[i] + k / 2) % n])
				? nxt[sa[i - 1]] : i;
			}
			rnk.swap(nxt);

			iota(cnt.begin(),cnt.end(),0);
			for(int i = 0;i < n;i++){
				int s1 = (sa[i] - k + n) % n;
				nxt[cnt[rnk[s1]]++] = s1;
			}
			sa.swap(nxt);
		}
	}

	inline int lower_bound(const string &t) const{
		return partition_point(sa.begin(),sa.end(),[&](int x){
			return s.compare(x,t.length(),t) < 0;
		}) - sa.begin();
	}

	int upper_bound(string &t) const{
		t.back()++;
		int ret = lower_bound(t);
		t.back()--;
		return ret;
	}

	inline int count(string &t) const{
		return upper_bound(t) - lower_bound(t);
	}
};

struct RMQ{
	int n,seg,init;
	vector<int> dat;

	RMQ(int siz,int def) : n(siz),init(def),seg(1){
		while(seg < n) seg *= 2;
		dat.resize(seg * 2 - 1);
		for(int i = 0;i < seg * 2 - 1;i++) dat[i] = init;
	}
	void update(int i,int x){
		i += seg - 1;
		dat[i] = x;
		while(i){
			i = (i - 1) / 2;
			dat[i] = min(dat[i * 2 + 1],dat[i * 2 + 2]); //do
		}
	}
	int get(int a = 0,int b = -1,int k = 0,int l = 0,int r = -1){
		if(b == -1) b = seg;
		if(r == -1) r = seg;
		if(b <= l || r <= a) return init;
		if(a <= l && r <= b) return dat[k];
		return min(get(a,b,k * 2 + 1,l,(l + r) / 2),get(a,b,k * 2 + 2,(l + r) / 2,r)); //do
	}
};

int ind(SuffixArray &sa,RMQ &rmq,string &s){
	int l = sa.lower_bound(s),r = sa.upper_bound(s);
	return rmq.get(l,r);
}

signed main(){
	string s;
	int n,m;
	cin >> s >> m;
	n = s.length();
	string t = s;
	reverse(all(t));
	SuffixArray sa1(s),sa2(t);
	RMQ rmq1(n + 5,INF),rmq2(n + 5,INF);
	for(int i = 0;i <= n;i++){
		rmq1.update(i,sa1[i]);
		rmq2.update(i,sa2[i]);
	}
	for(int i = 0;i < m;i++){
		string x,y;
		cin >> x >> y;
		reverse(all(y));
		int len = n - ind(sa1,rmq1,x) - ind(sa2,rmq2,y);
		if(len >= (int)max(x.size(),y.size())) cout << len << endl;
		else cout << 0 << endl;
	}
}