#include int main() { int n; scanf("%d", &n); std::vector> s(n); std::vector len(n); int total = 0; for (int i = 0; i < n; i++) { static char buf[800100]; scanf("%s", buf); len[i] = strlen(buf); total += len[i]; s[i].resize((len[i] + 11) / 12); for (int j = 0; j < len[i]; j += 12) { long long val = 0; for (int k = 0; k < 12; k++) { val <<= 5; if (buf[j + k] != 0) { val |= buf[j + k] - 'a' + 1; } } s[i][j / 12] = val; } } std::vector perm(n); for (int i = 0; i < n; i++) { perm[i] = i; } std::sort(perm.begin(), perm.end(), [&](int i, int j) { return s[i] < s[j]; }); std::vector iperm(n); for (int i = 0; i < n; i++) { iperm[perm[i]] = i; } 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); rmq[0] = lcp; for (int i = 0; i + 1 < rmq.size(); i++) { rmq[i + 1].resize(rmq[i].size() - (1 << i)); for (int j = 0; j < rmq[i + 1].size(); j++) { rmq[i + 1][j] = std::min(rmq[i][j], rmq[i][j + (1 << i)]); } } long long ans = 0; int m; long long x, d; scanf("%d %lld %lld", &m, &x, &d); const long long N = 1LL * n * (n - 1); while (m--) { int i = x / (n - 1); int j = x % (n - 1); if (i > j) { std::swap(i, j); } else { j++; } x += d; if (x >= N) { x -= N; } i = iperm[i]; j = iperm[j]; if (i > j) { std::swap(i, j); } const int k = lg[j - i]; ans += std::min(rmq[k][i], rmq[k][j - (1 << k)]); } printf("%lld\n", ans); }