int n, m, k, q; string s; ll a[27][27]; int tr[N * 8][27]; ll cnt[N]; int tot = 1; voidinsert(string s) { int now = 1; s = s + (char)('a' - 1); for (char nowchar : s) { int x = nowchar - 'a' + 1; for (int y = 0; y < 27; y++) if (x != y && tr[now][y]) a[y][x] += cnt[tr[now][y]]; if (!tr[now][x]) tr[now][x] = ++tot; now = tr[now][x]; cnt[now]++; } } voidwork() { cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> s; insert(s); } while (q--) { string cmp; cin >> cmp; ll ans = 0; cmp = (char)('a' - 1) + cmp; for (int i = 0; i < 27; i++) for (int j = i + 1; j < 27; j++) ans = (ans + a[cmp[j] - 'a' + 1][cmp[i] - 'a' + 1]); cout << ans << endl; } } intmain() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); work(); }