#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}

class SA_IS {
private:
  std::string str;
  std::vector<int> sa; 
  bool built = false;

  static inline bool is_lms(const std::vector<int>& is_l, size_t idx) {
    return idx > 0 && is_l[idx-1] && !is_l[idx];
  }

  void create_bin(const std::vector<int>& v, std::vector<int>& bin) {
    std::fill(bin.begin(), bin.end(), 0);
    for (auto& x : v) bin[x+1]++;
    for (int i = 1; i < bin.size(); i++) bin[i] += bin[i-1];
  }

  void set_lms(const std::vector<int>& v, std::vector<int>& sa, std::vector<int>& bin, const std::vector<int>& is_l) {
    for (int i = 0; i < v.size(); i++) {
      if (is_lms(is_l, i)) { sa[--bin[v[i]+1]] = i;}
    }
    for (int i = 0; i < v.size(); i++) {
      if (is_lms(is_l, i)) { bin[v[i]+1]++; }
    }
  }

  void induced_sort(const std::vector<int>& v, std::vector<int>& sa, size_t kind_num, std::vector<int>& bin, const std::vector<int>& is_l) {
    std::vector<int> cnt(kind_num+2, 0);
    for (int i = 0; i < v.size(); i++) {
      if (sa[i] <= 0 || !is_l[sa[i]-1]) continue;
      int kind = v[sa[i]-1];
      sa[bin[kind] + cnt[kind]++] = sa[i]-1;
    }
  }

  void reverse_induced_sort(const std::vector<int>& v, std::vector<int>& sa, std::vector<int>& bin, const std::vector<int>& is_l) {
    for (int i = v.size()-1; i > 0; i--) {
      if (sa[i] <= 0 || is_l[sa[i]-1]) continue;
      int kind = v[sa[i]-1];
      sa[--bin[kind+1]] = sa[i]-1;
    }
  }
public:
  SA_IS() : built(false) {}
  SA_IS(const std::string &str_) : str(str_) { build(); }
  SA_IS(std::string &&str_) : str(str_) { build(); }

  std::vector<int> build(const std::vector<int>& v, int kinds) {
    if (v.size() == 1) return std::vector<int>(1, 0);
    std::vector<int> bin(kinds+2, 0), is_l(v.size(), 0);
    std::vector<int> sa(v.size(), -1);

    for (int i = v.size()-2; i >= 0; i--) {
      is_l[i] = v[i] == v[i+1] ? is_l[i+1] : v[i] > v[i+1];
    }

    create_bin(v, bin);
    set_lms(v, sa, bin, is_l);
    induced_sort(v, sa, kinds, bin, is_l);
    reverse_induced_sort(v, sa, bin, is_l);

    std::vector<int> order(v.size());
    int cur = 0, prev = -1;
    for (int i = 0; i < v.size(); i++) if (is_lms(is_l, i)) order[i] = cur++;

    std::vector<int> next_v(cur);
    cur = -1;
    for (int i = 0; i < v.size(); i++) {
      if (!is_lms(is_l, sa[i])) continue;
      bool diff = false;
      for (int d = 0; d < v.size(); d++) {
        if (prev == -1 || v[sa[i]+d] != v[prev+d] || is_l[sa[i]+d] != is_l[prev+d]) {
          diff = true;
          break;
        }
        if (d > 0 && is_lms(is_l, sa[i]+d)) break;
      }
      if (diff) { cur++; prev = sa[i]; }
      next_v[order[sa[i]]] = cur;
    }

    std::vector<int> re_order(next_v.size());
    std::vector<int> next_sa = build(next_v, cur);
    std::fill(sa.begin(), sa.end(), -1);
    for (int i = 0; i < v.size(); i++) if (is_lms(is_l, i)) re_order[order[i]] = i;
    create_bin(v, bin);
    for (int i = next_sa.size()-1; i >= 0; i--) sa[--bin[v[re_order[next_sa[i]]]+1]] = re_order[next_sa[i]];
    create_bin(v, bin);
    induced_sort(v, sa, kinds, bin, is_l);
    reverse_induced_sort(v, sa, bin, is_l);

    return sa;
  }

  void build() {
    str += '$';
    std::vector<int> v(str.size());
    for (int i = 0; i < str.size(); i++) v[i] = str[i];
    sa = build(v, *std::max_element(v.begin(), v.end()));
    sa = std::vector<int>(sa.begin()+1, sa.end());
    built = true;
    str = str.substr(0, str.size()-1);
  }

  const std::vector<int>& get_sa() {
    assert(built);
    return sa;
  }

