#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cassert>

using namespace std;
using ll = long long;


class SuffixArray
{
public:
    SuffixArray(const string& s) : s{s}, index1{static_cast<int>(s.size())}, suffix_array(index1 + 1), rank(index1 + 1), lcp(index1 + 1), buf(index1 + 1)
    {
        for (int i = 0; i <= index1; i++) {
            suffix_array[i] = i;
            rank[i] = i < index1 ? s[i] : -1;
        }
        auto comp = [&](const int i, const int j) {
            if (rank[i] != rank[j]) {
                return rank[i] < rank[j];
            } else {
                const int ri = (i + index2 <= index1) ? rank[i + index2] : -1;
                const int rj = (j + index2 <= index1) ? rank[j + index2] : -1;
                return ri < rj;
            }
        };
        for (index2 = 1; index2 <= index1; index2 *= 2) {
            sort(suffix_array.begin(), suffix_array.end(), comp);
            buf[suffix_array[0]] = 0;
            for (int i = 1; i <= index1; i++) {
                buf[suffix_array[i]] = buf[suffix_array[i - 1]] + (comp(suffix_array[i - 1], suffix_array[i]) ? 1 : 0);
            }
            for (int i = 0; i <= index1; i++) {
                rank[i] = buf[i];
            }
        }

        for (int i = 0; i <= index1; i++) {
            rank[suffix_array[i]] = i;
        }
        int h = 0;
        lcp[0] = 0;
        for (int i = 0; i < index1; i++) {
            {
                const int j = suffix_array[rank[i] - 1];
                if (h > 0) {
                    h--;
                }
                for (; j + h < index1 and i + h < index1; h++) {
                    if (s[j + h] != s[i + h]) {
                        break;
                    }
                }
                lcp[rank[i] - 1] = h;
            }
        }
    }
    bool contain(const string& t) const
    {
        int a = 0;
        int b = s.size();
        while (b - a > 1) {
            const int c = (a + b) / 2;
            if (s.compare(suffix_array[c], t.size(), t) < 0) {
                a = c;
            } else {
                b = c;
            }
        }
        return s.compare(suffix_array[b], t.size(), t) == 0;
    }

    int lower_bound(const string& suffix) const
    {
        int a = 0;
        int b = s.size() + 1;
        while (b - a > 1) {
            const int c = (a + b) / 2;
            if (s.compare(suffix_array[c], string::npos, suffix) < 0) {
                a = c;
            } else {
                b = c;
            }
        }
        return b;
    }

    int upper_bound(const string& suffix) const
    {
        int a = 0;
        int b = s.size() + 1;
        while (b - a > 1) {
            const int c = (a + b) / 2;
            if (s.compare(suffix_array[c], suffix.size(), suffix) <= 0) {
                a = c;
            } else {
                b = c;
            }
        }
        return b;
    }

    const vector<int>& getArray() const
    {
        return suffix_array;
    }

private:
    const string s;
    const int index1;
    int index2;
    vector<int> suffix_array;
    vector<int> rank;
    vector<int> lcp;
    vector<int> buf;
};


template <typename Base>
class SparseTable
{
public:
    using T = typename Base::T;
    using SemiLattice = Base;
    SparseTable(const vector<T>& val) : size(val.size()), lg2(size + 1, 0)
    {
        for (int i = 2; i <= size; i++) {
            lg2[i] = lg2[i / 2] + 1;
        }
        table.resize(size, vector<T>(lg2[size] + 1));
        for (int i = 0; i < size; i++) {
            table[i][0] = val[i];
        }
        for (int j = 0; j < lg2[size]; j++) {
            const int w = 1 << j;
            for (int i = 0; i <= size - (w << 1); i++) {
                T tl = table[i][j], tr = table[i + w][j];
                table[i][j + 1] = op(tl, tr);
            }
        }
    }

    T accumulate(const int l, const int r) const
    {
        assert(0 <= l and l < r and r <= size);
        const int j = lg2[r - l];
        return op(table[l][j], table[r - (1 << j)][j]);
    }


private:
    const int size;
    vector<int> lg2;
    vector<vector<T>> table;
    const SemiLattice op{};
};

struct Min {
    using T = int;
    T operator()(const T& a, const T& b) const
    {
        return min(a, b);
    }
};
struct Max {
    using T = int;
    T operator()(const T& a, const T& b) const
    {
        return max(a, b);
    }
};

int main()
{
    string s;
    cin >> s;
    SuffixArray sa(s);
    // for (const int i : sa.getArray()) {
    //     cerr << s.substr(i, s.size() - i) << endl;
    // }
    SparseTable<Min> stmin{sa.getArray()};
    SparseTable<Max> stmax{sa.getArray()};

    int m;
    cin >> m;
    for (int i = 0; i < m; i++) {
        string x, y;
        cin >> x >> y;
        const int xl = sa.lower_bound(x);
        const int xr = sa.upper_bound(x);
        const int yl = sa.lower_bound(y);
        const int yr = sa.upper_bound(y);
        if (xl < xr and yl < yr) {
            const int lower = stmin.accumulate(xl, xr);
            const int upper = stmax.accumulate(yl, yr);
            if (lower + x.size() <= upper + y.size()) {
                cout << upper - lower + y.size() << endl;
            } else {
                cout << 0 << endl;
            }
        } else {
            cout << 0 << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 48876