#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9, PSEGLIM = 3000000, STLIM = 500500;
string alphabet = "$abcdefghijklmnopqrstuvwxyz";
vector<int> idxOf, invSA;
string str;
int N, M, curTime;
struct SuffixArray {
  string S;
  int N;
  vector<int> sa, rsa, lcp;
  void init(const string& _S) {
    S = _S;
    N = (int)S.length();
    genSA();
    genLCP();
  }
  void genSA() {
    sa.resize(N, 0);
    rsa.resize(N, 0);
    for (int i = 0; i < N; ++i) sa[i] = i;
    sort(sa.begin(), sa.end(), [&](int a, int b) { return S[a] < S[b]; });
    for (int i = 0; i < N; ++i) {
      bool same = i && S[sa[i]] == S[sa[i - 1]];
      rsa[sa[i]] = same ? rsa[sa[i - 1]] : i;
    }
    for (int len = 1; len < N; len *= 2) {
      vector<int> is(rsa), s(sa), nex(N);
      iota(nex.begin(), nex.end(), 0);
      for (int i = -1; i < N; ++i) {
        int s1 = (i == -1 ? N : s[i]) - len;
        if (s1 >= 0) sa[nex[rsa[s1]]++] = s1;
      }
      for (int i = 0; i < N; ++i) {
        bool same = i && sa[i - 1] + len < N && is[sa[i]] == is[sa[i - 1]] &&
                    is[sa[i] + len] == is[sa[i - 1] + len];
        rsa[sa[i]] = same ? rsa[sa[i - 1]] : i;
      }
    }
  }
  void genLCP() {
    lcp.resize(N - 1);
    int h = 0;
    for (int i = 0; i < N; ++i) {
      if (rsa[i]) {
        for (int j = sa[rsa[i] - 1]; j + h < N && S[i + h] == S[j + h]; h++)
          ;
        lcp[rsa[i] - 1] = h;
        if (h) h--;
      }
    }
  }
};
struct RMQ {
  vector<vector<int>> v;
  int n;
  void init(vector<int> a) {
    n = (int)a.size();
    int maxlog = 32 - __builtin_clz(n), r = 1;
    v.resize(maxlog + 1, vector<int>(n));
    for (int i = 0; i < n; ++i) v[0][i] = a[i];
    for (int e = 1; e <= maxlog; e++) {
      for (int i = 0; i < n; i++) {
        v[e][i] = min(v[e - 1][i], v[e - 1][min(n - 1, i + r)]);
      }
      r <<= 1;
    }
  }
  int query(int from, int to) {
    if (from == to) return v[0][from];
    int size = to - from + 1, e = 32 - __builtin_clz(size) - 1;
    return min(v[e][from], v[e][to - (1 << e) + 1]);
  }
};
struct PST {
  int L[PSEGLIM], R[PSEGLIM], MX[PSEGLIM], LFT[PSEGLIM], RGT[PSEGLIM];
  int cur;
  void init(int N) {
    for (int i = 0; i < PSEGLIM; ++i) {
      L[i] = -1;
      R[i] = -1;
      MX[i] = -1;
      LFT[i] = -1;
      RGT[i] = -1;
    }
    cur = 0;
    makeNode(0, N - 1);
  }
  int makeNode(int ll, int rr) {
    int res = cur++;
    L[res] = ll;
    R[res] = rr;
    MX[res] = -oo;
    if (ll != rr) {
      int md = (ll + rr) >> 1;
      LFT[res] = makeNode(ll, md);
      RGT[res] = makeNode(md + 1, rr);
    }
    return res;
  }
  int cloneNode(int id) {
    int res = cur++;
    L[res] = L[id];
    R[res] = R[id];
    MX[res] = MX[id];
    LFT[res] = LFT[id];
    RGT[res] = RGT[id];
    return res;
  }
  int pointUpdate(int id, int idx, int val) {
    int res = cloneNode(id);
    if (L[res] == R[res]) {
      MX[res] = val;
      return res;
    }
    int lf = LFT[res], rg = RGT[res];
    if (idx <= R[lf])
      LFT[res] = pointUpdate(lf, idx, val);
    else
      RGT[res] = pointUpdate(rg, idx, val);
    lf = LFT[res];
    rg = RGT[res];
    MX[res] = max(MX[lf], MX[rg]);
    return res;
  }
  int queryMax(int id, int ll, int rr) {
    if (ll <= L[id] && R[id] <= rr) return MX[id];
    if (R[id] < ll) return -oo;
    if (rr < L[id]) return -oo;
    return max(queryMax(LFT[id], ll, rr), queryMax(RGT[id], ll, rr));
  }
};
struct SuffixTree {
  int L[STLIM], R[STLIM], idxInStr[STLIM], inTime[STLIM], outTime[STLIM];
  int leaf[STLIM];
  int children[27][STLIM];
  int cur;
  void init() {
    for (int i = 0; i < STLIM; i++) {
      L[i] = -1;
      R[i] = -1;
      idxInStr[i] = -1;
      inTime[i] = -1;
      outTime[i] = -1;
      leaf[i] = -1;
    }
    for (int i = 0; i < 27; i++) {
      for (int j = 0; j < STLIM; j++) {
        children[i][j] = 0;
      }
    }
    cur = 0;
  }
  int makeNode(int ll, int rr) {
    int res = cur++;
    L[res] = ll;
    R[res] = rr;
    return res;
  }
};
SuffixArray saInit, saAll;
SuffixTree tree;
PST pstRoot;
RMQ rmqInit, rmqAll;
vector<int> startOf, roots, locs, Li, Ri;
vector<string> asks;
int curQuery, queryIdx, queryLen, qL, qR;
void buildTree(int L, int R, int haveLCP, int root) {
  if (L == R) {
    int idx = idxOf[str[saInit.sa[L] + haveLCP]];
    if (tree.children[idx][root] == 0) {
      tree.children[idx][root] = tree.makeNode(saInit.sa[L] + haveLCP, N - 1);
    }
    return;
  }
  int totLCP = rmqInit.query(L, R - 1) - haveLCP;
  if (totLCP != 0) {
    int idx = idxOf[str[saInit.sa[L] + haveLCP]];
    if (tree.children[idx][root] == 0) {
      int nL = saInit.sa[L] + haveLCP, nR = nL + totLCP - 1;
      tree.children[idx][root] = tree.makeNode(nL, nR);
    }
    buildTree(L, R, haveLCP + totLCP, tree.children[idx][root]);
    return;
  }
  int lo = L + 1, hi = R, bestIdx = L;
  for (int bs = 0; bs < 50 && lo <= hi; ++bs) {
    int mid = (lo + hi) / 2;
    int chk = rmqInit.query(L, mid - 1) - haveLCP;
    if (chk == 0)
      hi = mid - 1;
    else {
      bestIdx = mid;
      lo = mid + 1;
    }
  }
  buildTree(L, bestIdx, haveLCP, root);
  if (bestIdx + 1 <= R) {
    buildTree(bestIdx + 1, R, haveLCP, root);
  }
}
void setIdx(int now, int sum) {
  if (tree.L[now] != -1) {
    sum += tree.R[now] - tree.L[now] + 1;
  }
  tree.idxInStr[now] = N - sum;
  tree.leaf[now] = 1;
  tree.inTime[now] = curTime++;
  for (int i = 0; i < 27; ++i) {
    if (tree.children[i][now] == 0) continue;
    tree.leaf[now] = 0;
    setIdx(tree.children[i][now], sum);
  }
  if (tree.leaf[now]) {
    locs[tree.idxInStr[now]] = now;
  }
  tree.outTime[now] = curTime++;
}
bool solveQuery(int now, int matched, bool brokenOff) {
  if (tree.leaf[now]) {
    queryIdx = tree.idxInStr[now];
    queryLen = matched;
    return true;
  }
  int runs = 0;
  for (int i = 0; i < 27; ++i) {
    if (tree.children[i][now] == 0) continue;
    int to = tree.children[i][now];
    if (brokenOff) {
      int pos = qR - matched;
      if (pos >= 0) {
        int inTime = tree.inTime[to], outTime = tree.outTime[to];
        int chk = pstRoot.queryMax(roots[pos], inTime, outTime);
        if (chk >= qL) {
          bool ret = solveQuery(to, matched, brokenOff);
          runs++;
          if (ret) return ret;
          if (runs == 2) return false;
        }
      }
      continue;
    }
    int lenLeft = asks[curQuery].length() - matched;
    int lenHer = tree.R[to] - tree.L[to] + 1;
    int nmatch = matched;
    bool nBrokenOff = false;
    bool canQuery = false;
    if (lenLeft <= lenHer) {
      int i1 = invSA[startOf[curQuery] + matched];
      int i2 = invSA[tree.L[to]];
      int lcp = rmqAll.query(min(i1, i2), max(i1, i2) - 1);
      lcp = min(lcp, lenLeft);
      if (lenLeft == lcp) {
        nmatch += lenLeft;
        canQuery = true;
      } else {
        if (invSA[tree.L[to] + lcp] >
            invSA[startOf[curQuery] + matched + lcp]) {
          nmatch += lcp;
          nBrokenOff = true;
          canQuery = true;
        }
      }
    } else {
      int i1 = invSA[startOf[curQuery] + matched];
      int i2 = invSA[tree.L[to]];
      int lcp = rmqAll.query(min(i1, i2), max(i1, i2) - 1);
      lcp = min(lcp, lenHer);
      if (lcp == lenHer) {
        nmatch = matched + lcp;
        canQuery = true;
      } else {
        if (invSA[tree.L[to] + lcp] >
            invSA[startOf[curQuery] + matched + lcp]) {
          nmatch = matched + lcp;
          nBrokenOff = true;
          canQuery = true;
        }
      }
    }
    if (canQuery) {
      int pos = qR - nmatch;
      if (pos >= 0) {
        int inTime = tree.inTime[to], outTime = tree.outTime[to];
        int chk = pstRoot.queryMax(roots[pos], inTime, outTime);
        if (chk >= qL) {
          bool ret = solveQuery(to, nmatch, nBrokenOff);
          runs++;
          if (ret) return ret;
          if (runs == 2) return false;
        }
      }
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  idxOf.resize(256, 0);
  for (int i = 0; i < 27; ++i) idxOf[alphabet[i]] = i;
  cin >> str;
  str = str + "$";
  N = (int)str.length();
  curTime = 0;
  saInit.init(str);
  rmqInit.init(saInit.lcp);
  tree.init();
  int rootTree = tree.makeNode(-1, -1);
  buildTree(0, N - 1, 0, rootTree);
  locs.resize(N);
  setIdx(rootTree, 0);
  roots.resize(N);
  pstRoot.init(curTime - 1);
  roots[0] = 0;
  for (int i = 0; i < N; ++i) {
    if (i == 0) {
      roots[i] = pstRoot.pointUpdate(roots[i], tree.inTime[locs[i]], i);
    } else {
      roots[i] = pstRoot.pointUpdate(roots[i - 1], tree.inTime[locs[i]], i);
    }
  }
  int Q;
  cin >> Q;
  asks.resize(Q);
  Li.resize(Q);
  Ri.resize(Q);
  M = N;
  startOf.resize(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> Li[i] >> Ri[i];
    Li[i]--;
    Ri[i]--;
    cin >> asks[i];
    startOf[i] = M;
    M += (int)asks[i].length() + 1;
  }
  string bigStr(M, 0);
  for (int i = 0; i < N; ++i) bigStr[i] = str[i];
  int ptr = N;
  for (int i = 0; i < Q; ++i) {
    int len = (int)asks[i].length();
    for (int j = 0; j < len; j++) {
      bigStr[ptr++] = asks[i][j];
    }
    bigStr[ptr++] = '$';
  }
  saAll.init(bigStr);
  invSA.resize(M);
  for (int i = 0; i < M; ++i) {
    invSA[saAll.sa[i]] = i;
  }
  rmqAll.init(saAll.lcp);
  for (int i = 0; i < Q; ++i) {
    curQuery = i;
    queryIdx = -1;
    queryLen = -1;
    qL = Li[i];
    qR = Ri[i];
    bool ret = solveQuery(0, 0, false);
    if (!ret) {
      cout << -1 << '\n';
    } else {
      for (int j = queryIdx; j <= queryIdx + queryLen; j++) {
        cout << str[j];
      }
      cout << '\n';
    }
  }
  return 0;
}

### Prompt

Please formulate a cpp solution to the following problem:

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". 

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9, PSEGLIM = 3000000, STLIM = 500500;
string alphabet = "$abcdefghijklmnopqrstuvwxyz";
vector<int> idxOf, invSA;
string str;
int N, M, curTime;
struct SuffixArray {
  string S;
  int N;
  vector<int> sa, rsa, lcp;
  void init(const string& _S) {
    S = _S;
    N = (int)S.length();
    genSA();
    genLCP();
  }
  void genSA() {
    sa.resize(N, 0);
    rsa.resize(N, 0);
    for (int i = 0; i < N; ++i) sa[i] = i;
    sort(sa.begin(), sa.end(), [&](int a, int b) { return S[a] < S[b]; });
    for (int i = 0; i < N; ++i) {
      bool same = i && S[sa[i]] == S[sa[i - 1]];
      rsa[sa[i]] = same ? rsa[sa[i - 1]] : i;
    }
    for (int len = 1; len < N; len *= 2) {
      vector<int> is(rsa), s(sa), nex(N);
      iota(nex.begin(), nex.end(), 0);
      for (int i = -1; i < N; ++i) {
        int s1 = (i == -1 ? N : s[i]) - len;
        if (s1 >= 0) sa[nex[rsa[s1]]++] = s1;
      }
      for (int i = 0; i < N; ++i) {
        bool same = i && sa[i - 1] + len < N && is[sa[i]] == is[sa[i - 1]] &&
                    is[sa[i] + len] == is[sa[i - 1] + len];
        rsa[sa[i]] = same ? rsa[sa[i - 1]] : i;
      }
    }
  }
  void genLCP() {
    lcp.resize(N - 1);
    int h = 0;
    for (int i = 0; i < N; ++i) {
      if (rsa[i]) {
        for (int j = sa[rsa[i] - 1]; j + h < N && S[i + h] == S[j + h]; h++)
          ;
        lcp[rsa[i] - 1] = h;
        if (h) h--;
      }
    }
  }
};
struct RMQ {
  vector<vector<int>> v;
  int n;
  void init(vector<int> a) {
    n = (int)a.size();
    int maxlog = 32 - __builtin_clz(n), r = 1;
    v.resize(maxlog + 1, vector<int>(n));
    for (int i = 0; i < n; ++i) v[0][i] = a[i];
    for (int e = 1; e <= maxlog; e++) {
      for (int i = 0; i < n; i++) {
        v[e][i] = min(v[e - 1][i], v[e - 1][min(n - 1, i + r)]);
      }
      r <<= 1;
    }
  }
  int query(int from, int to) {
    if (from == to) return v[0][from];
    int size = to - from + 1, e = 32 - __builtin_clz(size) - 1;
    return min(v[e][from], v[e][to - (1 << e) + 1]);
  }
};
struct PST {
  int L[PSEGLIM], R[PSEGLIM], MX[PSEGLIM], LFT[PSEGLIM], RGT[PSEGLIM];
  int cur;
  void init(int N) {
    for (int i = 0; i < PSEGLIM; ++i) {
      L[i] = -1;
      R[i] = -1;
      MX[i] = -1;
      LFT[i] = -1;
      RGT[i] = -1;
    }
    cur = 0;
    makeNode(0, N - 1);
  }
  int makeNode(int ll, int rr) {
    int res = cur++;
    L[res] = ll;
    R[res] = rr;
    MX[res] = -oo;
    if (ll != rr) {
      int md = (ll + rr) >> 1;
      LFT[res] = makeNode(ll, md);
      RGT[res] = makeNode(md + 1, rr);
    }
    return res;
  }
  int cloneNode(int id) {
    int res = cur++;
    L[res] = L[id];
    R[res] = R[id];
    MX[res] = MX[id];
    LFT[res] = LFT[id];
    RGT[res] = RGT[id];
    return res;
  }
  int pointUpdate(int id, int idx, int val) {
    int res = cloneNode(id);
    if (L[res] == R[res]) {
      MX[res] = val;
      return res;
    }
    int lf = LFT[res], rg = RGT[res];
    if (idx <= R[lf])
      LFT[res] = pointUpdate(lf, idx, val);
    else
      RGT[res] = pointUpdate(rg, idx, val);
    lf = LFT[res];
    rg = RGT[res];
    MX[res] = max(MX[lf], MX[rg]);
    return res;
  }
  int queryMax(int id, int ll, int rr) {
    if (ll <= L[id] && R[id] <= rr) return MX[id];
    if (R[id] < ll) return -oo;
    if (rr < L[id]) return -oo;
    return max(queryMax(LFT[id], ll, rr), queryMax(RGT[id], ll, rr));
  }
};
struct SuffixTree {
  int L[STLIM], R[STLIM], idxInStr[STLIM], inTime[STLIM], outTime[STLIM];
  int leaf[STLIM];
  int children[27][STLIM];
  int cur;
  void init() {
    for (int i = 0; i < STLIM; i++) {
      L[i] = -1;
      R[i] = -1;
      idxInStr[i] = -1;
      inTime[i] = -1;
      outTime[i] = -1;
      leaf[i] = -1;
    }
    for (int i = 0; i < 27; i++) {
      for (int j = 0; j < STLIM; j++) {
        children[i][j] = 0;
      }
    }
    cur = 0;
  }
  int makeNode(int ll, int rr) {
    int res = cur++;
    L[res] = ll;
    R[res] = rr;
    return res;
  }
};
SuffixArray saInit, saAll;
SuffixTree tree;
PST pstRoot;
RMQ rmqInit, rmqAll;
vector<int> startOf, roots, locs, Li, Ri;
vector<string> asks;
int curQuery, queryIdx, queryLen, qL, qR;
void buildTree(int L, int R, int haveLCP, int root) {
  if (L == R) {
    int idx = idxOf[str[saInit.sa[L] + haveLCP]];
    if (tree.children[idx][root] == 0) {
      tree.children[idx][root] = tree.makeNode(saInit.sa[L] + haveLCP, N - 1);
    }
    return;
  }
  int totLCP = rmqInit.query(L, R - 1) - haveLCP;
  if (totLCP != 0) {
    int idx = idxOf[str[saInit.sa[L] + haveLCP]];
    if (tree.children[idx][root] == 0) {
      int nL = saInit.sa[L] + haveLCP, nR = nL + totLCP - 1;
      tree.children[idx][root] = tree.makeNode(nL, nR);
    }
    buildTree(L, R, haveLCP + totLCP, tree.children[idx][root]);
    return;
  }
  int lo = L + 1, hi = R, bestIdx = L;
  for (int bs = 0; bs < 50 && lo <= hi; ++bs) {
    int mid = (lo + hi) / 2;
    int chk = rmqInit.query(L, mid - 1) - haveLCP;
    if (chk == 0)
      hi = mid - 1;
    else {
      bestIdx = mid;
      lo = mid + 1;
    }
  }
  buildTree(L, bestIdx, haveLCP, root);
  if (bestIdx + 1 <= R) {
    buildTree(bestIdx + 1, R, haveLCP, root);
  }
}
void setIdx(int now, int sum) {
  if (tree.L[now] != -1) {
    sum += tree.R[now] - tree.L[now] + 1;
  }
  tree.idxInStr[now] = N - sum;
  tree.leaf[now] = 1;
  tree.inTime[now] = curTime++;
  for (int i = 0; i < 27; ++i) {
    if (tree.children[i][now] == 0) continue;
    tree.leaf[now] = 0;
    setIdx(tree.children[i][now], sum);
  }
  if (tree.leaf[now]) {
    locs[tree.idxInStr[now]] = now;
  }
  tree.outTime[now] = curTime++;
}
bool solveQuery(int now, int matched, bool brokenOff) {
  if (tree.leaf[now]) {
    queryIdx = tree.idxInStr[now];
    queryLen = matched;
    return true;
  }
  int runs = 0;
  for (int i = 0; i < 27; ++i) {
    if (tree.children[i][now] == 0) continue;
    int to = tree.children[i][now];
    if (brokenOff) {
      int pos = qR - matched;
      if (pos >= 0) {
        int inTime = tree.inTime[to], outTime = tree.outTime[to];
        int chk = pstRoot.queryMax(roots[pos], inTime, outTime);
        if (chk >= qL) {
          bool ret = solveQuery(to, matched, brokenOff);
          runs++;
          if (ret) return ret;
          if (runs == 2) return false;
        }
      }
      continue;
    }
    int lenLeft = asks[curQuery].length() - matched;
    int lenHer = tree.R[to] - tree.L[to] + 1;
    int nmatch = matched;
    bool nBrokenOff = false;
    bool canQuery = false;
    if (lenLeft <= lenHer) {
      int i1 = invSA[startOf[curQuery] + matched];
      int i2 = invSA[tree.L[to]];
      int lcp = rmqAll.query(min(i1, i2), max(i1, i2) - 1);
      lcp = min(lcp, lenLeft);
      if (lenLeft == lcp) {
        nmatch += lenLeft;
        canQuery = true;
      } else {
        if (invSA[tree.L[to] + lcp] >
            invSA[startOf[curQuery] + matched + lcp]) {
          nmatch += lcp;
          nBrokenOff = true;
          canQuery = true;
        }
      }
    } else {
      int i1 = invSA[startOf[curQuery] + matched];
      int i2 = invSA[tree.L[to]];
      int lcp = rmqAll.query(min(i1, i2), max(i1, i2) - 1);
      lcp = min(lcp, lenHer);
      if (lcp == lenHer) {
        nmatch = matched + lcp;
        canQuery = true;
      } else {
        if (invSA[tree.L[to] + lcp] >
            invSA[startOf[curQuery] + matched + lcp]) {
          nmatch = matched + lcp;
          nBrokenOff = true;
          canQuery = true;
        }
      }
    }
    if (canQuery) {
      int pos = qR - nmatch;
      if (pos >= 0) {
        int inTime = tree.inTime[to], outTime = tree.outTime[to];
        int chk = pstRoot.queryMax(roots[pos], inTime, outTime);
        if (chk >= qL) {
          bool ret = solveQuery(to, nmatch, nBrokenOff);
          runs++;
          if (ret) return ret;
          if (runs == 2) return false;
        }
      }
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  idxOf.resize(256, 0);
  for (int i = 0; i < 27; ++i) idxOf[alphabet[i]] = i;
  cin >> str;
  str = str + "$";
  N = (int)str.length();
  curTime = 0;
  saInit.init(str);
  rmqInit.init(saInit.lcp);
  tree.init();
  int rootTree = tree.makeNode(-1, -1);
  buildTree(0, N - 1, 0, rootTree);
  locs.resize(N);
  setIdx(rootTree, 0);
  roots.resize(N);
  pstRoot.init(curTime - 1);
  roots[0] = 0;
  for (int i = 0; i < N; ++i) {
    if (i == 0) {
      roots[i] = pstRoot.pointUpdate(roots[i], tree.inTime[locs[i]], i);
    } else {
      roots[i] = pstRoot.pointUpdate(roots[i - 1], tree.inTime[locs[i]], i);
    }
  }
  int Q;
  cin >> Q;
  asks.resize(Q);
  Li.resize(Q);
  Ri.resize(Q);
  M = N;
  startOf.resize(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> Li[i] >> Ri[i];
    Li[i]--;
    Ri[i]--;
    cin >> asks[i];
    startOf[i] = M;
    M += (int)asks[i].length() + 1;
  }
  string bigStr(M, 0);
  for (int i = 0; i < N; ++i) bigStr[i] = str[i];
  int ptr = N;
  for (int i = 0; i < Q; ++i) {
    int len = (int)asks[i].length();
    for (int j = 0; j < len; j++) {
      bigStr[ptr++] = asks[i][j];
    }
    bigStr[ptr++] = '$';
  }
  saAll.init(bigStr);
  invSA.resize(M);
  for (int i = 0; i < M; ++i) {
    invSA[saAll.sa[i]] = i;
  }
  rmqAll.init(saAll.lcp);
  for (int i = 0; i < Q; ++i) {
    curQuery = i;
    queryIdx = -1;
    queryLen = -1;
    qL = Li[i];
    qR = Ri[i];
    bool ret = solveQuery(0, 0, false);
    if (!ret) {
      cout << -1 << '\n';
    } else {
      for (int j = queryIdx; j <= queryIdx + queryLen; j++) {
        cout << str[j];
      }
      cout << '\n';
    }
  }
  return 0;
}

```