#include #include #include #include #include #include #include #include #include using namespace std; int main() { string s; cin >> s; int m; cin >> m; vector> succ(1); vector cnt(1); for (int i = 0; i < m; i++) { string c; cin >> c; int u = 0; for (char ch : c) { ch -= 'A'; if (succ[u][ch] == 0) { succ.push_back({}); cnt.push_back(0); succ[u][ch] = succ.size() - 1; } u = succ[u][ch]; } cnt[u]++; } queue q; vector link(succ.size()); q.push(0); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < 26; i++) { int v = succ[u][i]; if (v == 0) { succ[u][i] = succ[link[u]][i]; } else { link[v] = u == 0 ? 0 : succ[link[u]][i]; q.push(v); } } cnt[u] += cnt[link[u]]; } int u = 0; int ans = 0; for (char c : s) { u = succ[u][c - 'A']; ans += cnt[u]; } cout << ans << endl; }