#include using namespace std; inline unsigned lg(unsigned n) { asm("lzcnt %1,%0" : "=r"(n) : "r"(n)); return 31 - n; } class SuffixDataStructure { public: // a[i]>=0 SuffixDataStructure(const std::vector &a) : a(a) { sa = suffix_array(a); sa.insert(sa.begin(), a.size()); build_lcp(); build_rmq(); } int get_lcp(int i, int j) { if (i == j) { return a.size() - std::max(i, j); } i = isa[i]; j = isa[j]; if (i > j) { std::swap(i, j); } int k = lg(j - i); return std::min(rmq[k][i], rmq[k][j - (1 << k)]); } private: const std::vector &a; std::vector sa; std::vector isa; std::vector lcp; std::vector> rmq; vector suffix_array(vector a) { const int n = a.size(); if (n == 0) return {}; const int H = *max_element(begin(a), end(a)) + 1; vector s(n), ss, next(n), rank(n); for (int i = n - 2; i >= 0; i--) { s[i] = a[i] == a[i + 1] ? s[i + 1] : a[i] < a[i + 1]; if (!s[i] && s[i + 1]) { next[i + 1] = ss.empty() ? n : ss.back(); ss.push_back(i + 1); } } auto mp = [&](vector f, vector a) { for (int &x : a) x = f[x]; return a; }; auto induced_sort = [&]() { vector sa(n, -1), L(H + 1); for (int x : a) L[x + 1]++; for (int i = 0; i < H; i++) L[i + 1] += L[i]; auto S = L, SS = L; sa[L[a[n - 1]]++] = n - 1; for_each(ss.rbegin(), ss.rend(), [&](int i) { sa[--SS[a[i] + 1]] = i; }); for_each(sa.begin(), sa.end(), [&](int i) { i--; if (i >= 0 && !s[i]) sa[L[a[i]]++] = i; }); for_each(sa.rbegin(), sa.rend(), [&](int i) { i--; if (i >= 0 && s[i]) sa[--S[a[i] + 1]] = i; }); return sa; }; reverse(ss.begin(), ss.end()); int j = -1; for (int i : induced_sort()) if (0 < i && s[i] && !s[i - 1]) { if (j != -1) rank[i] = rank[j] + lexicographical_compare(a.begin() + j, a.begin() + next[j], a.begin() + i, a.begin() + next[i]); j = i; } ss = mp(ss, suffix_array(mp(rank, ss))); return induced_sort(); } void build_lcp() { const int n = a.size(); isa.resize(n + 1); lcp.assign(n + 1, 0); for (int i = 0; i <= n; i++) { isa[sa[i]] = i; } int k = 0; for (int i = 0; i < n; i++) { int j = sa[isa[i] - 1]; k = std::max(0, k - 1); while (i + k < n && j + k < n && a[i + k] == a[j + k]) { k++; } lcp[isa[i] - 1] = k; } } void build_rmq() { const int n = lcp.size(); const int m = lg(n); rmq.assign(m + 1, std::vector(n)); for (int i = 0; i < n; i++) { rmq[0][i] = lcp[i]; } for (int i = 0; i < m; i++) { for (int j = 0; j + (1 << i) < n; j++) { rmq[i + 1][j] = std::min(rmq[i][j], rmq[i][j + (1 << i)]); } } } }; int main() { int n; cin >> n; std::vector start(n); std::vector t; std::vector len(n); for (int i = 0; i < n; i++) { start[i] = t.size(); static char buf[800001]; scanf("%s", buf); int j; for (j = 0; buf[j] != 0; j++) { t.push_back(buf[j]); } len[i] = j; } SuffixDataStructure suf(t); int m; long long x, d; cin >> m >> x >> d; long long ans = 0; 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 = j + 1; } x += d; if (x >= N) { x -= N; } ans += min(suf.get_lcp(start[i], start[j]), min(len[i], len[j])); } std::cout << ans << std::endl; }