#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;
const int MAXN = 1000001;
const int sigma = 26;
int to[MAXN][sigma];
int sz;
int dp[MAXN];
int term[MAXN];
void add_str(string &s)
{
int cur = 0;
for(int i = 0; i < s.length(); i++)
{
char c = s[i];
if(!to[cur][c - 'a'])
{
to[cur][c - 'a'] = sz++;
}
cur = to[cur][c - 'a'];
}
term[cur]++;
}
void reset(int u)
{
sz=1;term[u]=0;
for(int i = 0; i < sigma; i++)
{
if(to[u][i])
{
int z = to[u][i];
to[u][i]=0;
reset(z);
}
}
}
int prefix(string &s)
{
int cur = 0;
int cnt=0;
for(int i = 0; i < s.length(); i++)
{
char c = s[i];
cnt+=term[cur];
//cerr<<i<<' '<<c<<' '<<cnt<<' '<<to[cur][c-'a']<<'\n';
if(!to[cur][c - 'a'])
{
return cnt;
}
cur=to[cur][c-'a'];
}
cnt+=term[cur];
return cnt;
}
string a[100001];
vector<pair<vector<string>, int> > vec;
int ans[100001];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
sz=1;
int n; cin>>n;
for(int i = 0; i < n; i++)
{
cin>>a[i];
}
int q; cin>>q;
vec.resize(q);
for(int i = 0; i < q; i++)
{
int m,c;
cin>>m>>c;
vec[i].se=c;
for(int j = 0;j<m;j++)
{
string s;cin>>s;
vec[i].fi.pb(s);
}
}
memset(ans,-1,sizeof(ans));
deque<pair<ii,vi> > dq;
vi ve2c;
for(int i = 0; i<q;i++) ve2c.pb(i);
dq.pb(mp(mp(1,n+1),ve2c));
int ptr=0;
while(!dq.empty())
{
int l=dq.front().fi.fi; int r=dq.front().fi.se;
int mid=(l+r)>>1;
if(l==r)
{
if(l>n)
{
dq.pop_front();
continue;
}
for(int i = 0; i < dq.front().se.size(); i++)
{
ans[dq.front().se[i]]=l;
}
dq.pop_front();
continue;
}
if(ptr>=mid)
{
ptr=0;
reset(0);
}
while(ptr<mid)
{
add_str(a[ptr]);
ptr++;
}
vi L,R;
for(int i = 0; i < dq.front().se.size(); i++)
{
int u = dq.front().se[i];
ll ct = 0;
for(int j = 0; j < vec[u].fi.size(); j++)
{
ll x=prefix(vec[u].fi[j]);
ct+=x;
// cerr<<u<<' '<<mid<<' '<<vec[u].fi[j]<<' '<<x<<'\n';
if(ct>=vec[u].se) break;
}
//cerr<<u<<' '<<mid<<' '<<ct<<' '<<vec[u].se<<'\n';
if(ct>=vec[u].se)
{
L.pb(u);
}
else
{
R.pb(u);
}
}
dq.pop_front();
dq.push_back(mp(mp(l,mid),L));
dq.pb(mp(mp(mid+1,r),R));
}
for(int i = 0; i < q; i++)
{
cout<<ans[i]<<'\n';
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4KI2luY2x1ZGUgPGV4dC9wYl9kcy90cmVlX3BvbGljeS5ocHA+CiAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdXNpbmcgbmFtZXNwYWNlIF9fZ251X3BiZHM7CiAKI2RlZmluZSBmaSBmaXJzdAojZGVmaW5lIHNlIHNlY29uZAojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIGZibyBmaW5kX2J5X29yZGVyCiNkZWZpbmUgb29rIG9yZGVyX29mX2tleQogCnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZGVmIHBhaXI8bGwsbGw+IGlpOwp0eXBlZGVmIHZlY3RvcjxpbnQ+IHZpOwp0eXBlZGVmIGx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