Given the string S and m queries. The i-th query is given by the two strings xi and yi.

For each query, answer the longest substring of the string S, starting with xi and ending with yi.

For the string S, | S | represents the length of S. Also, the fact that the character string T is a substring of the character string S means that a certain integer i exists and Tj = Si + j is satisfied for 1 ≤ j ≤ | T |. Where Tj represents the jth character of T.

Constraints

* 1 ≤ | S | ≤ 2 x 105
* 1 ≤ m ≤ 105
* 1 ≤ | xi |, | yi |
* $ \ sum ^ m_ {i = 1} $ (| xi | + | yi |) ≤ 2 x 105
* S and xi, yi consist only of half-width lowercase letters.

Input

The input is given in the following format.


S
m
x1 y1
x2 y2
::
xm ym


* The character string S is given on the first line.
* The number of queries m is given in the second line.
* Of the m lines from the 3rd line, the i-th query string xi, yi is given on the i-th line, separated by spaces.

Output

Answer the maximum substring length in the following format.


len1
len2
::
lenm


Output the longest substring length leni that satisfies the condition for the i-th query on the i-th line of the m lines from the first line. If there is no such substring, output 0.

Examples

Input

abracadabra
5
ab a
a a
b c
ac ca
z z


Output

11
11
4
3
0


Input

howistheprogress
4
ist prog
s ss
how is
the progress


Output

9
12
5
11


Input

icpcsummertraining
9
mm m
icpc summer
train ing
summer mm
i c
i i
g g
train i
summer er


Output

2
10
8
0
4
16
1
6
6
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &t) {
  os << "[";
  for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
    if (it != t.begin()) os << ",";
    os << *it;
  }
  os << "]";
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &t) {
  os << "{";
  for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) {
    if (it != t.begin()) os << ",";
    os << *it;
  }
  os << "}";
  return os;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &t) {
  return os << "(" << t.first << "," << t.second << ")";
}
template <class S, class T>
pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first + t.first, s.second + t.second);
}
template <class S, class T>
pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) {
  return pair<S, T>(s.first - t.first, s.second - t.second);
}
const int INF = 1 << 28;
const double EPS = 1e-8;
const int MOD = 1000000007;
struct SuffixArray {
  template <bool End, class T>
  vector<int> compute_buckets(const T *s, int n, int k) const {
    vector<int> buckets(k);
    for (int i = 0; i < (int)(n); i++) ++buckets[s[i]];
    int sum = 0;
    for (int i = 0; i < (int)(k); i++) {
      sum += buckets[i];
      buckets[i] = (End ? sum : (sum - buckets[i]));
    }
    return buckets;
  }
  template <class T>
  void induce_sa_l(int *sa, const T *s, int n, int k,
                   const vector<int> &ls) const {
    vector<int> buckets(compute_buckets<false>(s, n, k));
    for (int i = 0; i < (int)(n); i++) {
      const int j = sa[i] - 1;
      if (j >= 0 && !ls[j]) {
        sa[buckets[s[j]]++] = j;
      }
    }
  }
  template <class T>
  void induce_sa_s(int *sa, const T *s, int n, int k,
                   const vector<int> &ls) const {
    vector<int> buckets(compute_buckets<true>(s, n, k));
    for (int i = ((int)(n)-1); i >= 0; i--) {
      const int j = sa[i] - 1;
      if (j >= 0 && ls[j]) {
        sa[--buckets[s[j]]] = j;
      }
    }
  }
  bool is_lms(const vector<int> &ls, int i) const {
    return i > 0 && !ls[i - 1] && ls[i];
  }
  template <class T>
  void sa_is(int *sa, const T *s, int n) const {
    vector<int> ls(n);
    ls[n - 1] = 1;
    for (int i = ((int)(n - 1) - 1); i >= 0; i--)
      ls[i] = (s[i] != s[i + 1]) ? s[i] < s[i + 1] : ls[i + 1];
    const int k = (int)*std::max_element(s, s + n) + 1;
    vector<int> buckets = compute_buckets<true>(s, n, k);
    for (int i = 0; i < (int)(n); i++) sa[i] = -1;
    for (int i = 1; i <= (int)(n - 1); i++)
      if (is_lms(ls, i)) sa[--buckets[s[i]]] = i;
    induce_sa_l(sa, s, n, k, ls);
    induce_sa_s(sa, s, n, k, ls);
    int m = 0;
    for (int i = 0; i < (int)(n); i++)
      if (is_lms(ls, sa[i])) sa[m++] = sa[i];
    for (int i = m; i < n; i++) sa[i] = -1;
    int num = 0, prev = -1;
    for (int i = 0; i < (int)(m); i++) {
      int p = sa[i], diff = 0;
      for (int d = 0; d < (int)(n); d++) {
        const int l = p + d, r = prev + d;
        if (prev < 0 || s[l] != s[r] || ls[l] != ls[r]) {
          diff = 1;
          break;
        } else if (d > 0 && (is_lms(ls, l) || is_lms(ls, r))) {
          break;
        }
      }
      if (diff) {
        num++;
        prev = p;
      }
      p = (p - (p & 1)) / 2;
      sa[m + p] = num - 1;
    }
    for (int i = n - 1, j = n - 1; i >= m; i--)
      if (sa[i] >= 0) sa[j--] = sa[i];
    int *reduced_s = sa + n - m;
    if (num < m)
      sa_is(sa, reduced_s, m);
    else
      for (int i = 0; i < (int)(m); i++) sa[reduced_s[i]] = i;
    buckets = compute_buckets<true>(s, n, k);
    int j = 0;
    for (int i = 1; i <= (int)(n - 1); i++)
      if (is_lms(ls, i)) reduced_s[j++] = i;
    for (int i = 0; i < (int)(m); i++) sa[i] = reduced_s[sa[i]];
    for (int i = m; i < n; i++) sa[i] = -1;
    for (int i = ((int)(m)-1); i >= 0; i--) {
      const int j = sa[i];
      sa[i] = -1;
      sa[--buckets[s[j]]] = j;
    }
    induce_sa_l(sa, s, n, k, ls);
    induce_sa_s(sa, s, n, k, ls);
  }
  vector<int> sa;
  template <class T>
  vector<int> sa_is(const T &s) const {
    if (s.size() == 0) {
      return vector<int>(1);
    }
    const int n = s.size() + 1;
    vector<int> vs(n), sa(n);
    ;
    for (int i = 0; i < (int)(n - 1); i++) {
      vs[i] = s[i];
    }
    sa_is(&sa[0], &vs[0], n);
    return sa;
  }
  template <class T>
  explicit SuffixArray(const T &s) : sa(sa_is(s)) {}
  size_t size() const { return sa.size(); }
  int operator[](int i) const { return sa[i]; }
};
struct handler1 {
  handler1() {}
  int def_val() { return INF; }
  static int merge(const int &l, const int &r) { return min(l, r); }
};
struct handler2 {
  handler2() {}
  int def_val() { return INF; }
  static int merge(const int &l, const int &r) { return max(l, r); }
};
template <class Handler>
class NazoTable {
  int N, K;
  vector<vector<typename Handler::val_t> > d;
  Handler hdl;

