#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #include using namespace std; using namespace atcoder; using S = int; S op(S l, S r){ if(l <= r) return l; else return r; } S e(){ return 1 << 30; } void solve(){ int n; cin >> n; vector le; int ind = 0; string S; vector size(n); for(int i = 0; i < n; i++){ le.push_back(ind); string s; cin >> s; S += s; ind += s.size(); size[i] = s.size(); } auto sa = suffix_array(S); auto lcp = lcp_array(S, sa); vector inv(ind); for(int i = 0; i < ind; i++){ inv[sa[i]] = i; } vector pos(n); vector> lst; for(int i = 0; i < n; i++){ int l = le[i]; pos[i] = inv[l]; lst.push_back({inv[l], i}); } sort(lst.begin(), lst.end()); map mp; vector A; for(int i = 0; i < n; i++){ int l = lst[i].first; mp[l] = i; if(i != 0){ int mi = 1 << 30; for(int j = lst[i - 1].first; j < l; j++){ mi = min(mi, lcp[j]); } int ii = lst[i].second; int jj = lst[i - 1].second; mi = min(mi, min(size[ii], size[jj])); A.push_back(mi); } } for(int i = 0; i < n; i++){ pos[i] = mp[pos[i]]; } segtree seg(A); int m; long long x, d; cin >> m >> x >> d; long long ans = 0; while(m--){ int i = x / (n - 1); int j = x - i * (n - 1); if(i > j) swap(i, j); else j++; int l = pos[i]; int r = pos[j]; if(l > r) swap(l, r); ans += seg.prod(l, r); x = (x + d) % (n * (n - 1)); } cout << ans << "\n"; } int main(){ cin.tie(0)->sync_with_stdio(0); int t; t = 1; // cin >> t; while(t--) solve(); }