#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5;
int n, q;
string a[N + 1];
struct query {
int k, l;
vector<int> g;
} Q[N + 1];
struct trie {
vector<array<int, 26>> T;
vector<int> val;
trie(): T(1), val(1) {}
int add(string &s, int x, int e) {
int u = 0, cnt = 0;
for (int c : s) {
c -= 'a';
if (!T[u][c])
T[u][c] = T.size(),
T.emplace_back(),
val.emplace_back();
u = T[u][c];
cnt -= val[u] == e;
val[u] += x;
cnt += val[u] == e;
}
return cnt;
}
};
trie T;
namespace sub14 {
bool detect() {
return true;
}
void main() {
for (int i = 0; i < q; ++i) {
auto [k, l, g] = Q[i];
int ans = 0;
for (int j : g)
ans += T.add(a[j], 1, l);
cout << ans << '\n';
for (int j : g)
T.add(a[j], -1, l);
}
}
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0);
#define _ "purple"
if (fopen(_ ".inp", "r")) {
freopen(_ ".inp", "r", stdin);
freopen(_ ".out", "w", stdout);
}
cin >> n >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 0; i < q; ++i) {
cin >> Q[i].k >> Q[i].l;
Q[i].g.resize(Q[i].k);
for (int &j : Q[i].g) cin >> j;
}
if (sub14::detect()) sub14::main();
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIGludCBsb25nIGxvbmcKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTiA9IDJlNTsKaW50IG4sIHE7CnN0cmluZyBhW04gKyAxXTsKCnN0cnVjdCBxdWVyeSB7CiAgICBpbnQgaywgbDsKICAgIHZlY3RvcjxpbnQ+IGc7Cn0gUVtOICsgMV07CgpzdHJ1Y3QgdHJpZSB7CiAgICB2ZWN0b3I8YXJyYXk8aW50LCAyNj4+IFQ7CiAgICB2ZWN0b3I8aW50PiB2YWw7CiAgICB0cmllKCk6IFQoMSksIHZhbCgxKSB7fQoKICAgIGludCBhZGQoc3RyaW5nICZzLCBpbnQgeCwgaW50IGUpIHsKICAgICAgICBpbnQgdSA9IDAsIGNudCA9IDA7CiAgICAgICAgZm9yIChpbnQgYyA6IHMpIHsKICAgICAgICAgICAgYyAtPSAnYSc7CiAgICAgICAgICAgIGlmICghVFt1XVtjXSkKICAgICAgICAgICAgICAgIFRbdV1bY10gPSBULnNpemUoKSwKICAgICAgICAgICAgICAgIFQuZW1wbGFjZV9iYWNrKCksCiAgICAgICAgICAgICAgICB2YWwuZW1wbGFjZV9iYWNrKCk7CgogICAgICAgICAgICB1ID0gVFt1XVtjXTsKICAgICAgICAgICAgY250IC09IHZhbFt1XSA9PSBlOwogICAgICAgICAgICB2YWxbdV0gKz0geDsKICAgICAgICAgICAgY250ICs9IHZhbFt1XSA9PSBlOwogICAgICAgIH0KICAgICAgICByZXR1cm4gY250OwogICAgfQp9OwoKdHJpZSBUOwoKbmFtZXNwYWNlIHN1YjE0IHsKICAgIGJvb2wgZGV0ZWN0KCkgewogICAgICAgIHJldHVybiB0cnVlOwogICAgfQoKICAgIHZvaWQgbWFpbigpIHsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHE7ICsraSkgewogICAgICAgICAgICBhdXRvIFtrLCBsLCBnXSA9IFFbaV07CiAgICAgICAgICAgIGludCBhbnMgPSAwOwogICAgICAgICAgICBmb3IgKGludCBqIDogZykKICAgICAgICAgICAgICAgIGFucyArPSBULmFkZChhW2pdLCAxLCBsKTsKICAgICAgICAgICAgY291dCA8PCBhbnMgPDwgJ1xuJzsKICAgICAgICAgICAgZm9yIChpbnQgaiA6IGcpCiAgICAgICAgICAgICAgICBULmFkZChhW2pdLCAtMSwgbCk7CiAgICAgICAgfQogICAgfQp9CgppbnQzMl90IG1haW4oKSB7CiAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbygwKSwgY2luLnRpZSgwKTsKICAgIAogICAgI2RlZmluZSBfICJwdXJwbGUiCiAgICBpZiAoZm9wZW4oXyAiLmlucCIsICJyIikpIHsKICAgICAgICBmcmVvcGVuKF8gIi5pbnAiLCAiciIsIHN0ZGluKTsKICAgICAgICBmcmVvcGVuKF8gIi5vdXQiLCAidyIsIHN0ZG91dCk7CiAgICB9CiAgICAKICAgIGNpbiA+PiBuID4+IHE7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyArK2kpIGNpbiA+PiBhW2ldOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBxOyArK2kpIHsKICAgICAgICBjaW4gPj4gUVtpXS5rID4+IFFbaV0ubDsKICAgICAgICBRW2ldLmcucmVzaXplKFFbaV0uayk7CgogICAgICAgIGZvciAoaW50ICZqIDogUVtpXS5nKSBjaW4gPj4gajsKICAgIH0KCiAgICBpZiAoc3ViMTQ6OmRldGVjdCgpKSBzdWIxNDo6bWFpbigpOwp9Cg==