#include #include constexpr int N = 800001; char buf[N]; int trie[N][26]; int pos[100000]; int eulerID[N]; int now; int rmq[21][N * 2]; int lg[N * 2]; int readint() { int n; scanf("%d", &n); return n; } long long readlong() { long long n; scanf("%lld", &n); return n; } void dfs(int u, int depth) { eulerID[u] = now; rmq[0][now++] = depth; for (int i = 0; i < 26; i++) { if (!trie[u][i]) { continue; } dfs(trie[u][i], depth + 1); rmq[0][now++] = depth; } } int main() { const int n = readint(); int cnt = 1; for (int i = 0; i < n; i++) { scanf("%s", buf); int u = 0; for (int j = 0; buf[j] != 0; j++) { char c = buf[j] - 'a'; if (!trie[u][c]) { trie[u][c] = cnt++; } u = trie[u][c]; } pos[i] = u; } dfs(0, 0); for (int i = 2; i <= now; i++) { lg[i] = lg[i / 2] + 1; } for (int i = 0; i + 1 <= lg[now]; i++) { for (int j = 0; j + (1 << i) < now; j++) { rmq[i + 1][j] = std::min(rmq[i][j], rmq[i][j + (1 << i)]); } } const int m = readint(); long long x = readlong(); const long long d = readlong(); long long ans = 0; for (int foo = 0; foo < m; foo++) { int i = x / (n - 1); int j = x % (n - 1); if (i > j) { std::swap(i, j); } else { j++; } x = (x + d) % (1LL * n * (n - 1)); int l = eulerID[pos[i]]; int r = eulerID[pos[j]]; if (l > r) { std::swap(l, r); } r++; const int k = lg[r - l]; ans += std::min(rmq[k][l], rmq[k][r - (1 << k)]); } printf("%lld\n", ans); }