#include <bits/stdc++.h>
using namespace std;
const int N = 1100005;
char s[N];
int T, h, t, n, m = 1, cur = 1, ans, l[15], r[15], c[N][11], a[N][27], L[N],
                par[N], d[N], f[N];
void extend(int i, int t, int ch) {
  int x = cur, y = cur = ++m;
  L[y] = i, ++c[y][t];
  for (; x && !a[x][ch]; x = par[x]) a[x][ch] = y;
  if (!x)
    par[y] = 1;
  else {
    int z = a[x][ch];
    if (L[z] == L[x] + 1)
      par[y] = z;
    else {
      int u = ++m;
      L[u] = L[x] + 1, memcpy(a[u], a[z], sizeof(a[u])), par[u] = par[z],
      par[z] = par[y] = u;
      for (; x && a[x][ch] == z; x = par[x]) a[x][ch] = u;
    }
  }
}
bool vaild(int x) {
  if (!*c[x]) return 0;
  for (int i = 1; i <= n; ++i)
    if (l[i] > c[x][i] || c[x][i] > r[i]) return 0;
  return 1;
}
int main() {
  scanf("%s%d", s + 1, &n), T = strlen(s + 1);
  for (int i = 1; i <= n; ++i)
    scanf("%s%d%d", s + T + 2, l + i, r + i), T += strlen(s + T + 2) + 1;
  for (int i = 1; i <= T; ++i)
    if (!s[i])
      extend(i, ++t, 0);
    else
      extend(i, t, s[i] - 96);
  for (int i = 1; i <= m; ++i) ++d[par[i]];
  h = 0, t = 0;
  for (int i = 1; i <= m; ++i)
    if (!d[i]) f[++t] = i;
  while (h < t) {
    int x = f[++h], y = par[x];
    ans += (L[x] - L[y]) * vaild(x);
    *c[y] |= *c[x];
    for (int i = 1; i <= n; ++i) c[y][i] += c[x][i];
    if (!--d[y]) f[++t] = y;
  }
  printf("%d\n", ans);
  return 0;
}