  bool is_upper(const std::string& t, int s_idx) { // t > sa[si]
    int idx = 0; s_idx = sa[s_idx];
    while (s_idx+idx < sa.size() && idx < t.size()) {
      if (t[idx] > str[s_idx+idx]) return true;
      if (t[idx] < str[s_idx+idx]) return false;
      idx++;
    }
    return (idx < t.size() && s_idx + idx >= str.size());
  }

  int lower_bound(const std::string& t) {
    int l = -1, r = sa.size(), m;
    while (r - l > 1) {
      m = (l + r) >> 1;
      if (is_upper(t, m)) l = m;
      else r = m;
    }
    return r;
  }

  ::std::pair<int, int> bounds(std::string& t) {
    int l = lower_bound(t); t.back()++;
    int r = lower_bound(t); t.back()--;
    return ::std::pair<int, int>(l, r);
  }

  const int& operator[](const size_t& k) {
    return sa[k];
  }

  const size_t size() { return str.size()-1; }
};

template<class ValueMonoid, template<class...> class Container=::std::vector>
class SegTree{
public:
	using value_structure = ValueMonoid;
	using value_type = typename value_structure::value_type;
	using const_reference = const value_type &;
	using container_type = Container<value_type>;
	using size_type = typename container_type::size_type;

private:
	::std::vector<value_type> tree;
	size_type size_;

	static size_type getsize(const size_type x){
		size_type ret = 1;
		while(ret < x)
			ret <<= 1;
		return ret;
	}

	inline value_type calc(const value_type a, const value_type b){
		return value_structure::operation(a, b);
	}

	inline void calc_node(const size_type index){
		if(tree.size() <= (index << 1 | 1)) return;
		tree[index] = value_structure::operation(tree[index<<1], tree[index<<1 | 1]);
	}
public:
	SegTree() : size_(0), tree(){}
	SegTree(const size_type size)
			: size_(size), tree(size << 1, value_structure::identity()){}
  SegTree(const std::vector<value_type>& v) {
    *this = SegTree(v.begin(), v.end());
  }
	template<class InputIterator>
	SegTree(InputIterator first, InputIterator last)
			: size_(::std::distance(first, last)){
		tree = container_type(size_, value_structure::identity());
		tree.insert(tree.end(), first, last);
		for(size_type i = size_;i > 0;i--){
			calc_node(i);
		}
	}

	size_type size() const { return size_; }
	const_reference operator[](const size_type k) const {
		assert(k < size_);
		return tree[k+size_];
	}

	value_type query(size_type l, size_type r){
		assert(l <= r);
		assert(0 <= l && l < size_);
		assert(0 <= r && r <= size_);
		value_type retl = value_structure::identity(), retr = value_structure::identity();
		for(l += size_, r += size_; l < r ; l >>= 1, r >>= 1){
			if(l&1) retl = calc(retl, tree[l++]);
			if(r&1) retr = calc(tree[--r], retr);
		}
		return calc(retl, retr);
	}

	template<class F>
	void update(size_type index, const F& f){
		assert(0 <= index && index < size());
		index += size_;
		tree[index] = f(::std::move(tree[index]));
		while(index >>= 1)
			calc_node(index);
	}

	/*
	template<class F>
	size_type search(const F& f) const { // [0, result) is True and [0, result-1) is not.
		if(f(value_structure::identity()))
			return 0;
		if(!f(tree[1]))
			return size_+1;
		value_type acc = value_structure::identity();
		size_type i = 1;
		while(i < 
	}
	*/
};
class v_monoid {
public:
  using value_type = pair<int, int>;
  static value_type identity() { return value_type(numeric_limits<int>::max(), numeric_limits<int>::min()); }
  static value_type operation(const value_type &a, const value_type &b) {
    return value_type(min(a.fs, b.fs), max(a.sc, b.sc));
  }
};

int main(void) {
  string s;
  cin >> s;
  SA_IS sa(s);
  vector<pair<int, int>> v(s.size());
  REP(i, v.size()) v[i] = PII(sa[i], sa[i]);
  SegTree<v_monoid> ssa(v.begin(), v.end());
  int m;
  scanf("%d", &m);
  REP(_, m) {
    string x, y;
    cin >> x >> y;
    auto r1 = sa.bounds(x), r2 = sa.bounds(y);
    if (r1.first >= r1.second || r2.first >= r2.second) {
      cout << 0 << endl;
      continue;
    }
    PII l = ssa.query(r1.first, r1.second), r = ssa.query(r2.first, r2.second);
    if (r1.first == r1.second || r2.first == r2.second || r.sc+y.size() < l.fs+x.size()) {
      cout << 0 << endl;
    } else {
      cout << r.sc-l.fs + y.size() << endl;
    }
  }
}


Measured memory_bytes: 10332