#include using namespace std; #define _p(...) (void)printf(__VA_ARGS__) #define forr(x,arr) for(auto&& x:arr) #define _overload3(_1,_2,_3,name,...) name #define _rep2(i,n) _rep3(i,0,n) #define _rep3(i,a,b) for(int i=int(a);i=int(a);i--) #define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__) #define all(x) (x).begin(),(x).end() #define bit(n) (1LL<<(n)) #define sz(x) ((int)(x).size()) #define ten(n) ((int)1e##n) #define fst first #define snd second using ll=long long; using pii=pair;using pll=pair;using pil=pair;using pli=pair; using vs=vector;using vvs=vector;using vvvs=vector; using vb=vector;using vvb=vector;using vvvb=vector; using vi=vector;using vvi=vector;using vvvi=vector; using vl=vector;using vvl=vector;using vvvl=vector; using vd=vector;using vvd=vector;using vvvd=vector; using vpii=vector;using vvpii=vector;using vvvpii=vector; template bool amax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template bool amin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } ll ri(){ll l;cin>>l;return l;} string rs(){string s;cin>>s;return s;} templateT read(){T t;cin>>t;return t;} templateostream&operator<<(ostream&o,const pair&p){return o<<'('<ostream&operator<<(ostream&o,const vector&t){o<<"{";forr(e,t)o<void e_r_r(vs::iterator it,T a,Args... args){cerr<substr((*it)[0]==' ',it->length())<<" = "<> trie; vector> E; Trie() { assign_new_state(); } int add_word(const string& word) { int now = 0; for (char c : word) { c -= 'a'; if (trie[now][c] == EMPTY) { trie[now][c] = trie.size(); assign_new_state(); E[now].emplace_back(trie[now][c]); } now = trie[now][c]; } return now; } private: void assign_new_state() { trie.emplace_back(26, EMPTY); E.emplace_back(); }; }; template struct SparseTable { vector> table; SparseTable() {} SparseTable(const vector &A) { init(A); } void init(const vector &A) { int n = (int) A.size(); int height = 32 - __builtin_clz(n - 1); int width = 1 << height; table.resize(height, vector(width)); for (int i = 0; i < width; i++) table[0][i] = A[i]; for (int i = 1; i < height; i++) for (int j = 0; j < width; j++) { const int b = ((j >> i) | 1) << i; table[i][j] = (j >> i) & 1 ? query(b, j) : query(j, b-1); } } // [l, r] T query(int l, int r) { if (l == r) return table[0][l]; const int d = 31 - __builtin_clz(l ^ r); return min(table[d][l], table[d][r]); } }; // complexity depends on RMQ template struct LCA { static const int inf = 1e9+7; size_t n; vector D, P, L, R; vector> ET; vector WD; vector> W; bool weighted; RMQ rmq; LCA(const vector> &G) : n(G.size()), D(n), P(n), L(n), R(n), WD(n), W(n), weighted(0) { dfs(G); rmq.init(ET); } LCA(const vector>> &G) : n(G.size()), D(n), P(n), L(n), R(n), WD(n), W(n), weighted(1) { vector> H(n); for (int i = 0; i < n; i++) for (pii x : G[i]) { H[i].emplace_back(x.first); W[i].emplace_back(x.second); } dfs(H); rmq.init(ET); } void dfs(const vector> &G, int root = 0) { vector count(n); stack S; S.push(root); while (!S.empty()) { int v = S.top(); S.pop(); if (count[v] == 1) { count[v] = 2; R[v] = ET.size() - 1; if (P[v] >= 0) ET.emplace_back(D[P[v]], P[v]); } else if (count[v] == 0) { S.push(v); count[v] = 1; L[v] = ET.size(); ET.emplace_back(D[v], v); for (int i = 0; i < (int)G[v].size(); i++) if (!count[G[v][i]]) { D[G[v][i]] = D[v] + 1; WD[G[v][i]] = WD[v] + (weighted ? W[v][i] : 1); P[G[v][i]] = v; S.push(G[v][i]); } } } } int query(int u, int v) { u = L[u]; v = L[v]; if (u > v) swap(u, v); return rmq.query(u, v).second; } long long dist(int u, int v) { return WD[u] + WD[v] - 2 * WD[query(u, v)]; } }; void Main() { ll n = ri(); Trie trie; vi V(n); vi L(n); rep(i, n) { string s = rs(); V[i] = trie.add_word(s); L[i] = sz(s); } LCA> lca(trie.E); ll m = ri(); ll x = ri(); ll d = ri(); ll ans = 0; rep(k, 1, m+1) { ll i = (x / (n - 1)); ll j = (x % (n - 1)); if (i > j) swap(i, j); else j++; x = (x + d) % (n * (n - 1)); int u = V[i]; int v = V[j]; int d = lca.dist(u, v); int a = (L[i] + L[j] - d) / 2; ans += a; } cout << ans << endl; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); Main(); return 0; }