#include using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, x, n) for(int i = x; i <= n; i++) #define rep3(i, x, n) for(int i = x; i >= n; i--) #define elif else if #define sp(x) fixed << setprecision(x) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() using ll = long long; using ld = long double; using pii = pair; using pil = pair; using pli = pair; using pll = pair; const ll MOD = 1e9+7; //const ll MOD = 998244353; const int inf = (1<<30)-1; const ll INF = (1LL<<60)-1; const ld EPS = 1e-10; template bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;}; template bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;}; template struct Sparse_Table{ Semigroup ope(Semigroup a, Semigroup b){ return min(a, b); } const int n; vector> st; vector lookup; Sparse_Table(const vector &table) : n(sz(table)){ st.resize(n); rep(i, n) st[i].pb(table[i]); for(int k = 0; (1<> N; string s[N]; rep(i, N) cin >> s[i]; pair p[N]; rep(i, N) p[i] = make_pair(s[i], i); sort(p, p+N); int ord[N]; rep(i, N) ord[p[i].second] = i; vector lcp(N-1); rep(i, N-1){ string a = p[i].first, b = p[i+1].first; lcp[i] = 0; while(min(sz(a), sz(b)) > lcp[i] && a[lcp[i]] == b[lcp[i]]) lcp[i]++; } Sparse_Table st(lcp); int M; ll x, d; cin >> M >> x >> d; ll ans = 0; rep(i, M){ int a = x/(N-1), b = x%(N-1); if(a <= b) b++; a = ord[a], b = ord[b]; if(a > b) swap(a, b); ans += st.query(a, b); x += d, x %= N*(N-1); } cout << ans << endl; }