#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > allSubstr(string s) {
  int n = s.size();
  for (int i = 0; i < n; i++) s[i] = s[i] - 'a' + 1;
  vector<pair<int, int> > v((n * (n + 1)) / 2);
  int sz = 0;
  for (int i = 0; i < n; ++i) {
    int H1 = 0, H2 = 0;
    for (int j = i; j < n; ++j) {
      H1 = (H1 * 27LL + s[j]) % 1000000007LL;
      H2 = (H2 * 27LL + s[j]) % 1000000009LL;
      v[sz++] = (make_pair((int)H1, (int)H2));
    }
  }
  sort(v.begin(), v.end());
  return v;
}
vector<pair<int, int> > hashes[10];
int L[10], R[10];
int main() {
  ios_base::sync_with_stdio(false);
  string s, t;
  int m, n;
  while (cin >> s) {
    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> t >> L[i] >> R[i];
      hashes[i] = allSubstr(t);
    }
    int n = s.size();
    for (int i = 0; i < n; i++) s[i] = s[i] - 'a' + 1;
    vector<pair<int, int> > v((n * (n + 1)) / 2);
    int sz = 0;
    for (int i = 0; i < n; ++i) {
      int H1 = 0, H2 = 0;
      for (int j = i; j < n; ++j) {
        H1 = (H1 * 27LL + s[j]) % 1000000007LL;
        H2 = (H2 * 27LL + s[j]) % 1000000009LL;
        pair<int, int> target = make_pair(H1, H2);
        v[sz++] = (target);
      }
    }
    sort(v.begin(), v.end());
    int nv = unique(v.begin(), v.end()) - v.begin(), ans = 0;
    for (int i = 0; i < nv; i++) {
      pair<int, int> target = v[i];
      bool solve = 1;
      for (int k = 0; k < m; k++) {
        int cnt = upper_bound(hashes[k].begin(), hashes[k].end(), target) -
                  lower_bound(hashes[k].begin(), hashes[k].end(), target);
        if (L[k] <= cnt && cnt <= R[k]) continue;
        solve = 0;
        break;
      }
      if (solve) ans++;
    }
    cout << ans << '\n';
  }
}
