#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define mt make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; class LCA { public: LCA() { } LCA(const vector> &G, int root = 0) :V((int)G.size()), depth(V), parent(V) { rep(i, LG)ancestor[i] = vector(V); // dfs(root, 0, 0, G); dfs_stk(root, G); rep(i, V) ancestor[0][i] = parent[i]; rep(i, LG - 1)rep(v, V) ancestor[i + 1][v] = ancestor[i][ancestor[i][v]]; } int get(int u, int v) { if (depth[u] < depth[v]) swap(u, v); int dist = depth[u] - depth[v]; for (int i = 0; i < LG; ++i) if ((dist >> i) & 1) u = ancestor[i][u]; if (u == v) return u; for (int i = LG - 1; i >= 0; --i) { if (ancestor[i][u] != ancestor[i][v]) { u = ancestor[i][u]; v = ancestor[i][v]; } } return ancestor[0][u]; } int getDepth(int u) { return depth[u]; } int goUp(int u, int len) { for (int i = 0; i < LG; ++i) if (len >> i & 1)u = ancestor[i][u]; return u; } int distance(int u, int v) { return depth[u] + depth[v] - 2 * depth[get(u, v)]; } int getParent(int u) { return parent[u]; } private: static const int LG = 18; int V; vector depth, parent, ancestor[LG]; void dfs(int v, int p, int d, const vector> &G) { depth[v] = d; parent[v] = p; for (int to : G[v]) { if (to != p) { dfs(to, v, d + 1, G); } } } void dfs_stk(int root, const vector > & G) { stack > stk; stk.push(mt(root, 0, 0)); while (sz(stk)) { int v, p, d; tie(v, p, d) = stk.top(); stk.pop(); depth[v] = d; parent[v] = p; for (int to : G[v]) { if (to != p) { stk.push(mt(to, v, d + 1)); } } } } }; pair si[100001]; int N; int M, pos[100001]; ll X, D; int I[3000000], J[3000000]; // tuple T[3000000]; char s_[800010]; string S[100001]; void init() { rep(i, M) { I[i] = (int)(X / (N - 1)) + 1; J[i] = (int)(X % (N - 1)) + 1; if (I[i] > J[i])swap(I[i], J[i]); else J[i] = J[i] + 1; X = (int)((X + D) % (N*(N - 1ll))); --I[i]; --J[i]; } } vector G; int st[300001][26]; int cur = 1; int f(string &s) { int u = 0; rep(i, sz(s)) { int a = s[i] - 'a'; if (st[u][a] != -1) { u = st[u][a]; } else { st[u][a] = cur; G[u].push_back(cur); u = cur; cur++; } } return u; } int main(){ cin >> N; MEM(st, -1); int V = 0; rep(i, N) { scanf("%s", s_); // si[i].first = string(s_); // si[i].second = i; S[i] = string(s_); V += sz(S[i]); } G.resize(V + 10); // sort(si, si + N); // rep(i, N) { // id[si[i].second] = i; // } cin >> M >> X >> D; init(); rep(i, N) { pos[i] = f(S[i]); } G.resize(cur); LCA lca(G); ll ans = 0; rep(i, M) { int anc = lca.get(pos[I[i]], pos[J[i]]); int dep = lca.getDepth(anc); ans += dep; } cout << ans << endl; }