## https://yukicoder.me/problems/no/1160 B = 30 H = 9007199254740997 def main(): S = input() M = int(input()) C = [] for _ in range(M): C.append(input()) # Sについてローリングハッシュを計算 r_hash_list = [0 for _ in range(len(S) + 1)] r_hash = 0 for i in range(len(S)): r_hash *= B r_hash %= H r_hash += ord(S[i]) - ord("A") + 1 r_hash %= H r_hash_list[i + 1] = r_hash # hash_map = {} pow_b = 1 for l in range(1, min(10, len(S)) + 1): pow_b *= B pow_b %= H for i in range(len(S) - l + 1): h = (r_hash_list[i] * pow_b) % H h = (r_hash_list[i + l] - h) % H if h not in hash_map: hash_map[h] = 0 hash_map[h] += 1 answer = 0 for c in C: c_hash = 0 for i in range(len(c)): c_hash *= B c_hash %= H c_hash += ord(c[i]) - ord("A") + 1 c_hash %= H if c_hash in hash_map: answer += hash_map[c_hash] print(answer) if __name__ == "__main__": main()