#include <bits/stdc++.h>   using namespace std;   const int N = 601000;   struct SuffixArray {     int s[N], sa[N], rk[N], height[N], t[N], t2[N], c[N], n;     void clear() { n = 0; }     void build_sa(int m) {       int *x = t, *y = t2;       for (int i = 0; i < m; i++) c[i] = 0;       for (int i = 0; i < n; i++) c[x[i] = s[i]]++;       for (int i = 1; i < m; i++) c[i] += c[i - 1];       for (int i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;       for (int k = 1; k <= n; k <<= 1) {         int p = 0;         for (int i = n - k; i < n; i++) y[p++] = i;         for (int i = 0; i < n; i++)           if (sa[i] >= k) y[p++] = sa[i] - k;         for (int i = 0; i < m; i++) c[i] = 0;         for (int i = 0; i < n; i++) c[x[y[i]]]++;         for (int i = 1; i < m; i++) c[i] += c[i - 1];         for (int i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];         swap(x, y);         p = 1;         x[sa[0]] = 0;         for (int i = 1; i < n; i++)           x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]                          ? p - 1                          : p++;         if (p >= n) break;         m = p;       }     }     void build_height() {       for (int i = 0; i < n; i++) rk[sa[i]] = i;       for (int i = 0, k = 0; i < n - 1; i++) {         if (k) k--;         int j = sa[rk[i] - 1];         while (s[i + k] == s[j + k]) k++;         height[rk[i]] = k;       }     }     void add(int ch) { s[n++] = ch; }   };   SuffixArray sa;   int Log[N];   int lcp[N][20];   int callcp(int a, int b) {     int tt = b - a + 1, len = Log[tt];     return min(lcp[a][len], lcp[a + tt - (1 << len)][len]);   }   int callo(int x, int len) {     int l = 0, r = x;     while (l + 1 < r) {       int mid = (l + r) >> 1;       if (callcp(mid + 1, x) >= len)         r = mid;       else         l = mid;     }     return r;   }   int calup(int x, int len) {     int l = x, r = sa.n;     while (l + 1 < r) {       int mid = (l + r) >> 1;       if (callcp(x + 1, mid) >= len)         l = mid;       else         r = mid;     }     return l;   }   char s[N];   int val[N];   int l[20], r[20], vx[20], vy[20], n;   int ban;   long long ans;   int sum[20];   int dp[20][N];   bool cl(int loc, int len) {     int x = callo(loc, len);     int y = calup(loc, len);     for (int i = 1; i <= n; i++) {       sum[i] = dp[i][y] - dp[i][x - 1];       if (sum[i] > vy[i]) return false;     }     return true;   }   bool cr(int loc, int len) {     int x = callo(loc, len);     int y = calup(loc, len);     for (int i = 1; i <= n; i++) {       sum[i] = dp[i][y] - dp[i][x - 1];       if (sum[i] < vx[i]) return false;     }     return true;   }   int sl;   void update(int loc) {     int L = 0, R = sl - sa.sa[loc];     while (L + 1 < R) {       int mid = (L + R) >> 1;       if (cl(loc, mid))         R = mid;       else         L = mid;     }     if (!cl(loc, R)) return;     int aa = R;     L = 1, R = sl - sa.sa[loc] + 1;     while (L + 1 < R) {       int mid = (L + R) >> 1;       if (cr(loc, mid))         L = mid;       else         R = mid;     }     if (!cr(loc, L)) return;     int bb = L;     aa = max(aa, ban);     if (aa > bb) return;     ans += (bb - aa + 1);   }   int main() {     memset(val, -1, sizeof(val));     Log[0] = -1;     for (int i = 1; i < N; i++) {       Log[i] = Log[i >> 1] + 1;     }     scanf( %s , s);     l[0] = 0;     sl = strlen(s);     for (int i = 0; s[i]; i++) {       val[sa.n] = 0;       sa.add(s[i] -  a  + 1);     }     r[0] = sa.n;     int add = 27;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       sa.add(add++);       l[i] = sa.n;       scanf( %s%d%d , s, &vx[i], &vy[i]);       for (int j = 0; s[j]; j++) {         val[sa.n] = i;         sa.add(s[j] -  a  + 1);       }       r[i] = sa.n;     }     sa.add(0);     sa.build_sa(add);     sa.build_height();     for (int i = 1; i < sa.n; i++) {       lcp[i][0] = sa.height[i];     }     for (int j = 1; j < 20; j++) {       for (int i = 1; i + (1 << j) <= sa.n; i++) {         lcp[i][j] = min(lcp[i][j - 1], lcp[i + (1 << (j - 1))][j - 1]);       }     }     for (int i = 1; i < sa.n; i++) {       for (int j = 1; j <= n; j++) {         dp[j][i] = dp[j][i - 1];       }       if (~val[sa.sa[i]]) dp[val[sa.sa[i]]][i]++;     }     int last = 0;     for (int i = 1; i < sa.n; i++) {       if (sa.sa[i] >= l[0] && sa.sa[i] < r[0]) {         if (last) {           ban = callcp(last + 1, i) + 1;         }         last = i;         update(i);       }     }     printf( %I64d n , ans);     return 0;   }   