1037_H. Security

Some programming website is establishing a secure communication protocol. For security reasons, they want to choose several more or less random strings.

Initially, they have a string s consisting of lowercase English letters. Now they want to choose q strings using the following steps, and you are to help them.

  1. A string x consisting of lowercase English letters and integers l and r (1 ≤ l ≤ r ≤ |s|) are chosen. 
  2. Consider all non-empty distinct substrings of the s_l s_{l + 1} … s_r, that is all distinct strings s_i s_{i+1} … s_{j} where l ≤ i ≤ j ≤ r. Among all of them choose all strings that are lexicographically greater than x. 
  3. If there are no such strings, you should print -1. Otherwise print the lexicographically smallest among them. 



String a is lexicographically less than string b, if either a is a prefix of b and a ≠ b, or there exists such a position i (1 ≤ i ≤ min(|a|, |b|)), such that a_i < b_i and for all j (1 ≤ j < i) a_j = b_j. Here |a| denotes the length of the string a.

Input

The first line of input contains a non-empty string s (1 ≤ |s| ≤ 10^{5}) consisting of lowercase English letters.

The second line contains an integer q (1 ≤ q ≤ 2 ⋅ 10^5) — the number of strings to select.

Each of the next q lines contains two integers l, r (1 ≤ l ≤ r ≤ |s|) and a non-empty string x consisting of lowercase English letters. The total length of strings x for all queries does not exceed 2 ⋅ 10^{5}.

Output

Output q lines, each of them should contain the desired string or -1, if there is no such string.

Examples

Input

baa
5
1 2 ba
2 3 a
1 2 b
2 3 aa
1 3 b


Output

-1
aa
ba
-1
ba


Input

bacb
4
1 2 ba
2 3 ac
1 3 ac
3 4 c


Output

-1
c
b
cb


Input

bba
1
1 1 b


Output

-1

Note

Consider the first example.

The string s is "baa". The queries are as follows.

  1. We consider the substring s_1 s_2 that is "ba". It has substrings "b", "a" and "ba", since none of them is greater than the query string "ba", the answer is -1.
  2. We consider substring "aa". Among its substrings only "aa" is greater than the query string "a". So the answer is "aa".
  3. We consider substring "ba". Out of "b", "a" and "ba" only "ba" is greater than the query string "b", so the answer is "ba".
  4. We consider substring "aa". No substring of "aa" is greater than the query string "aa" so the answer is -1.
  5. We consider substring "baa" and it has (among others) substrings "ba", "baa" which are greater than the query string "b". Since "ba" is lexicographically smaller than "baa", the answer is "ba". 

Example input:
baa
5
1 2 ba
2 3 a
1 2 b
2 3 aa
1 3 b

bacb
4
1 2 ba
2 3 ac
1 3 ac
3 4 c

bba
1
1 1 b


Example output:
-1
aa
ba
-1
ba

-1
c
b
cb

-1


Tags: data structures
string suffix structures
Difficulty rating: 3200

Solution:
#include <bits/stdc++.h>
using namespace std;
enum { N = 400007, M = 8000000 };
char A[N], B[N];
int n, m, q, o, t, c, T[N][26], L[N], P[N], ls[M], rs[M], S[M], rt[N], hd[N],
    nt[N];
void upd(int &u, int x, int L = 1, int R = n) {
  u = ++c, S[u] = 1;
  if (L == R) return;
  int m = (L + R) >> 1;
  if (x <= m)
    upd(ls[u], x, L, m);
  else
    upd(rs[u], x, m + 1, R);
}
int qry(int u, int l, int r, int L = 1, int R = n) {
  if (!u || (l == L && r == R)) return S[u];
  int m = (L + R) >> 1;
  if (r <= m) return qry(ls[u], l, r, L, m);
  if (l > m) return qry(rs[u], l, r, m + 1, R);
  return qry(ls[u], l, m, L, m) || qry(rs[u], m + 1, r, m + 1, R);
}
int mrg(int x, int y) {
  if (!x || !y) return x | y;
  int u = ++c;
  S[u] = S[x] + S[y];
  ls[u] = mrg(ls[x], ls[y]);
  rs[u] = mrg(rs[x], rs[y]);
  return u;
}
void add(int c, int i) {
  int u = ++t;
  L[u] = L[o] + 1, upd(rt[u], i);
  for (; o && !T[o][c]; o = P[o]) T[o][c] = u;
  if (!o)
    P[u] = 1;
  else {
    int p = T[o][c];
    if (L[o] + 1 == L[p])
      P[u] = p;
    else {
      int q = ++t;
      P[q] = P[p], L[q] = L[o] + 1;
      memcpy(T[q], T[p], sizeof(T[q]));
      P[u] = P[p] = q;
      for (; o && T[o][c] == p; o = P[o]) T[o][c] = q;
    }
  }
  o = u;
}
void fly(int u) {
  for (int v = hd[u]; v; v = nt[v]) {
    fly(v);
    rt[u] = mrg(rt[u], rt[v]);
  }
}
int main() {
  scanf("%s%d", A + 1, &q), n = strlen(A + 1);
  o = t = 1;
  for (int i = 1; i <= n; ++i) add(A[i] - 'a', i);
  for (int i = 2; i <= t; ++i) nt[i] = hd[P[i]], hd[P[i]] = i;
  fly(1);
  while (q--) {
    int l, r;
    scanf("%d%d%s", &l, &r, B + 1), m = strlen(B + 1);
    B[m + 1] = 'a' - 1;
    int u = 1, a = -1;
    char b;
    for (int i = 1; i <= m + 1; ++i) {
      if (l + i - 1 > r) break;
      for (int j = B[i] - 'a' + 1; j <= 25; ++j) {
        int v = T[u][j];
        if (v && qry(rt[v], l + i - 1, r)) {
          a = i, b = j;
          break;
        }
      }
      if (i > m) break;
      u = T[u][B[i] - 'a'];
      if (!u) break;
    }
    if (a < 0)
      puts("-1");
    else {
      for (int i = 1; i <= a - 1; ++i) putchar(B[i]);
      putchar(b + 'a'), putchar('\n');
    }
  }
  return 0;
}