#include <bits/stdc++.h>
using namespace std;

const function<int(int, int)> minQ = [](int l, int r) { return min(l, r); };

template <typename T>
class SegmentTree {
	using func_t = function<T(T, T)>;
	const int n;
	const T id;
	func_t merge;
	vector<T> data;
	int size(int n) {
		int res;
		for (res = 1; res < n; res <<= 1);
		return res;
	}
	T sub(int l, int r, int node, int lb, int ub) {
		if (ub <= l || r <= lb) return id;
		if (l <= lb && ub <= r) return data[node];
		return merge(sub(l, r, node * 2, lb, (lb + ub) / 2), sub(l, r, node * 2 + 1, (lb + ub) / 2, ub));
	}
public:
	SegmentTree() : n(0), id(0) {}
	SegmentTree(const SegmentTree& st) :
		n(st.n), id(st.id), merge(st.merge), data(st.data) {}
	SegmentTree(SegmentTree&& st) :
		n(st.n), id(st.id), merge(st.merge), data(st.data) {}
	SegmentTree(int n_, T id_, func_t merge_) :
		n(size(n_)), id(id_), merge(merge_), data(size(n_) * 2, id_) {}
	SegmentTree(const vector<T>& data_, T id_, func_t merge_) :
		n(size(data_.size())), id(id_), merge(merge_), data(n * 2, id_) {
		for (int i = 0; i < (int)data_.size(); i++)
			data[i + n] = data_[i];
		for (int i = n - 1; i >= 0; i--)
			data[i] = merge(data[i * 2], data[i * 2 + 1]);
	}
	SegmentTree(vector<T>&& data_, T id_, func_t merge_) :
		n(size(data_.size())), id(id_), merge(merge_), data(n * 2, id_) {
		for (int i = 0; i < (int)data_.size(); i++)
			data[i + n] = data_[i];
		for (int i = n - 1; i >= 0; i--)
			data[i] = merge(data[i * 2], data[i * 2 + 1]);
	}
	void Update(int p, T val) {
		p += n;
		data[p] = val;
		while (p >>= 1) data[p] = merge(data[p * 2], data[p * 2 + 1]);
	}
	void Add(int p, T val) {
		p += n;
		data[p] += val;
		while (p >>= 1) data[p] = merge(data[p * 2], data[p * 2 + 1]);
	}
	T Find(int i) {
		return data[i + n];
	}
	T Find(int l, int r) {
		return sub(l, r + 1, 1, 0, n);
	}
};

class StringSearch {
	const int n;
	string S;
	vector<int> sa, rank;
	SegmentTree<int> sast;
public:
	StringSearch(const string &S_)
		: n(S_.size()), S(S_), sa(n + 1), rank(n + 1), sast(n + 1, n, minQ)
	{
		for (int i = 0; i <= n; i++) {
			sa[i] = i;
			rank[i] = i < n ? S[i] : -1;
		}
		vector<int> tmp(n + 1);
		for (int k = 1; k <= n; k *= 2) {
			auto 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;
				int rj = j + k <= n ? rank[j + k] : -1;
				return ri < rj;
			};
			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);
			}
			for (int i = 0; i <= n; i++) {
				rank[i] = tmp[i];
			}
		}
		for (int i = 0; i <= n; i++) {
			sast.Update(i, sa[i]);
		}
	}
	int front(string T) {
		int s = Pos(T);
		char c = T.back();
		T.pop_back();
		T.push_back(c + 1);
		return sast.Find(s, Pos(T) - 1);
	}
	int Pos(const string &T) {
		int a = 0, b = n + 1;
		while (a + 1 < b) {
			int c = (a + b) / 2;
			if (S.compare(sa[c], T.length(), T) < 0) a = c;
			else b = c;
		}
		return b;
	}
};

int main()
{
	string S, x, y;
	cin >> S;
	int n = S.size();
	StringSearch ss(S);
	reverse(S.begin(), S.end());
	StringSearch rss(S);
	int m;
	cin >> m;
	while (m--) {
		cin >> x >> y;
		reverse(y.begin(), y.end());
		int res = n - ss.front(x) - rss.front(y);
		cout << (res < 0 || res < (int)x.size() || res < (int)y.size() ? 0 : res) << endl;
	}
	return 0;
}

Measured memory_bytes: 11436