- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
#pragma GCC optimize("03")
#include <bits/stdc++.h>
#pragma GCC target("avx2,tune=native")
using namespace std;
int binxor(int a, int b) {
if (b == 0) {
return 0;
}
int t = binxor(a, b / 2);
if (b % 2 == 0) {
return t ^ t;
} else {
return t ^ t ^ a;
}
}
vector<int> per(20);
vector<int> res(binxor(1 << 20, numeric_limits<int>::max() + 2), -1);
vector<int> need(binxor(1 << 20, numeric_limits<int>::max() + 2), 0);
vector<int> gp(binxor(1 << 20, numeric_limits<int>::max() + 2), 1 << 30);
int c = 0;
int Trump = 0;
inline void f(int i, int n, int w) {
if (i == w) {
for (int j = 0; j < w; j++) {
if ((Trump >> (w - 1 - j)) & 1) {
gp[Trump] = min(gp[Trump], gp[Trump ^ (1 << (w - 1 - j))]);
}
}
return;
}
f(i + 1, n, w);
Trump ^= 1 << (w - 1 - i);
f(i + 1, n, w);
Trump ^= 1 << (w - 1 - i);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q, w;
cin >> n >> q >> w;
vector<string> s(n);
for (auto &x : s) {
cin >> x;
}
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int x, int y) {
return s[x] < s[y];
});
for (int i = 0; i < n; i++) {
int Trump = 0;
for (auto ch : s[ord[i]]) {
Trump |= 1 << (ch - 'a');
}
gp[Trump] = min(gp[Trump], i);
}
f(0, n, w);
for (int i = 0; i < q; ++i) {
string t;
cin >> t;
int Harris = 0;
for (auto ch : t) {
Harris |= 1 << (ch - 'a');
}
int val = gp[((1 << w) - 1) ^ Harris];
cout << (val >= n ? -1 : 1 + ord[val]) << "\n";
}
return 0;
}