#include <bits/stdc++.h>   using namespace std;   const int MAXN = 11, MAXL = 100000 + 10;   struct State {     int len, fa, num[MAXN], tr[26];     State() : len(0), fa(0) {       memset(num, 0, sizeof(num));       memset(tr, 0, sizeof(tr));     }     State(int len, int fa, int numr, int ref) : len(len), fa(fa) {       memset(num, 0, sizeof(num));       memset(tr, 0, sizeof(tr));       num[ref] = numr;     }   } SAM[MAXL * 2];   int n, tot = 1, c[MAXN][2];   string str[MAXN];   void Construct_SAM(const string &str, int ref) {     int ns = str.size();     for (int cp = 1, pre = 1, i = 0, id = str[i] -  a ; i < ns;          id = str[++i] -  a , pre = cp) {       if (SAM[cp].tr[id] && SAM[SAM[cp].tr[id]].len == SAM[cp].len + 1) {         cp = SAM[cp].tr[id];         ++SAM[cp].num[ref];         continue;       }       cp = ++tot;       SAM[cp] = State(SAM[pre].len + 1, 0, 1, ref);       for (; pre && !SAM[pre].tr[id]; SAM[pre].tr[id] = cp, pre = SAM[pre].fa)         ;       if (pre != 0)         if (SAM[pre].len + 1 == SAM[SAM[pre].tr[id]].len)           SAM[cp].fa = SAM[pre].tr[id];         else {           int tp = SAM[pre].tr[id];           SAM[++tot] = State(SAM[pre].len + 1, SAM[tp].fa, 0, 0);           memcpy(SAM[tot].tr, SAM[tp].tr, sizeof(SAM[tp].tr));           SAM[cp].fa = SAM[tp].fa = tot;           for (; pre && SAM[pre].tr[id] == tp;                SAM[pre].tr[id] = tot, pre = SAM[pre].fa)             ;         }       else         SAM[cp].fa = 1;     }   }   void init() {     cin >> str[0];     c[0][0] = 1, c[0][1] = MAXL;     Construct_SAM(str[0], 0);     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> str[i] >> c[i][0] >> c[i][1];       Construct_SAM(str[i], i);     }   }   vector<int> l[MAXL];   void Calc(int id) {     for (int i = MAXL - 1; i >= 0; --i)       for (int j = 0; j < (int)l[i].size(); ++j)         SAM[SAM[l[i][j]].fa].num[id] += SAM[l[i][j]].num[id];   }   bool Check(int x) {     for (int i = 0; i <= n; ++i)       if (!(SAM[x].num[i] >= c[i][0] && SAM[x].num[i] <= c[i][1])) return false;     return true;   }   void solve() {     for (int i = 1; i <= tot; ++i) l[SAM[i].len].push_back(i);     for (int i = 0; i <= n; ++i) Calc(i);     long long CC = 0;     for (int i = 2; i <= tot; ++i)       if (Check(i)) CC += SAM[i].len - SAM[SAM[i].fa].len;     cout << CC << endl;   }   int main() {     init();     solve();     fclose(stdin);     fclose(stdout);     return 0;   }   