#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define llint long long #define inf 1e18 #define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++) #define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) using namespace std; typedef pair P; struct AhoCorasick{ struct TrieNode{ int num, sum; int next[26], suff; TrieNode(){ num = sum = 0; for(int i = 0; i < 26; i++) next[i] = -1; } }; vector trie; AhoCorasick(){ init(); } void init(){ trie.clear(); trie.push_back(TrieNode()); } int seek(string &s) { int p = 0; for(int i = 0; i < s.size(); i++){ int c = s[i]-'a'; if(trie[p].next[c] == -1){ trie.push_back(TrieNode()); trie[p].next[c] = (int)trie.size()-1; } p = trie[p].next[c]; } return p; } int trans(int v, int c) { if(trie[v].next[c] != -1) return trie[v].next[c]; if(v == 0) return 0; return trans(trie[v].suff, c); } void addString(string &s, int x = 1) { int p = seek(s); trie[p].num += x; } void makeSuffixLink() { queue Q; Q.push(0); trie[0].suff = 0; int v; while(Q.size()){ v = Q.front(); Q.pop(); trie[v].sum = trie[v].num + trie[trie[v].suff].sum; for(int i = 0; i < 26; i++){ int u = trie[v].next[i]; if(u == -1) continue; trie[u].suff = trans(trie[v].suff, i); if(v == 0) trie[u].suff = 0; Q.push(u); } } } }; llint n; string s, t[5005]; AhoCorasick aho; int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> s; cin >> n; for(int i = 1; i <= n; i++) cin >> t[i]; for(int i = 0; i < s.size(); i++) s[i] += 'a' - 'A'; for(int i = 1; i <= n; i++){ for(int j = 0; j < t[i].size(); j++){ t[i][j] += 'a' - 'A'; } } for(int i = 1; i <= n; i++) aho.addString(t[i]); aho.makeSuffixLink(); /*for(int i = 0; i < aho.trie.size(); i++){ for(int j = 0; j < 26; j++){ if(aho.trie[i].next[j] != -1) cout << (char)(j+'a') << aho.trie[i].next[j] << " "; } cout<< endl; }*/ /*for(int i = 0; i < aho.trie.size(); i++){ cout << i << " " << aho.trie[i].suff << " " << aho.trie[i].sum << endl; }*/ llint ans = 0, v = 0; for(int i = 0; i < s.size(); i++){ v = aho.trans(v, s[i]-'a'); ans += aho.trie[v].sum; } cout << ans << endl; return 0; }