using static System.Math; using System.Collections.Generic; using System; class RollingHash { public const ulong B = (ulong)1e9 + 7; public string S { get; set; } public int N { get; set; } public ulong[] Power { get; set; } public ulong[] Hash { get; set; } public RollingHash(string s) { this.S = s; this.N = s.Length; this.Power = new ulong[this.N + 1]; this.Power[0] = 1; for (int i = 0; i < N; i++) this.Power[i + 1] = this.Power[i] * B; this.Hash = new ulong[this.N + 1]; for (int i = 0; i < N; i++) this.Hash[i + 1] = this.Hash[i] * B + S[i]; } public ulong Get(int l, int r) => this.Hash[r] - (this.Hash[l] * this.Power[r - l]); } public class Hello { static void Main() { var s = Console.ReadLine().Trim(); var m = int.Parse(Console.ReadLine().Trim()); var c = new string[m]; var cLmax = 0; for (int i = 0; i < m; i++) { var c0 = Console.ReadLine().Trim(); c[i] = c0; cLmax = Max(cLmax, c0.Length); } getAns(s, m, c, cLmax); } static void getAns(string s, int m, string[] c, int cLmax) { var sL = s.Length; var rh = new RollingHash(s); var d = new Dictionary(); for (int i = 0; i < sL; i++) for (int j = 1; j <= cLmax; j++) { if (i + j <= sL) { var t = rh.Get(i, i + j); if (d.ContainsKey(t)) d[t]++; else d[t] = 1; } } var count = 0; foreach (var x in c) { var rhc = new RollingHash(x); var t2 = rhc.Get(0, x.Length); if (d.ContainsKey(t2)) count += d[t2]; } Console.WriteLine(count); } }