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 SegmentTree { int n; int[] tree; public SegmentTree(int n) { this.n = n; tree = Enumerable.Repeat(int.MaxValue, n * 2 - 1).ToArray(); } public void Update(int i, int x) { tree[i += n - 1] = x; while (i > 0) tree[i = (i - 1) / 2] = Min(tree[i * 2 + 1], tree[i * 2 + 2]); } public int Query(int i, int j) => Query(i, j, 0, 0, n); int Query(int i, int j, int k, int l, int r) { if (r <= i || j <= l) return int.MaxValue; if (i <= l && r <= j) return tree[k]; var vl = Query(i, j, k * 2 + 1, l, (l + r) / 2); var vr = Query(i, j, k * 2 + 2, (l + r) / 2, r); return Min(vl, vr); } } static class Extentions { public static void Swap(ref T x, ref T y) { var tmp = x; x = y; y = tmp; } } 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 IEnumerable> Queries(long n, long m, long x, long d) { 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; yield return Pair.Create((int)i, (int)j); x = (x + d) % (n * (n - 1)); } } static int Lcp(string a, string b) => a.Zip(b, (x, y) => x == y).TakeWhile(x => x).Count(); 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; if (n > 100) return; // for debug var ss = s.Select(Pair.Create).OrderBy(a => a.First).ToArray(); var si = ss.Select((a, i) => Pair.Create(a.Second, i)) .OrderBy(a => a.First).Select(a => a.Second).ToArray(); var segtree = new SegmentTree(131072); var lcplen = new int[n - 1]; for (var i = 0; i < n - 1; i++) { lcplen[i] = Lcp(ss[i].First, ss[i + 1].First); segtree.Update(i, lcplen[i]); } var ans = 0L; foreach (var query in Queries(n, m, x, d)) { var p = query.First - 1; var q = query.Second - 1; var t = segtree.Query(si[p], si[q]); if (t == int.MaxValue) throw new Exception(); // for debug ans += t; } io.Write(ans); } }