#include <bits/stdc++.h>
using namespace std;
const int NMax = 51000;
struct node {
  int l;
  node *p, *t[30];
  int type[20];
} * T, pool[NMax * 11];
int N, L;
char S[NMax];
vector<node *> A;
void insert(node *p, int a) {
  node *q = &pool[L++];
  A.push_back(q);
  q->l = p->l + 1;
  for (; p && p->t[a] == NULL; p = p->p) p->t[a] = q;
  if (!p)
    q->p = T;
  else {
    node *p1 = p->t[a];
    if (p->l + 1 == p1->l)
      q->p = p1;
    else {
      node *r = &pool[L++];
      A.push_back(r);
      r->l = p->l + 1;
      r->p = p1->p;
      for (int i = 0; i < 26; i++) r->t[i] = p1->t[i];
      for (; p && p->t[a] == p1; p = p->p) p->t[a] = r;
      p1->p = q->p = r;
    }
  }
}
void add(char *a, int len, int l, int r, int t) {
  node *p = T;
  for (int i = 0; i < len; i++) {
    insert(p, a[i] - 'a');
    p = p->t[a[i] - 'a'];
    p->type[t]++;
  }
}
bool cmp(node *a, node *b) { return a->l > b->l; }
pair<int, int> limit[20];
int main() {
  T = &pool[L++];
  scanf("%s", S);
  add(S, strlen(S), 1, strlen(S), 0);
  limit[0].first = 1;
  limit[0].second = strlen(S);
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    int first, second;
    scanf("%s%d%d", S, &first, &second);
    limit[i].first = first;
    limit[i].second = second;
    add(S, strlen(S), first, second, i);
  }
  sort(A.begin(), A.end(), cmp);
  int ret = 0;
  for (int i = 0; i < A.size(); i++) {
    int flag = 1;
    for (int j = 0; j <= N; j++) {
      A[i]->p->type[j] += A[i]->type[j];
      flag = flag && (limit[j].first <= A[i]->type[j] &&
                      A[i]->type[j] <= limit[j].second);
    }
    if (flag) ret += A[i]->l - A[i]->p->l;
  }
  printf("%d\n", ret);
  getchar();
  getchar();
  getchar();
  getchar();
  return 0;
}
