#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
const int MAXCHAR = 26;
struct Vertex {
int next[MAXCHAR], go[MAXCHAR];
int leaf = -1;
int p = -1;
char pch;
int link = -1, leaflink = -1;
Vertex(int p = -1, char ch = '$') : p(p), pch(ch) {
fill(begin(next), end(next), -1);
fill(begin(go), end(go), -1);
}
};
vector<Vertex> trie(1);
void add_string(string const &s, int idx) {
int v = 0;
for (char ch : s) {
int c = ch - 'a';
if (trie[v].next[c] == -1) {
trie[v].next[c] = trie.size();
trie.emplace_back(v, ch);
}
v = trie[v].next[c];
}
trie[v].leaf = idx;
}
int go(int v, char ch);
int get_link(int v) {
if (trie[v].link == -1) {
if (v == 0 || trie[v].p == 0)
trie[v].link = 0;
else
trie[v].link = go(get_link(trie[v].p), trie[v].pch);
get_link(trie[v].link); //
trie[v].leaflink = (trie[trie[v].link].leaf != -1) ? trie[v].link : trie[trie[v].link].leaflink;
}
return trie[v].link;
}
int go(int v, char ch) {
int c = ch - 'a';
if (trie[v].go[c] == -1) {
if (trie[v].next[c] != -1)
trie[v].go[c] = trie[v].next[c];
else
trie[v].go[c] = v == 0 ? 0 : go(get_link(v), ch);
}
return trie[v].go[c];
}
string S;
int N;
int K[MAXN];
string M[MAXN];
vector<int> results[MAXN];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> S;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> K[i];
cin >> M[i];
add_string(M[i], i);
}
for (int i = 1; i < trie.size(); i++) //
get_link(i); //
int v = 0;
for (int i = 0; i < S.size(); i++) {
v = go(v, S[i]);
int cur = v;
while (cur != -1) {
if(trie[cur].leaf != -1) //
results[trie[cur].leaf].push_back(i);
cur = trie[cur].leaflink;
}
}
for (int i = 0; i < N; i++) {
int minLength = 1 << 30;
for (int j = K[i]; j <= results[i].size(); j++) {
minLength = min(minLength, results[i][j - 1] - results[i][j - K[i]]);
}
if (minLength == 1 << 30) {
cout << -1 << endl;
} else {
cout << minLength + M[i].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AgICAgICAgIHRyaWVbdl0ubGluayA9IDA7CiAgICAgICAgZWxzZQogICAgICAgICAgICB0cmllW3ZdLmxpbmsgPSBnbyhnZXRfbGluayh0cmllW3ZdLnApLCB0cmllW3ZdLnBjaCk7CiAgICAgICAgZ2V0X2xpbmsodHJpZVt2XS5saW5rKTsgLy8gCiAgICAgICAgdHJpZVt2XS5sZWFmbGluayA9ICh0cmllW3RyaWVbdl0ubGlua10ubGVhZiAhPSAtMSkgPyB0cmllW3ZdLmxpbmsgOiB0cmllW3RyaWVbdl0ubGlua10ubGVhZmxpbms7CiAgICB9CiAgICByZXR1cm4gdHJpZVt2XS5saW5rOwp9CgppbnQgZ28oaW50IHYsIGNoYXIgY2gpIHsKICAgIGludCBjID0gY2ggLSAnYSc7CiAgICBpZiAodHJpZVt2XS5nb1tjXSA9PSAtMSkgewogICAgICAgIGlmICh0cmllW3ZdLm5leHRbY10gIT0gLTEpCiAgICAgICAgICAgIHRyaWVbdl0uZ29bY10gPSB0cmllW3ZdLm5leHRbY107CiAgICAgICAgZWxzZQogICAgICAgICAgICB0cmllW3ZdLmdvW2NdID0gdiA9PSAwID8gMCA6IGdvKGdldF9saW5rKHYpLCBjaCk7CiAgICB9CiAgICByZXR1cm4gdHJpZVt2XS5nb1tjXTsKfQoKc3RyaW5nIFM7CmludCBOOwppbnQgS1tNQVhOXTsKc3RyaW5nIE1bTUFYTl07CnZlY3RvcjxpbnQ+IHJlc3VsdHNbTUFYTl07CgppbnQgbWFpbigpIHsKICAgIGlvczo6c3luY193aXRoX3N0ZGlvKDApOwogICAgY2luLnRpZSgwKTsKICAgIGNpbiA+PiBTOwogICAgY2luID4+IE47CiAgICBmb3IgKGludCBpID0gMDsgaSA8IE47IGkrKykgewogICAgICAgIGNpbiA+PiBLW2ldOwogICAgICAgIGNpbiA+PiBNW2ldOwogICAgICAgIGFkZF9zdHJpbmcoTVtpXSwgaSk7CiAgICB9CiAgICBmb3IgKGludCBpID0gMTsgaSA8IHRyaWUuc2l6ZSgpOyBpKyspIC8vCiAgICAgICAgZ2V0X2xpbmsoaSk7ICAgICAgICAgICAgICAgICAgICAgIC8vCiAgICBpbnQgdiA9IDA7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IFMuc2l6ZSgpOyBpKyspIHsKICAgICAgICB2ID0gZ28odiwgU1tpXSk7CiAgICAgICAgaW50IGN1ciA9IHY7CiAgICAgICAgd2hpbGUgKGN1ciAhPSAtMSkgewogICAgICAgICAgICBpZih0cmllW2N1cl0ubGVhZiAhPSAtMSkgLy8gCiAgICAgICAgICAgICAgICByZXN1bHRzW3RyaWVbY3VyXS5sZWFmXS5wdXNoX2JhY2soaSk7CiAgICAgICAgICAgIGN1ciA9IHRyaWVbY3VyXS5sZWFmbGluazsKICAgICAgICB9CiAgICB9CiAgICBmb3IgKGludCBpID0gMDsgaSA8IE47IGkrKykgewogICAgICAgIGludCBtaW5MZW5ndGggPSAxIDw8IDMwOwogICAgICAgIGZvciAoaW50IGogPSBLW2ldOyBqIDw9IHJlc3VsdHNbaV0uc2l6ZSgpOyBqKyspIHsKICAgICAgICAgICAgbWluTGVuZ3RoID0gbWluKG1pbkxlbmd0aCwgcmVzdWx0c1tpXVtqIC0gMV0gLSByZXN1bHRzW2ldW2ogLSBLW2ldXSk7CiAgICAgICAgfQogICAgICAgIGlmIChtaW5MZW5ndGggPT0gMSA8PCAzMCkgewogICAgICAgICAgICBjb3V0IDw8IC0xIDw8IGVuZGw7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgY291dCA8PCBtaW5MZW5ndGggKyBNW2ldLnNpemUoKSA8PCBlbmRsOwogICAgICAgIH0KICAgIH0KfQ==