#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

#define MAX_N (1<<18)
struct SegTree {
  int seg[MAX_N*2-1];
  SegTree() {
    for (int i=0; i<MAX_N*2-1; i++) seg[i] = INF;
  }

  void update(int i, int x) {
    i += MAX_N-1;
    if (seg[i] <= x) return;
    seg[i] = x;
    while (i > 0) {
      i = (i-1) / 2;
      seg[i] = min(seg[i*2+1], seg[i*2+2]);
    }
  }
  int query(int a, int b, int k=0, int l=0, int r=MAX_N) {
    if (r <= a || b <= l) return INF;
    if (a <= l && r <= b) return seg[k];
    return min(
      query(a, b, k*2+1, l, (l+r)/2),
      query(a, b, k*2+2, (l+r)/2, r)
    );
  }
};

#undef MAX_N
#define MAX_N 200000
struct SuffixArray {
  int n, k;
  string S;
  int rank[MAX_N+1], tmp[MAX_N+1];
  int sa[MAX_N+1];
  SegTree seg;

  bool compare_sa(int i, int j) {
    if (rank[i] != rank[j]) return rank[i] < rank[j];
    else {
      int ri = i+k<=n ? rank[i+k] : -1;
      int rj = j+k<=n ? rank[j+k] : -1;
      return ri < rj;
    }
  }

  SuffixArray(string s) : S(s) {
    n = S.length();
    for (int i=0; i<=n; i++) {
      sa[i] = i;
      rank[i] = i<n ? S[i] : -1;
    }
    for (k=1; k<=n; k*=2) {
      sort(sa, sa+n+1, [this] (int a, int b) {
        return compare_sa(a, b);
      });
      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++) seg.update(i, sa[i]);
  }

  bool contains(string T) {
    int lo = 0, hi = S.length();
    while (hi - lo > 1) {
      int mid = (lo + hi) / 2;
      if (S.compare(sa[mid], T.length(), T) < 0) lo = mid;
      else hi = mid;
    }
    return S.compare(sa[hi], T.length(), T) == 0;
  }

  int leftmost(string T) {
    if (!contains(T)) return -1;
    int lo = 0, hi = S.length();
    while (hi - lo > 1) {
      int mid = (lo + hi) / 2;
      if (S.compare(sa[mid], T.length(), T) < 0) lo = mid;
      else hi = mid;
    }
    int left = hi;
    lo = left;
    hi = S.length()+1;
    while (hi - lo > 1) {
      int mid = (lo + hi) / 2;
      if (S.compare(sa[mid], T.length(), T) == 0) lo = mid;
      else hi = mid;
    }
    int right = hi;
    return seg.query(left, right);
  }
};

int Q;
string S;

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  cin >> S >> Q;
  string rev(S);
  reverse(all(rev));

  SuffixArray sa(S), sb(rev);
  rep(i, Q) {
    string x, y;
    cin >> x >> y;
    reverse(all(y));
    int l = sa.leftmost(x), r = sb.leftmost(y);
    if (l == -1 || r == -1) {
      cout << 0 << "\n";
      continue;
    }
    r = S.length()-1-r;
    if (l > r || l+(int)x.length()-1 > r || r-(int)y.length()+1 < l) cout << 0 << "\n";
    else {
      int s = r - l + 1;
      cout << s << "\n";
    }
  }
  return 0;
}

Measured memory_bytes: 12988