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 T1, class T2>
inline bool minimize(T1& a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2>
inline bool maximize(T1& a, T2 b) {
  return a < b && (a = b, 1);
}
int const inf = 1 << 29;
int const MAX_N = 2e5 + 10;
struct suffix_array {
  int n, k;
  vector<int> sa;
  suffix_array(string const& S) {
    vector<int> rank, temp;
    n = S.size();
    rank.resize(n + 1);
    temp.resize(n + 1);
    sa.resize(n + 1);
    for (int i = 0; i <= n; i++) {
      sa[i] = i;
      rank[i] = i < n ? S[i] : -1;
    }
    for (k = 1; k <= n; k *= 2) {
      const auto compare = [&](int i, int j) {
        if (rank[i] != rank[j]) return rank[i] < rank[j];
        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(), compare);
      temp[sa[0]] = 0;
      for (int i = 1; i <= n; i++) {
        temp[sa[i]] = temp[sa[i - 1]] + (compare(sa[i - 1], sa[i]) ? 1 : 0);
      }
      for (int i = 0; i <= n; i++) {
        rank[i] = temp[i];
      }
    }
  }
  int operator[](int i) { return sa.at(i); }
  static int const npos = -1;
  int lower_bound(string const& S, string const& T) {
    int l = 0, r = n;
    while (l + 1 < r) {
      int m = (l + r) / 2;
      (S.compare(sa[m], T.size(), T) < 0 ? l : r) = m;
    }
    return S.compare(sa[r], T.size(), T) == 0 ? r : npos;
  }
  int upper_bound(string const& S, string const& T) {
    int l = 1, r = n + 1;
    while (l + 1 < r) {
      int m = (l + r) / 2;
      (S.compare(sa[m], T.size(), T) <= 0 ? l : r) = m;
    }
    return S.compare(sa[l], T.size(), T) == 0 ? r : npos;
  }
};
struct segtree {
  int N;
  vector<int> dat, sum;
  function<int(int, int)> func;
  segtree(int n, function<int(int, int)> const& f) {
    N = 1;
    while (N < n) N <<= 1;
    dat.assign(2 * N - 1, 0);
    sum.assign(2 * N - 1, 0);
    func = f;
  }
  void update(int i, int x) { add(i, i + 1, x, 0, 0, N); }
  void add(int a, int b, int x) { add(a, b, x, 0, 0, N); }
  int add(int a, int b, int x, int k, int l, int r) {
    if (b <= l || r <= a) return dat[k];
    if (a <= l && r <= b) {
      sum[k] += x;
      return dat[k] += x;
    }
    int m = (l + r) / 2;
    return dat[k] = func(add(a, b, x, 2 * k + 1, l, m),
                         add(a, b, x, 2 * k + 2, m, r)) +
                    sum[k];
  }
  int operator()(int a, int b) { return query(a, b, 0, 0, N); }
  int query(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a) return 1e9;
    if (a <= l && r <= b) return dat[k];
    int m = (l + r) / 2;
    return func(query(a, b, 2 * k + 1, l, m), query(a, b, 2 * k + 2, m, r)) +
           sum[k];
  }
};
char buf[(int)2e5 + 10];
int main() {
  scanf("%s", buf);
  string S = buf;
  string T = S;
  reverse(T.begin(), T.end());
  suffix_array sa(S);
  suffix_array rev_sa(T);
  int N = S.size();
  segtree min_query(N, [](int a, int b) { return min(a, b); });
  segtree rev_min_query(N, [](int a, int b) { return min(a, b); });
  for (int i = 0; i < N; i++) {
    min_query.update(i, sa[i]);
    rev_min_query.update(i, rev_sa[i]);
  }
  int M;
  cin >> M;
  for (int _ = 0; _ < M; _++) {
    string a, b;
    scanf("%s", buf);
    a = buf;
    scanf("%s", buf);
    b = buf;
    int a_size = a.size();
    int b_size = b.size();
    reverse(b.begin(), b.end());
    int lb = sa.lower_bound(S, a);
    if (lb == suffix_array::npos) {
      puts("0");
      continue;
    }
    int ub = sa.upper_bound(S, a);
    int rev_lb = rev_sa.lower_bound(T, b);
    if (rev_lb == suffix_array::npos) {
      puts("0");
      continue;
    }
    int rev_ub = rev_sa.upper_bound(T, b);
    int L = min_query(lb, ub);
    int R = N - rev_min_query(rev_lb, rev_ub);
    if (L + a_size <= R && L + b_size <= R) {
      printf("%d\n", R - L);
    } else {
      puts("0");
    }
  }
  return 0;
}
