#include <bits/stdc++.h>

using namespace std;

#define int long long
#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for(auto& a : b)

typedef pair<int, int> P;

const int inf = 1LL << 55;

// Suffix Array
struct SuffixArray
{
  int n, k;
  vector<int> sa, rank, temp;
  string s;
  SuffixArray(const string& str)
  {
    s = str;
    n = (int)str.size();
    sa.resize(n + 1);
    rank.resize(n + 1);
    temp.resize(n + 1);

    // initialize for one character's rank
    for(int i = 0; i <= n; i++) {
      sa[i] = i;
      rank[i] = i < n ? str[i] : -1; // including null
    }

    // build Suffix Array
    for(k = 1; k <= n; k *= 2) {

      const auto comp = [&](int i, int j) -> bool {
	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;
	}      
      };
      
      sort(sa.begin(), sa.end(), comp);

      temp[sa[0]] = 0;
      for(int i = 1; i <= n; i++) {
	temp[sa[i]] = temp[sa[i-1]] + (comp(sa[i-1], sa[i]));
      }

      for(int i = 0; i <= n; i++) {
	rank[i] = temp[i];
      }
    }
  }
  bool contain(const string& t)
  {
    int l = 0, r = n;
    while(r - l > 1) {
      int m = (l + r) / 2;
      if(s.compare(sa[m], t.size(), t) < 0) l = m;
      else r = m;
    }
    return s.compare(sa[r], t.size(), t) == 0;
  }
  int find(const string& t)
  {
    int l = 0, r = n;
    while(r - l > 1) {
      int m = (l + r) / 2;
      if(s.compare(sa[m], t.size(), t) < 0) l = m;
      else r = m;
    }
    return s.compare(sa[r], t.size(), t) == 0 ? r : -1;
  }
  int lower_bound(const string& t)
  {
    int l = 0, r = n;
    while(r - l > 1) {
      int m = (l + r) / 2;
      if(s.compare(sa[m], t.size(), t) < 0) l = m;
      else r = m;
    }
    return r;
  }
  int upper_bound(const string& t)
  {
    int l = 0, r = n + 1;
    while(r - l > 1) {
      int m = (l + r) / 2;
      if(s.compare(sa[m], t.size(), t) <= 0) l = m;
      else r = m;
    }
    return r;
  }
  int operator[](int k) const
  {
    return sa[k];
  }
};

struct SegmentTree
{
  vector<int> data;
  int sz;
  SegmentTree(int n)
  {
    sz = 1;
    while(sz < n) sz *= 2;
    data.resize(2*sz-1, inf);
  }
  void update(int k, int x)
  {
    k += sz - 1;
    data[k] = x;
    while(k > 0) {
      k = (k - 1) / 2;
      data[k] = min(data[2*k+1], data[2*k+2]);
    }
  }
  int query(int a, int b, int k, int l, int r)
  {
    if(r <= a || b <= l) return inf;
    if(a <= l && r <= b) return data[k];
    return min(query(a, b, 2*k+1, l, (l+r)/2),
	       query(a, b, 2*k+2, (l+r)/2, r));
  }
  int query(int a, int b)
  {
    return query(a, b, 0, 0, sz);
  }
  int operator[](int k) const
  {
    return data[k+sz-1];
  }
};

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  string S, T;
  int m, n;
  cin >> S >> m;
  n = S.size();
  T = S; reverse(all(T));
  SuffixArray saX(S), saY(T);
  SegmentTree sgX(n+1), sgY(n+1);
  for(int i = 0; i <= n; i++) {
    sgX.update(i, saX[i]);
    sgY.update(i, saY[i]);
  }

  while(m--) {
    string x, y;
    cin >> x >> y; reverse(all(y));
    
    int xidx = saX.find(x);
    if(xidx == -1) {
      cout << 0 << endl;
      continue;
    }
    int xup = saX.upper_bound(x);

    int yidx = saY.find(y);
    if(yidx == -1) {
      cout << 0 << endl;
      continue;
    }
    int yup = saY.upper_bound(y);

    int xhead = sgX.query(xidx, xup);
    int ytail = n - sgY.query(yidx, yup);
    
    if(xhead + x.size() > ytail || xhead > ytail - y.size()) cout << 0 << endl;
    else cout << ytail - xhead << endl;
  }
  return 0;
}

Measured memory_bytes: 21592