#include #include #include #include #include #ifdef __linux__ #define getchar getchar_unlocked #define putchar putchar_unlocked #endif int main() { int n; scanf("%d", &n); std::vector> s(n); std::vector perm(n); for (int i = 0; i < n; i++) { perm[i] = i; } std::vector len(n); for (int i = 0; i < n; i++) { static char buf[100100]; scanf("%s", buf); len[i] = strlen(buf); for (int j = 0; buf[j] != 0; j += 12) { long long val = 0; for (int k = 0; k < 12; k++) { val <<= 5; if (buf[j + k] != 0) { val |= buf[k] - 'a' + 1; } } s[i].push_back(val); } } std::sort(perm.begin(), perm.end(), [&](int i, int j) { return s[i] < s[j]; }); std::vector lcp(n - 1); for (int i = 0; i < n - 1; i++) { const int l = perm[i]; const int r = perm[i + 1]; const int nl = s[l].size(); const int nr = s[r].size(); int h = 0; while (h < std::min(nl, nr) && s[l][h] == s[r][h]) { h++; } if (h == nl) { lcp[i] = len[l]; } else if (h == nr) { lcp[i] = len[r]; } else { long long v = s[l][h] ^ s[r][h]; int j; for (j = 0; j < 12; j++) { if (v >> (5 * (11 - j)) & 0x1f) { break; } } lcp[i] = h * 12 + j; } } std::vector lg(lcp.size() + 1); for (int i = 2; i < lg.size(); i++) { lg[i] = lg[i / 2] + 1; } std::vector> rmq(lg[lcp.size()] + 1, std::vector(lcp.size())); for (int i = 0; i < lcp.size(); i++) { rmq[0][i] = lcp[i]; } for (int i = 0; i < rmq.size() - 1; i++) { for (int j = 0; j + (1 << i) < rmq[i].size(); j++) { rmq[i + 1][j] = std::min(rmq[i][j], rmq[i][j + (1 << i)]); } } long long ans = 0; int m, x, d; scanf("%d %d %d", &m, &x, &d); while (m--) { int i = x / (n - 1); int j = x % (n - 1); if (i > j) { std::swap(i, j); } else { j++; } if (i == j) { ans += len[i]; } else { i = perm[i]; j = perm[j]; if (i > j) { std::swap(i, j); } int k = lg[j - i]; ans += std::min(rmq[k][i], rmq[k][j - (1 << k)]); } x = (x + d) % (1LL * n * (n - 1)); } printf("%lld", ans); }