#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

#pragma warning(disable : 4996)

using namespace std;

vector<int> dat;
vector<int> tmp;

int doubles, lengths;

bool compare_suffix(int i, int j)
{
	if (dat[i] == dat[j])
	{
		int ri = (i + doubles <= lengths ? dat[i + doubles] : -1);
		int rj = (j + doubles <= lengths ? dat[j + doubles] : -1);

		return ri < rj;
	}

	return dat[i] < dat[j];
}

vector<int> suffix_array(string S)
{
	lengths = S.size();

	vector<int> arrays(lengths + 1);

	dat.resize(lengths + 1);
	tmp.resize(lengths + 1);

	for (int i = 0; i <= lengths; i++)
	{
		arrays[i] = i;

		dat[i] = i < lengths ? S[i] : -1;
	}

	for (doubles = 1; doubles <= lengths; doubles <<= 1)
	{
		sort(arrays.begin(), arrays.begin() + lengths + 1, compare_suffix);

		tmp[arrays[0]] = 0;

		for (int i = 1; i <= lengths; i++)
		{
			tmp[arrays[i]] = tmp[arrays[i - 1]] + (compare_suffix(arrays[i - 1], arrays[i]) ? 1 : 0);
		}

		for (int i = 0; i <= lengths; i++)
		{
			dat[i] = tmp[i];
		}
	}

	return arrays;
}

template <typename Type> class MinQuery
{
private:

	int size;

	vector<Type> minvalue;

	Type _query(int a, int b, int k, int l, int r)
	{
		if (r <= a || b <= l) { return INF; }
		if (a <= l && r <= b) { return minvalue[k]; }

		Type vl = _query(a, b, 2 * k, l, (l + r) / 2);
		Type vr = _query(a, b, 2 * k + 1, (l + r) / 2, r);

		return min(vl, vr);
	}

public:

	Type INF;

	MinQuery(int size_)
	{
		INF = numeric_limits<Type>::max();

		for (size = 1; size < size_;) { size <<= 1; }

		minvalue = vector<Type>(size * 2, INF);
	}

	void update(int i, const Type& x)
	{
		i += size;

		minvalue[i] = x;

		while (i > 1)
		{
			i = i / 2;

			minvalue[i] = min(minvalue[2 * i], minvalue[2 * i + 1]);
		}
	}

	Type query(int s, int t)
	{
		return _query(s, t, 1, 0, size);
	}
};

template <typename Type> class MaxQuery
{
private:

	int size;

	vector<Type> maxvalue;

	Type _query(int a, int b, int k, int l, int r)
	{
		if (r <= a || b <= l) { return INF; }
		if (a <= l && r <= b) { return maxvalue[k]; }

		Type vl = _query(a, b, 2 * k, l, (l + r) / 2);
		Type vr = _query(a, b, 2 * k + 1, (l + r) / 2, r);

		return max(vl, vr);
	}

public:

	Type INF;

	MaxQuery(int size_)
	{
		INF = numeric_limits<Type>::min();

		for (size = 1; size < size_;) { size <<= 1; }

		maxvalue = vector<Type>(size * 2, INF);
	}

	void update(int i, const Type& x)
	{
		i += size;

		maxvalue[i] = x;

		while (i > 1)
		{
			i = i / 2;

			maxvalue[i] = max(maxvalue[2 * i], maxvalue[2 * i + 1]);
		}
	}

	Type query(int s, int t)
	{
		return _query(s, t, 1, 0, size);
	}
};

int main()
{
	string S; cin >> S;

	vector<int> vas = suffix_array(S);

	MinQuery<int> Q1(lengths + 1); for (int i = 0; i <= lengths; i++) Q1.update(i, vas[i]);
	MaxQuery<int> Q2(lengths + 1); for (int i = 0; i <= lengths; i++) Q2.update(i, vas[i]);

	int Q; scanf("%d", &Q);

	string X, Y;

	for (int i = 0; i < Q; i++)
	{
		int xlen, ylen, xl, xr, yl, yr, l, r, m;

		cin >> X >> Y;

		xlen = X.size();
		ylen = Y.size();


		// ------ Left Search ------ //

		l = 0;
		r = lengths + 1;

		while (r - l > 1)
		{
			m = (l + r) / 2;

			string T = S.substr(vas[m], xlen);

			if (T < X)
			{
				l = m;
			}
			else
			{
				r = m;
			}
		}

		xl = r;

		l = 0;
		r = lengths + 1;

		while (r - l > 1)
		{
			m = (l + r) / 2;

			string T = S.substr(vas[m], xlen);

			if (T <= X)
			{
				l = m;
			}
			else
			{
				r = m;
			}
		}

		xr = r;


		// ------ Right Search ------ //

		l = 0;
		r = lengths + 1;

		while (r - l > 1)
		{
			m = (l + r) / 2;

			string T = S.substr(vas[m], ylen);

			if (T < Y)
			{
				l = m;
			}
			else
			{
				r = m;
			}
		}

		yl = r;

		l = 0;
		r = lengths + 1;

		while (r - l > 1)
		{
			m = (l + r) / 2;

			string T = S.substr(vas[m], ylen);

			if (T <= Y)
			{
				l = m;
			}
			else
			{
				r = m;
			}
		}

		yr = r;

		
		// ------ Query ------ //
		
		if (xl - xr == 0 || yl - yr == 0)
		{
			printf("0\n");
		}
		else
		{
			int petl = Q1.query(xl, xr);
			int petr = Q2.query(yl, yr);

			int ret = petr - petl + ylen;

			if (ret >= xlen)
			{
				printf("%d\n", petr - petl + ylen);
			}
			else
			{
				printf("0\n");
			}
		}
	}

	return 0;
}

Measured memory_bytes: 10016