#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long double ld; typedef long long ll; typedef unsigned long long ull; typedef pair i_i; typedef pair ll_i; typedef pair i_ll; typedef pair d_i; typedef pair ll_ll; typedef pair d_d; struct edge { int u, v; ll w; }; #define rep(i, N) for (int i = 0; i < (int)(N); i++) #define pb push_back int INF = INT_MAX / 10; ll MOD = 1000000007; ll _MOD = 1000000009; double EPS = 1e-10; struct aho_corasick { int N, M; vector > next, accepts, uss; void dfs(int i, string& s, int j, int u) { if (j == s.length()) { accepts[u].pb(i); return; } int k = s[j] - 'A' + 1; if (next[u][k] == -1) { next.pb(vector(27, -1)); accepts.pb({}); if (j + 1 == uss.size()) uss.pb({}); uss[j + 1].pb(M); next[u][k] = M++; } dfs(i, s, j + 1, next[u][k]); } aho_corasick(vector patterns) { N = patterns.size(); M = 1; next.pb(vector(27, -1)); accepts.pb({}); uss.pb({0}); rep(i, N) dfs(i, patterns[i], 0, 0); for (vector us: uss) for (int u: us) for (int k = 1; k <= 26; k++) { if (next[u][k] == -1) continue; int v; for (v = next[u][0]; v != -1 && next[v][k] == -1; v = next[v][0]); next[next[u][k]][0] = (v == -1 ? 0 : next[v][k]); } } vector search(string s) { vector freq(M); int u = 0; for (char c: s) { int k = c - 'A' + 1; for (; u != -1 && next[u][k] == -1; u = next[u][0]); u = (u == -1 ? 0 : next[u][k]); freq[u]++; } vector ans(N); for (int j = uss.size() - 1; j > 0; j--) for (int u: uss[j]) { for (int i: accepts[u]) ans[i] = freq[u]; freq[next[u][0]] += freq[u]; } return ans; } }; int main() { string s; cin >> s; int N; cin >> N; vector patterns(N); rep(i, N) cin >> patterns[i]; aho_corasick ac(patterns); vector a = ac.search(s); cout << accumulate(a.begin(), a.end(), 0LL) << endl; }