import random class Rolling_Hash: def __init__(self, S): self.S = S self.MOD = (1<<61)-1 #self.B = random.randint(1<<12, 1<<28) self.B = 94627281 self.N = len(S) self.hs = [0] * (self.N+1) self.pw = [1] * (self.N+1) self.pwinv = [1] * (self.N+1) for i in range(self.N): self.hs[i+1] = (ord(S[i]) * self.pw[i] + self.hs[i]) % self.MOD self.pw[i+1] = (self.B * self.pw[i]) % self.MOD self.pwinv[self.N] = pow(self.pw[self.N], -1, self.MOD) for i in range(self.N): j = self.N - i - 1 self.pwinv[j] = (self.pwinv[j+1] * self.B) % self.MOD def hashvalue(self, idx): return self.hs[idx] def hash_substr(self, l, r): # [l, r) subhash = (self.hs[r] - self.hs[l]) return (subhash * self.pwinv[l]) % self.MOD def hash_segment(self, startidx, length): return self.hash_substr(startidx, startidx + length) def get_hasharray(self): return self.hs S = input() RH = Rolling_Hash(S) dict = {} ds = {} N = len(S) for L in range(1, 11): for i in range(N-L+1): hs = RH.hash_segment(i, L) if hs in dict: dict[hs] += 1 else: dict[hs] = 1 if S[i:i+L] in ds: ds[S[i:i+L]] += 1 else: ds[S[i:i+L]] = 1 #print(dict) M = int(input()) ans = 0 for i in range(M): C = input() RHC = Rolling_Hash(C) hsc = RHC.hash_substr(0,len(C)) if hsc in dict: #print('exist', C, hsc) ans += dict[hsc] print(ans)