#include <bits/stdc++.h>
using namespace std;
struct Word {
string s;
int index;
bool operator < (Word o) const {
return s < o.s;
}
};
int const WMAX = 1000000;
int const NMAX = 1000;
int w, n;
Word d[1 + WMAX];
bool isPrefix(string a, string b) {
if(b.length() < a.length()) return false;
return (b.substr(0, a.length()) == a);
}
int binarysearch(int from, int to, string val) {
//cout << "checking: " << val << ", " << from << " <-> " << to << "\n";
if(from < to) {
int guess = (from + to)/2;
if(val <= d[guess].s) {
return binarysearch(from, guess, val);
} else {
return binarysearch(guess+1, to, val);
}
}
return from;
}
int main() {
cin >> w >> n;
for(int i = 1; i <= w; i++) {
cin >> d[i].s;
d[i].index = i;
}
sort(d+1, d+w+1);
/*for(int i = 1; i <= w; i++) {
cout << d[i].s << "\n";
}*/
for(int i = 1; i <= n; i++) {
int k;
string pre;
cin >> k >> pre;
int ans = binarysearch(1, w, pre);
//cout << "here: " << ans << " " << k << "\n";
ans += k-1;
if(ans > w || !isPrefix(pre, d[ans].s)) {
cout << -1 << "\n";
} else {
cout << d[ans].index << "\n";
}
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IFdvcmQgewogICAgc3RyaW5nIHM7CiAgICBpbnQgaW5kZXg7CiAgICBib29sIG9wZXJhdG9yIDwgKFdvcmQgbykgY29uc3QgewogICAgICAgIHJldHVybiBzIDwgby5zOwogICAgfQp9OwoKaW50IGNvbnN0IFdNQVggPSAxMDAwMDAwOwppbnQgY29uc3QgTk1BWCA9IDEwMDA7CmludCB3LCBuOwpXb3JkIGRbMSArIFdNQVhdOwoKYm9vbCBpc1ByZWZpeChzdHJpbmcgYSwgc3RyaW5nIGIpIHsKCWlmKGIubGVuZ3RoKCkgPCBhLmxlbmd0aCgpKSByZXR1cm4gZmFsc2U7CglyZXR1cm4gKGIuc3Vic3RyKDAsIGEubGVuZ3RoKCkpID09IGEpOwp9CgppbnQgYmluYXJ5c2VhcmNoKGludCBmcm9tLCBpbnQgdG8sIHN0cmluZyB2YWwpIHsKCS8vY291dCA8PCAiY2hlY2tpbmc6ICIgPDwgdmFsIDw8ICIsICIgPDwgZnJvbSA8PCAiIDwtPiAiIDw8IHRvIDw8ICJcbiI7CiAgICBpZihmcm9tIDwgdG8pIHsKICAgICAgICBpbnQgZ3Vlc3MgPSAoZnJvbSArIHRvKS8yOwogICAgICAgIGlmKHZhbCA8PSBkW2d1ZXNzXS5zKSB7CiAgICAgICAgICAgIHJldHVybiBiaW5hcnlzZWFyY2goZnJvbSwgZ3Vlc3MsIHZhbCk7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcmV0dXJuIGJpbmFyeXNlYXJjaChndWVzcysxLCB0bywgdmFsKTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gZnJvbTsKfQoKaW50IG1haW4oKSB7CiAgICBjaW4gPj4gdyA+PiBuOwogICAgZm9yKGludCBpID0gMTsgaSA8PSB3OyBpKyspIHsKICAgICAgICBjaW4gPj4gZFtpXS5zOwogICAgICAgIGRbaV0uaW5kZXggPSBpOwogICAgfQogICAgc29ydChkKzEsIGQrdysxKTsKICAgIC8qZm9yKGludCBpID0gMTsgaSA8PSB3OyBpKyspIHsKICAgIAljb3V0IDw8IGRbaV0ucyA8PCAiXG4iOwogICAgfSovCiAgICBmb3IoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewogICAgICAgIGludCBrOwogICAgICAgIHN0cmluZyBwcmU7CiAgICAgICAgY2luID4+IGsgPj4gcHJlOwogICAgICAgIGludCBhbnMgPSBiaW5hcnlzZWFyY2goMSwgdywgcHJlKTsKICAgICAgICAvL2NvdXQgPDwgImhlcmU6ICIgPDwgYW5zIDw8ICIgIiA8PCBrIDw8ICJcbiI7CiAgICAgICAgYW5zICs9IGstMTsKICAgICAgICBpZihhbnMgPiB3IHx8ICFpc1ByZWZpeChwcmUsIGRbYW5zXS5zKSkgewogICAgICAgICAgICBjb3V0IDw8IC0xIDw8ICJcbiI7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgY291dCA8PCBkW2Fuc10uaW5kZXggPDwgIlxuIjsKICAgICAgICB9CiAgICB9Cn0=