import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum LIM_LOGN = 17; int N; string[] S; int M; long X, D; void main() { try { for (; ; ) { N = readInt(); S = new string[N]; foreach (i; 0 .. N) { S[i] = readToken(); } M = readInt(); X = readLong(); D = readLong(); /* for k in 1 .. 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] = j[k] + 1 end x = (x + d) % (n * (n - 1)) end */ auto I = new long[M + 1]; auto J = new long[M + 1]; long x = X, d = D; foreach (k; 1 .. M + 1) { 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] = J[k] + 1; } x = (x + d) % (N * (N - 1)); } debug { writeln("I = ", I); writeln("J = ", J); } auto L = new int[N]; foreach (i; 0 .. N) { L[i] = cast(int)(S[i].length); } auto ord = iota(N).array.sort!((i, j) => (S[i] < S[j])); auto dro = new int[N]; foreach (k; 0 .. N) { dro[ord[k]] = k; } auto lcp = new int[N - 1]; foreach (k; 0 .. N - 1) { const i = ord[k]; const j = ord[k + 1]; int l; for (l = 0; l < L[i] && l < L[j] && S[i][l] == S[j][l]; ++l) {} lcp[k] = l; } debug { writeln(ord.map!(i => S[i])); writeln("lcp = ", lcp); } auto mn = new int[][](LIM_LOGN, N - 1); mn[0][] = lcp[]; foreach (e; 1 .. LIM_LOGN) { mn[e][] = mn[e - 1][]; foreach (k; 0 .. N - 1 - (1 << (e - 1))) { chmin(mn[e][k], mn[e - 1][k + (1 << (e - 1))]); } } debug { writeln("mn = ", mn); } long ansSum; foreach (m; 1 .. M + 1) { const i = cast(int)(I[m] - 1); const j = cast(int)(J[m] - 1); int kI = dro[i]; int kJ = dro[j]; if (kI > kJ) { swap(kI, kJ); } int ans; if (kI == kJ) { ans = L[i]; } else { const e = bsr(kJ - kI); ans = min(mn[e][kI], mn[e][kJ - (1 << e)]); debug { writeln(kI, " ", kJ, ": ", ans); } } ansSum += ans; } writeln(ansSum); } } catch (EOFException e) { } }