class AhoCorasick: class State: def __init__(self, id): self.id = id self.next = {} def has_key(self, x): return x in self.next def __init__(self, terms): self.state = [AhoCorasick.State(0)] self.output = [[]] self._make_goto(terms) self._make_failure() def _make_goto(self, terms): for term in terms: cur = self.state[0] for x in term: if not cur.has_key(x): new = AhoCorasick.State(len(self.state)) cur.next[x] = new self.state.append(new) self.output.append([]) cur = cur.next[x] s = cur.id self.output[s].append(term) def _make_failure(self): failure = [0] * len(self.state) queue = [0] while queue: s = queue.pop() for x in self.state[s].next.keys(): next = self.g(s,x) if next is not None: queue.append(next) if s != 0: f = failure[s] while self.g(f, x) is None: f = failure[f] failure[next] = self.g(f, x) self.output[next].extend(self.output[failure[next]]) self.failure = failure def g(self, s, x): if x in self.state[s].next: return self.state[s].next[x].id else: if s == 0: return 0 else: return None def count(self, text): s = 0 total = 0 for c in text: while self.g(s, c) is None: s = self.failure[s] s = self.g(s, c) total += len(self.output[s]) # for i,x in enumerate(self.output[s]): # print('%d,%d => %s' % (i-len(x)+1, i, x)) return total text = input() m = int(input()) terms = [input() for i in range(m)] ac = AhoCorasick(terms) print(ac.count(text))