Problem: 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

Solution (in CPP):

Solution:
#include <bits/stdc++.h>

using namespace std;

struct SuffixArray
{
  vector< int > SA, LCP;
  string s;

  SuffixArray()
  {

  }

  SuffixArray(const string& str)
  {
    Build_SA(str);
  }

  void Build_SA(const string& str)
  {
    s = str;
    SA.resize(s.size());
    iota(begin(SA), end(SA), 0);
    sort(begin(SA), end(SA), [&](const int& a, const int& b)
    {
      if(s[a] == s[b]) return (a > b);
      return (s[a] < s[b]);
    });
    vector< int > classes(s.size()), c(s.size()), cnt(s.size());
    for(int i = 0; i < s.size(); i++) {
      c[i] = s[i];
    }
    for(int len = 1; len < s.size(); len <<= 1) {
      for(int i = 0; i < s.size(); i++) {
        if(i > 0 && c[SA[i - 1]] == c[SA[i]] && SA[i - 1] + len < s.size() && c[SA[i - 1] + len / 2] == c[SA[i] + len / 2]) {
          classes[SA[i]] = classes[SA[i - 1]];
        } else {
          classes[SA[i]] = i;
        }
      }
      iota(begin(cnt), end(cnt), 0);
      copy(begin(SA), end(SA), begin(c));
      for(int i = 0; i < s.size(); i++) {
        int s1 = c[i] - len;
        if(s1 >= 0) SA[cnt[classes[s1]]++] = s1;
      }
      classes.swap(c);
    }
  }

  void Build_LCP()
  {
    vector< int > rank(s.size());
    for(int i = 0; i < s.size(); i++) {
      rank[SA[i]] = i;
    }
    LCP.resize(s.size() - 1);
    for(int i = 0, h = 0; i < s.size(); i++) {
      if(rank[i] + 1 < s.size()) {
        for(int j = SA[rank[i] + 1]; max(i, j) + h < s.length() && s[i + h] == s[j + h]; ++h);
        LCP[rank[i]] = h;
        if(h > 0) --h;
      }
    }
  }

  int operator[](int k) const
  {
    return (SA[k]);
  }

  int size() const
  {
    return (s.size());
  }

  bool lt_substr(string& t, int si = 0, int ti = 0)
  {
    int sn = s.size(), tn = t.size();
    while(si < sn && ti < tn) {
      if(s[si] < t[ti]) return (true);
      if(s[si] > t[ti]) return (false);
      ++si, ++ti;
    }
    return (si >= sn && ti < tn);
  }

  int lower_bound(string& t)
  {
    int low = -1, high = SA.size();
    while(high - low > 1) {
      int mid = (low + high) >> 1;
      if(lt_substr(t, SA[mid])) low = mid;
      else high = mid;
    }
    return (high);
  }

  pair< int, int > lower_upper_bound(string& t)
  {
    int idx = lower_bound(t);
    int low = idx - 1, high = SA.size();
    t.back()++;
    while(high - low > 1) {
      int mid = (low + high) >> 1;
      if(lt_substr(t, SA[mid])) low = mid;
      else high = mid;
    }
    t.back()--;
    return (make_pair(idx, high));
  };
};

struct SegmentTree
{
  const int INF = 1 << 30;

  int sz;
  vector< int > small, big;
  int unit;

  SegmentTree(int n)
  {
    sz = 1;
    while(sz < n) sz <<= 1;
    small.assign(2 * sz - 1, INF);
    big.assign(2 * sz - 1, -INF);
  }

  void set(int k, int x)
  {
    small[k + sz - 1] = big[k + sz - 1] = x;
  }

  void build()
  {
    for(int k = sz - 2; k >= 0; k--) {
      small[k] = min(small[2 * k + 1], small[2 * k + 2]);
      big[k] = max(big[2 * k + 1], big[2 * k + 2]);
    }
  }

  int rangemaximumquery(int a, int b, int k, int l, int r)
  {
    if(a >= r || b <= l) return (-INF);
    if(a <= l && r <= b) return (big[k]);
    return (max(rangemaximumquery(a, b, 2 * k + 1, l, (l + r) >> 1),
                rangemaximumquery(a, b, 2 * k + 2, (l + r) >> 1, r)));
  }

  int rangemaximumquery(int a, int b)
  {
    return (rangemaximumquery(a, b, 0, 0, sz));
  }

  int rangeminimumquery(int a, int b, int k, int l, int r)
  {
    if(a >= r || b <= l) return (INF);
    if(a <= l && r <= b) return (small[k]);
    return (min(rangeminimumquery(a, b, 2 * k + 1, l, (l + r) >> 1),
                rangeminimumquery(a, b, 2 * k + 2, (l + r) >> 1, r)));
  }

  int rangeminimumquery(int a, int b)
  {
    return (rangeminimumquery(a, b, 0, 0, sz));
  }

};

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S;
  cin >> S;
  SuffixArray sa(S);
  SegmentTree tree(S.size());
  for(int i = 0; i < sa.size(); i++) tree.set(i, sa[i]);
  tree.build();

  int M;
  cin >> M;
  while(M--) {
    string X, Y;
    cin >> X >> Y;

    auto a = sa.lower_upper_bound(X);
    auto b = sa.lower_upper_bound(Y);
    int left = tree.rangeminimumquery(a.first, a.second);
    int right = tree.rangemaximumquery(b.first, b.second);
    if(a.first < a.second && b.first < b.second && left <= right && left + X.size() <= right + Y.size()) {
      cout << right + Y.size() - left << endl;
    } else {
      cout << 0 << endl;
    }
  }
}