import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; void main() { auto N = readln.chomp.to!int; auto S = N.iota.map!(i => tuple(readln.chomp, i)).array; auto ttt = readln.split.map!(to!long); auto M = ttt[0].to!int; auto x = ttt[1]; auto d = ttt[2]; auto I = new long[](M); auto J = new long[](M); foreach (k; 0..M) { I[k] = (x / (N - 1)) + 1; J[k] = (x % (N - 1)) + 1; if (I[k] > J[k]) swap(I[k], J[k]); else J[k] += 1; x = (x + d) % (N.to!long * (N.to!long - 1)); } S.sort(); auto D = new int[](N-1); foreach (i; 0..N-1) { foreach (j; 0..min(S[i][0].length, S[i+1][0].length)) { if (S[i][0][j] == S[i+1][0][j]) D[i]++; else break; } } auto rev = new int[](N); foreach (i; 0..N) { rev[S[i][1]] = i; } auto st = new SegmentTree(N-1); foreach (i; 0..N-1) st.update(i, D[i]); long[int][int] mem; long ans = 0; foreach (k; 0..M) { int i = rev[I[k.to!int].to!int - 1]; int j = rev[J[k.to!int].to!int - 1]; if (i > j) swap(i, j); if (i in mem && j in mem[i]) ans += mem[i][j]; ans += st.getmin(i, j-1); } ans.writeln; } class SegmentTree { int[] table; int size; this(int n) { assert(bsr(n) < 29); size = 1 << (bsr(n) + 2); table = new int[](size); fill(table, 1 << 29); } void update(int pos, int num) { return update(pos, num, 0, 0, size/2-1); } void update(int pos, int num, int i, int left, int right) { if (left == right) { table[i] = num; return; } auto mid = (left + right) / 2; if (pos <= mid) update(pos, num, i*2+1, left, mid); else update(pos, num, i*2+2, mid+1, right); table[i] = min(table[i*2+1], table[i*2+2]); } int getmin(int pl, int pr) { return getmin(pl, pr, 0, 0, size/2-1); } int getmin(int pl, int pr, int i, int left, int right) { if (pl > right || pr < left) return 1 << 29; else if (pl <= left && right <= pr) return table[i]; else return min(getmin(pl, pr, i*2+1, left, (left+right)/2), getmin(pl, pr, i*2+2, (left+right)/2+1, right)); } }