using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; using System.Text; using static System.Console; using static System.Convert; using static System.Math; using static Extentions; class IO { int idx; string[] input; public IO(TextReader reader) { input = reader.ReadToEnd().Split(new[] { " ", "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries); } T Get(Func parser) => parser(input[idx++]); public string S => Get(s => s); public char C => Get(char.Parse); public int I => Get(int.Parse); public long L => Get(long.Parse); public double F => Get(double.Parse); public decimal D => Get(decimal.Parse); public BigInteger B => Get(BigInteger.Parse); T[] Gets(int n, Func parser) => input.Skip((idx += n) - n).Take(n).Select(parser).ToArray(); public string[] Ss(int n) => Gets(n, s => s); public char[] Cs(int n) => Gets(n, char.Parse); public int[] Is(int n) => Gets(n, int.Parse); public long[] Ls(int n) => Gets(n, long.Parse); public double[] Fs(int n) => Gets(n, double.Parse); public decimal[] Ds(int n) => Gets(n, decimal.Parse); public BigInteger[] Bs(int n) => Gets(n, BigInteger.Parse); public void Write(params T[] xs) => WriteLine(string.Join(" ", xs)); public void Write(params object[] xs) => WriteLine(string.Join(" ", xs)); } static class Pair { public static Pair Create(T1 first, T2 second) => new Pair(first, second); } struct Pair { public T1 First; public T2 Second; public Pair(T1 first, T2 second) { First = first; Second = second; } } class SparseTable { int[] xs, logTable; int[,] table; int n; public SparseTable(int[] xs) { n = xs.Length; this.xs = xs; logTable = new int[n + 1]; for (var i = 2; i <= n; i++) logTable[i] = logTable[i >> 1] + 1; table = new int[n, logTable[n] + 1]; for (var i = 0; i < n; i++) table[i, 0] = i; for (var k = 1; (1 << k) <= n; k++) { for (var i = 0; i + (1 << k) <= n; i++) { var a = table[i, k - 1]; var b = table[i + (1 << (k - 1)), k - 1]; table[i, k] = xs[a] < xs[b] ? a : b; } } } public int Query(int i, int j) { var k = logTable[j - i + 1]; return Min(xs[table[i, k]], xs[table[j - (1 << k) + 1, k]]); } } static class Extentions { public static void Swap(ref T x, ref T y) { var tmp = x; x = y; y = tmp; } public static IOrderedEnumerable Shuffle( this IEnumerable seq, Random rand) => seq.OrderBy(x => rand.Next()); } static class Program { public static void Main() { var sw = new StreamWriter(OpenStandardOutput()) { NewLine = "\n" }; #if DEBUG sw.AutoFlush = true; #else sw.AutoFlush = false; #endif SetOut(sw); Solve(new IO(In)); Out.Flush(); } static int Lcp(string a, string b) { var cnt = 0; var len = Min(a.Length, b.Length); for (var i = 0; i < len; i++) if (a[i] == b[i]) cnt++; else break; return cnt; } static void Solve(IO io) { var n = io.I; var s = io.Ss(n); var m = io.I; var x = io.L; var d = io.L; var rand = new Random(); var ss = s.Select(Pair.Create).Shuffle(rand).ToArray(); Array.Sort(ss, (a, b) => a.First.CompareTo(b.First)); var tsi = ss.Select((a, i) => Pair.Create(a.Second, i)) .Shuffle(rand).ToArray(); Array.Sort(tsi, (a, b) => a.First.CompareTo(b.First)); var si = tsi.Select(a => a.Second).ToArray(); var lcplen = new int[n - 1]; for (var i = 0; i < n - 1; i++) lcplen[i] = Lcp(ss[i].First, ss[i + 1].First); var st = new SparseTable(lcplen); var ans = 0; for (var k = 0; k < m; k++) { var i = x / (n - 1) + 1; var j = x % (n - 1) + 1; if (i > j) Swap(ref i, ref j); else j = j + 1; var p = si[i - 1]; var q = si[j - 1]; if (p > q) Swap(ref p, ref q); var t = st.Query(p, q - 1); ans += t; x = (x + d) % ((long)n * (n - 1)); } io.Write(ans); } }