#include using namespace std; using i64 = long long; using VI = vector; using pii = pair; template struct trie { vector tr; trie() { tr.push_back(Node()); }; int add(const string &s) { int n = s.size(); int p = 0; for (int i = 0; i < n; i++) { int c = s[i] - 'A'; if (!tr[p][c]) { tr[p][c] = tr.size(); tr.emplace_back(tr[p][c], tr[p].dep + 1); } p = tr[p][c]; } return p; } int size() const { return tr.size(); } }; template struct ACAutomaton : public trie { vector fail; ACAutomaton() { this->tr.push_back(Node()); }; void BuildAC() { fail.resize(this->tr.size()); queue Q; for (int i = 0; i < 26; i++) if (this->tr[0][i]) Q.push(this->tr[0][i]); while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int i = 0; i < 26; i++) { if (this->tr[u][i]) fail[this->tr[u][i]] = this->tr[fail[u]][i], Q.push(this->tr[u][i]); else this->tr[u][i] = this->tr[fail[u]][i]; } } return; } }; struct TrieNode { TrieNode() { id = 0, dep = 0, nxt = array(); }; TrieNode(int _id, int _dep) : id(_id), dep(_dep) {} int id; int dep; array nxt = {}; int &operator[](const int x) { return this->nxt[x]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string s; int m; cin >> s >> m; ACAutomaton ac; vector pos(m + 1); for (int i = 1; i <= m; i++) { string res; cin >> res; pos[i] = ac.add(res); } ac.BuildAC(); vector val(ac.size()); vector> adj(ac.size()); for (int i = 0; i < ac.size(); i++) { if (i != ac.fail[i]) adj[ac.fail[i]].push_back(i); } int p = 0; for (auto it : s) { p = ac.tr[p][it - 'A']; val[p]++; } function dfs = [&](int u) { for (auto v : adj[u]) { dfs(v); val[u] += val[v]; } }; dfs(0); int ans = 0; for (int i = 1; i <= m; i++) ans += val[pos[i]]; cout << ans << endl; return 0; }