#include using namespace std; using ll = long long; const int INF = 1e9 + 10; const ll INFL = 4e18; vector suffix_array(const string &S) { int n = S.size(); vector suffixArray(n), ranks(n), tmp(n); for (int i = 0; i < n; ++i) { suffixArray[i] = i; ranks[i] = S[i]; } for (int k = 1; k < n; k *= 2) { auto cmp = [&](int i, int j) { if (ranks[i] != ranks[j]) return ranks[i] < ranks[j]; int ri = i + k < n ? ranks[i + k] : -1; int rj = j + k < n ? ranks[j + k] : -1; return ri < rj; }; sort(suffixArray.begin(), suffixArray.end(), cmp); tmp[suffixArray[0]] = 0; for (int i = 1; i < n; ++i) { tmp[suffixArray[i]] = tmp[suffixArray[i - 1]] + (cmp(suffixArray[i - 1], suffixArray[i]) ? 1 : 0); } ranks = tmp; } return suffixArray; } int main() { string S; int Q; cin >> S >> Q; int N = S.size(); vector sa = suffix_array(S); ll ans = 0; while (Q--) { string T; cin >> T; int M = T.size(); int left, right; { int lo = -1, hi = N; while (hi - lo > 1) { int mid = (hi + lo) / 2; string U = S.substr(sa[mid], M); if (U < T) { lo = mid; } else { hi = mid; } } left = lo; } { int lo = -1, hi = N; while (hi - lo > 1) { int mid = (hi + lo) / 2; string U = S.substr(sa[mid], M); if (U <= T) { lo = mid; } else { hi = mid; } } right = lo; } ans += right - left; } cout << ans << endl; }