#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline int read();
const int M = 500016, MOD = 1000000007;
int sz, lst;
int ch[M << 1][40], len[M << 1], link[M << 1], cnt[12][M << 1];
void extend(const char *s, int id) {
  for (int i = 0; s[i]; ++i) {
    int c = s[i] - 'a';
    int cur = ++sz;
    len[cur] = len[lst] + 1;
    int p = lst;
    while (!ch[p][c]) {
      ch[p][c] = cur;
      p = link[p];
    }
    if (ch[p][c] != cur) {
      int q = ch[p][c];
      if (len[p] + 1 == len[q])
        link[cur] = q;
      else {
        int clone = ++sz;
        memcpy(ch[clone], ch[q], sizeof(ch[q]));
        link[clone] = link[q];
        len[clone] = len[p] + 1;
        while (ch[p][c] == q) {
          ch[p][c] = clone;
          p = link[p];
        }
        link[q] = link[cur] = clone;
      }
    }
    lst = cur;
    ++cnt[id][cur];
  }
}
void build() {
  vector<int> nodes;
  for (int i = 1; i <= sz; ++i) nodes.push_back(i);
  sort(nodes.begin(), nodes.end(),
       [&](int a, int b) { return len[a] > len[b]; });
  for (auto u : nodes)
    for (auto cntx : cnt) cntx[link[u]] += cntx[u];
}
char str[M];
int lef[M], rig[M];
int main(void) {
  scanf("%s", str);
  extend(str, 0);
  lef[0] = 1, rig[0] = 50000;
  int n = read();
  for (int i = 1; i <= n; ++i) {
    string t = "{";
    extend(t.c_str(), n + 1);
    scanf("%s", str);
    extend(str, i);
    lef[i] = read(), rig[i] = read();
  }
  build();
  int ans = 0;
  for (int i = 1; i <= sz; ++i) {
    int suc = 1;
    for (int j = 0; j <= n; ++j)
      if (cnt[j][i] < lef[j] || cnt[j][i] > rig[j]) suc = 0, j = n + 1;
    ans += suc * (len[i] - len[link[i]]);
  }
  printf("%d\n", ans);
  return 0;
}
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
