#include using namespace std; using ll = long long; const ll INF = 1LL << 60; #define all(a) (a).begin(), (a).end() #define sz(a) ssize(a) template bool chmin(T &a, U b) { return a > b ? a = b, 1 : 0; } template bool chmax(T &a, U b) { return a < b ? a = b, 1 : 0; } using pll = pair; using vpll = vector; using vll = vector; using vvll = vector; #define _O(_1, _2, _3, n, ...) n #define _r1(i, n) for (ll i = 0; i < (ll)(n); i++) #define _r2(i, l, r) for (ll i = (ll)(l); i < (ll)(r); i++) #define rep(...) _O(__VA_ARGS__, _r2, _r1)(__VA_ARGS__) #define _rr1(i, n) for (ll i = (ll)(n) - 1; i >= 0; i--) #define _rr2(i, l, r) for (ll i = (ll)(r) - 1; i >= (ll)(l); i--) #define rrep(...) _O(__VA_ARGS__, _rr2, _rr1)(__VA_ARGS__) template ostream &operator<<(ostream &o, const vector &v) { rep(i, sz(v)) o << (i ? " " : "") << v[i]; return o; } template void out(const H &h, const T &...t) { cout << h; ((cout << " " << t), ...); #ifdef LOCAL cout << endl; #else cout << '\n'; #endif } struct SuffixAutomaton { struct Node { int len = 0, link = -1, end = -1, cnt = 0; // end: 出現右端, cnt: 接頭辞ノードなら 1 (DP初期値) map nxt; }; vector t = {{}}; int last = 0, n = 0; ll distinct = 0; SuffixAutomaton() = default; SuffixAutomaton(const auto &s) { t.reserve(2 * sz(s) + 2); for (ll c : s) extend(c); } int extend(ll c) { int cur = sz(t), p = last; t.push_back({t[last].len + 1, 0, ++n, 1, {}}); for (; ~p && !t[p].nxt.count(c); p = t[p].link) t[p].nxt[c] = cur; if (~p) { int q = t[p].nxt[c]; if (t[p].len + 1 == t[q].len) t[cur].link = q; else { int cl = sz(t); t.push_back({t[p].len + 1, t[q].link, t[q].end, 0, t[q].nxt}); for (; ~p && t[p].nxt[c] == q; p = t[p].link) t[p].nxt[c] = cl; t[q].link = t[cur].link = cl; } } return distinct += t[cur].len - t[t[cur].link].len, last = cur; } // notebook:extension-begin queries vector topo_order() const { vector b(n + 1), o(sz(t)); for (auto &nd : t) b[nd.len]++; rep(i, 1, n + 1) b[i] += b[i - 1]; rrep(i, sz(t)) o[--b[t[i].len]] = i; return o; // len 昇順 (逆順で suffix link 木 DP) } vll build_cnt() const { auto o = topo_order(); vll c(sz(t)); rep(i, sz(t)) c[i] = t[i].cnt; rrep(i, 1, sz(o)) c[t[o[i]].link] += c[o[i]]; return c; } int walk(const auto &p) const { int v = 0; for (ll c : p) { auto it = t[v].nxt.find(c); if (it == t[v].nxt.end()) return -1; v = it->second; } return v; } void step(int &v, int &len, ll c) const { while (~v && !t[v].nxt.count(c)) v = t[v].link, len = ~v ? t[v].len : 0; ~v ? (v = t[v].nxt.at(c), len++) : (v = len = 0); } array lcs(const auto &u) const { int v = 0, len = 0; array best = {0, 0, 0, 0}; rep(i, sz(u)) { step(v, len, u[i]); if (len > best[1] - best[0]) best = {t[v].end - len, t[v].end, i + 1 - len, i + 1}; } return best; } // notebook:extension-end queries // notebook:extension-begin kth_substr vll kth_substring(ll k) const { auto o = topo_order(); vll dp(sz(t), 1); dp[0] = 0; rrep(i, sz(o)) for (auto [c, v] : t[o[i]].nxt) dp[o[i]] = min(INF, dp[o[i]] + dp[v]); if (k < 0 || k >= dp[0]) return {}; vll res; int u = 0; while (true) { if (u && !k--) return res; for (auto [c, v] : t[u].nxt) { if (k < dp[v]) { res.push_back(c), u = v; break; } k -= dp[v]; } } } // notebook:extension-end kth_substr }; int main(){ std::cin.tie(nullptr), std::ios_base::sync_with_stdio(false); string t; cin >> t; SuffixAutomaton SA(t); auto c = SA.build_cnt(); ll q; cin >> q; ll sm = 0; while(q--){ string p; cin >> p; auto u = SA.walk(p); sm += (~u?c[u]:0); } out(sm); } // g++ -std=c++23 -DLOCAL tmp.cpp // ./a.out