 public:
  NazoTable(vector<typename Handler::val_t> v) : hdl() {
    N = v.size();
    K = 1;
    while ((1 << K) < v.size()) K++;
    v.resize(1 << K, hdl.def_val());
    d = vector<vector<typename Handler::val_t> >(
        K, vector<typename Handler::val_t>(1 << K));
    for (int k = 0; k < K; k++) {
      for (int i = 0; i < 1 << (K - k - 1); i++) {
        int center = (i << (k + 1)) | (1 << k);
        d[k][center - 1] = v[center - 1];
        d[k][center] = v[center];
        for (int j = 2; j <= (1 << k); j++)
          if (center < N)
            d[k][center - j] = hdl.merge(v[center - j], d[k][center - j + 1]);
        for (int j = 1; j < (1 << k); j++)
          if (center + j < N)
            d[k][center + j] = hdl.merge(d[k][center + j - 1], v[center + j]);
      }
    }
  }
  int query(int l, int r) {
    if (r < l) return hdl.def_val();
    if (l ^ r) {
      int k = 31 - __builtin_clz(l ^ r);
      return hdl.merge(d[k][l], d[k][r]);
    }
    return d[0][l];
  }
};
int n;
string s;
pair<int, int> getRange(SuffixArray &sa, string &a) {
  auto comp = [&](int med) {
    int p = sa[med], q = 0;
    while (1) {
      if (a.size() <= q) return 0;
      if (s.size() <= p) return 1;
      if (s[p] != a[q]) return s[p] < a[q] ? 1 : -1;
      p++;
      q++;
    }
  };
  pair<int, int> res;
  int l = 0, r = sa.size();
  while (l + 1 < r) {
    int med = (l + r) / 2;
    if (comp(med) > 0)
      l = med;
    else
      r = med;
  }
  res.first = r;
  l = 0, r = sa.size();
  while (l + 1 < r) {
    int med = (l + r) / 2;
    if (comp(med) >= 0)
      l = med;
    else
      r = med;
  }
  res.second = r;
  return res;
}
int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin >> s >> n;
  SuffixArray sa(s);
  vector<int> d;
  for (int i = 0; i < (int)(sa.size()); i++) d.push_back(sa[i]);
  NazoTable<handler1> rminq(d);
  NazoTable<handler2> rmaxq(d);
  for (int i = 0; i < (int)(n); i++) {
    string a, b;
    cin >> a >> b;
    int l1, r1, l2, r2;
    tie(l1, r1) = getRange(sa, a);
    tie(l2, r2) = getRange(sa, b);
    int res = rmaxq.query(l2, r2 - 1) - rminq.query(l1, r1 - 1) + b.size();
    if (sa.size() == l1 || sa.size() == l2 || res < max(a.size(), b.size()))
      res = 0;
    printf("%d\n", res);
  }
  return 0;
}
