#include using namespace std; #define repd(i,a,b) for (ll i=(a);i<(b);i++) #define rep(i,n) repd(i,0,n) #define all(x) (x).begin(),(x).end() template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } typedef long long ll; typedef pair P; typedef vector vec; using Graph = vector; const long long INF = 1LL<<60; const long long MOD = 1000000007; #include #include #include using namespace std; template struct SparseTable { inline static constexpr T INF = numeric_limits::max() / 2; int N; vector > table; T f(T a, T b) { return min(a, b); } SparseTable() {} SparseTable(const vector &v) : N(v.size()) { int b = 1; while ((1 << b) <= N) ++b; table.push_back(v); for (int i = 1; i < b; i++) { table.push_back(vector(N, INF)); for (int j = 0; j + (1 << i) <= N; j++) { table[i][j] = f(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]); } } } // [l, r) T query(int l, int r) { assert(0 <= l and l <= r and r <= N); if (l == r) return INF; int b = 31 - __builtin_clz(r - l); return f(table[b][l], table[b][r - (1 << b)]); } }; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n;cin>>n; vector> s(n); rep(i,n){ cin>>s[i].first; s[i].second=i; } sort(all(s)); vec lcp(n-1); rep(i,n-1){ string s1=s[i].first; string s2=s[i+1].first; ll x=min(s1.size(),s2.size()); ll now=0; rep(j,x){ if(s1[j]!=s2[j])break; now++; } lcp[i]=now; } vec memo(n); rep(i,n){ memo[s[i].second]=i; } SparseTable st(lcp); ll m,x,d;cin>>m>>x>>d; ll ans=0; rep(i,m){ ll I=(x/(n-1))+1; ll J=(x%(n-1))+1; if(I>J)swap(I,J); else J++; I--;J--; if(memo[I]>memo[J])swap(I,J); ans+=st.query(memo[I],memo[J]); x=(x+d)%(n*(n-1)); } cout<