#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)

template<ll MOD>
class rolloing_hash {
public:
    int n;
    ll b;
    vector<ll> pow, hash;

    rolloing_hash() {}

    rolloing_hash(const string &s, ll b_ = 10007) { set(s, b_); }

    void set(const string &s, ll b_ = 10007) {
        n = s.size();
        b = b_;
        calc(s.c_str());
    }

    ll get() const { return get(n); }

    ll get(int i) const { return hash[i]; }

    ll get(int i, int j) const {
        return (get(j) - get(i) * pow[j - i] % MOD + MOD) % MOD;
    }

    void reserve(int sz) {
        pow.reserve(sz + 1);
        hash.reserve(sz + 1);
    }

private:
    void calc(const char *s) {
        pow.resize(n + 1); pow[0] = 1;
        hash.resize(n + 1); hash[0] = 0;
        for (int i = 0; i < n; ++i) {
            pow[i + 1] = pow[i] * b % MOD;
            hash[i + 1] = (s[i] + hash[i] * b) % MOD;
        }
    }
};

// 1000000007, 1000000009, 1000000021, ...
using RH1 = rolloing_hash<1000000007>;
using RH2 = rolloing_hash<1000000021>;


using hash_t = pair<ll, ll>;

string s;
string x[200010], y[200010];
hash_t hx[200010], hy[200010];
map<hash_t, int> shx, shy;
set<int> sx, sy;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    cin >> s;
    int n;
    cin >> n;
    RH1 rh1;
    RH2 rh2;
    rh1.reserve(200010);
    rh2.reserve(200010);

    for (int i = 0; i < n; i++) {
        cin >> x[i] >> y[i];

        rh1.set(x[i]);
        rh2.set(x[i]);
        hash_t h = hash_t(rh1.get(x[i].size()), rh2.get(x[i].size()));
        hx[i] = h;
        shx[h] = -1;
        sx.insert(x[i].size());

        rh1.set(y[i]);
        rh2.set(y[i]);
        h = hash_t(rh1.get(y[i].size()), rh2.get(y[i].size()));
        hy[i] = h;
        shy[h] = -1;
        sy.insert(y[i].size());
    }
    
    rh1.set(s);
    rh2.set(s);
    for (auto &e : sx) {
        for (int i = 0; i + e <= (int)s.size(); ++i) {
            hash_t h(rh1.get(i, i + e), rh2.get(i, i + e));
            auto it = shx.find(h);
            if (it != shx.end() && it->second == -1) it->second = i;
        }
    }
    for (auto &e : sy) {
        for (int i = (int)s.size() - e; i >= 0; --i) {
            hash_t h(rh1.get(i, i + e), rh2.get(i, i + e));
            auto it = shy.find(h);
            if (it != shy.end() && it->second == -1) it->second = i;
        }
    }

    for (int i = 0; i < n; i++) {
        int l = shx[hx[i]];
        int r = shy[hy[i]];
        if (l != -1 && r != -1 && l <= r && l + x[i].size() <= r + y[i].size()) {
            cout << r - l + (int)y[i].size();
        } else {
            cout << 0;
        }
        cout << '\n';
    }
}

Measured memory_bytes: 24852