#include<bits/stdc++.h>
using namespace std;
const int M = 1000000007;

class suffix_array {
    // compare (rank[i], rank[i + k]) and (rank[j], rank[j + k])
    static bool compare_sa(int n, const vector<int>& rank, int i, int j, int k) {
        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;
    }
public:
    static vector<int> construct_sa(const string& s) {
        int n = s.length();
        vector<int> sa(n + 1), rank(n + 1);
        for (int i = 0; i <= n; ++i) {
            sa[i] = i;
            rank[i] = i < n ? s[i] : -1;
        }
        for (int k = 1; k <= n; k <<= 1) {
            sort(sa.begin(), sa.end(), [&n, &k, &rank](const int& a, const int& b){ return compare_sa(n, rank, a, b, k); });

            vector<int> tmp(n + 1);
            for (int i = 1; i <= n; ++i)
                tmp[sa[i]] = tmp[sa[i - 1]] + compare_sa(n, rank, sa[i - 1], sa[i], k);
            for (int i = 0; i <= n; ++i)
                rank[i] = tmp[i];
        }
        return sa;
    }
    static vector<int> construct_lcp(const string& s, const vector<int>& sa) {
        int n = s.length();
        vector<int> rank(n + 1), lcp(n + 1);
        for (int i = 0; i <= n; ++i) rank[sa[i]] = i;

        int h = 0;
        for (int i = 0; i < n; ++i) {
            if (h > 0) --h;
            for (int j = sa[rank[i] - 1]; j + h < n && i + h < n; ++h)
                if (s[j + h] != s[i + h]) break;

            lcp[rank[i] - 1] = h;
        }
        return lcp;
    }
};

class min_segtree {
private:
    int n, s, t;
    vector<int> tr;
    const int ex = M;

    int q(int k, int l, int r) {
        return r <= s || t <= l ? ex : s <= l && r <= t ? tr[k]
                : min(q(k << 1 | 1, l, (l + r) >> 1), q((k + 1) << 1, (l + r) >> 1, r));
    }

public:
    min_segtree(int m) {
        n = 1;
        while (n < m) n <<= 1;
        tr.clear();
        tr.resize((n << 1) - 1, ex);
    }
    void update(int j, const int& x) {
        int i = j + n - 1;
        tr[i] = x;
        while (i > 0) { i = (i - 1) >> 1; tr[i] = min(tr[i << 1 | 1], tr[(i + 1) << 1]); }
    }
    // [s, t)
    int run(int _s, int _t) { s = _s; t = _t; return q(0, 0, n); }
};

class max_segtree {
private:
    int n, s, t;
    vector<int> tr;
    const int ex = -1;

    int q(int k, int l, int r) {
        return r <= s || t <= l ? ex : s <= l && r <= t ? tr[k]
                : max(q(k << 1 | 1, l, (l + r) >> 1), q((k + 1) << 1, (l + r) >> 1, r));
    }

public:
    max_segtree(int m) {
        n = 1;
        while (n < m) n <<= 1;
        tr.clear();
        tr.resize((n << 1) - 1, ex);
    }
    void update(int j, const int& x) {
        int i = j + n - 1;
        tr[i] = x;
        while (i > 0) { i = (i - 1) >> 1; tr[i] = max(tr[i << 1 | 1], tr[(i + 1) << 1]); }
    }
    // [s, t)
    int run(int _s, int _t) { s = _s; t = _t; return q(0, 0, n); }
};


int main() {
    string s;
    int q;
    cin >> s >> q;
    int n = s.length();
    vector<int> sa = suffix_array::construct_sa(s);
    min_segtree minsg(n + 1);
    max_segtree maxsg(n + 1);
    for (int i = 0; i < n + 1; ++i) {
        minsg.update(i, sa[i]);
        maxsg.update(i, sa[i]);
    }
    for (int _ = 0; _ < q; ++_) {
        string x, y;
        cin >> x >> y;
        int lx, rx, ly, ry;
        {
            int l = -1, ue = n + 1;
            while (ue - l > 1) {
                int m = (l + ue) / 2;
                if (strcmp(x.c_str(), s.c_str() + sa[m]) <= 0)
                    ue = m;
                else
                    l = m;
            }
            lx = ue;
        }
        x += '{';
        {
            int le = -1, u = n + 1;
            while (u - le > 1) {
                int m = (le + u) / 2;
                if (strcmp(x.c_str(), s.c_str() + sa[m]) < 0)
                    u = m;
                else
                    le = m;
            }
            rx = u;
        }
        {
            int l = -1, ue = n + 1;
            while (ue - l > 1) {
                int m = (l + ue) / 2;
                if (strcmp(y.c_str(), s.c_str() + sa[m]) <= 0)
                    ue = m;
                else
                    l = m;
            }
            ly = ue;
        }
        y += '{';
        {
            int le = -1, u = n + 1;
            while (u - le > 1) {
                int m = (le + u) / 2;
                if (strcmp(y.c_str(), s.c_str() + sa[m]) < 0)
                    u = m;
                else
                    le = m;
            }
            ry = u;
        }
        int ans = maxsg.run(ly, ry) - minsg.run(lx, rx) + y.length() - 1;
        if (ans < (int)max(x.length(), y.length()) - 1) ans = 0;
        cout << ans << "\n";
    }
    return 0;
}



Measured memory_bytes: 8792