// code by lynmisakura. wish to be accepted! #include using namespace std; #ifdef LOCAL #include "/Users/debug.h" #else #define debug(...) 42 #endif using ll = long long; using ull = unsigned long long; using ld = long double; using vi = vector; using vvi = vector; using vl = vector; using vvl = vector; using pi = pair; using pl = pair; using vpi = vector; using vpl = vector; #define REP(i, N) for(int i = 0;i < N;i++) #define mp make_pair #define pb push_back #define eb emplace_back #define all(x) (x).begin(),(x).end() template void init(vector &a, int N) { a.resize(N); } template void init(vector &a, int N, T val) { a.assign(N, val); } template bool chmin(T &a, T b) { if (a > b) { a = b; return true; } else { return false; } } template bool chmax(T &a, T b) { if (a < b) { a = b; return true; } else { return false; } } template T extgcd(T a, T b, T &x, T &y) { T d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } template T modinv(T a, T m) { T x, y; extgcd(a, m, x, y); return (m + x % m) % m; } template class rolling_hash { public: rolling_hash(vector &a) : a(a) { init(); } rolling_hash(std::string &s, char c = 'a') { a.resize((int)s.length()); for (int i = 0; i < (int)s.length(); i++) { a[i] = s[i] - c + 1; } init(); } void init() { N = a.size(); hash.assign(N + 1, 0); b_inv.assign(N + 1, 0); long b_pow = 1; for (int i = 0; i < N; i++) { hash[i + 1] = (hash[i] + a[i] * b_pow) % P; b_pow = (b_pow * B) % P; } b_inv[0] = 1; b_inv[1] = modinv(B, P); for (int i = 2; i <= N; i++) { b_inv[i] = (b_inv[i - 1] * b_inv[1]) % P; } } int64_t f(int l, int r) { int64_t res = (hash[r] - hash[l] + P) % P; res = (res * b_inv[l]) % P; return res; } int64_t f_all(){ return f(0,N); } private: int N; vector a; vector hash; vector b_inv; }; int main() { string s; cin >> s; int len = s.length(); rolling_hash rh_1(s,'A'); rolling_hash rh_2(s,'A'); map m; REP(i,len)for(int j = 1;j <= 10 && i + j <= len;j++){ ll h_1 = rh_1.f(i,i + j); ll h_2 = rh_2.f(i,i + j); m[mp(h_1,h_2)]++; } ll ans = 0; int q; cin >> q; while(q--){ string c; cin >> c; rolling_hash rh_3(c,'A'); rolling_hash rh_4(c,'A'); ll h_3 = rh_3.f_all(); ll h_4 = rh_4.f_all(); ans += m[mp(h_3,h_4)]; } cout << ans << '\n'; }