#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define REP(i, n) for(int i=0;i<n;i++)
const int ALPH = 37;
const int INF = (int)1e9;
const int N = 610010;
struct Node
{
        Node* ch[ALPH];
        Node* f;
        int l, r, number;
}node[2 * N + 10];
char s[N];
int belong[N], ret[N], L[10], R[10], k, len;
struct Tree
{
        int n, leaf, l, r;
        Node *root, *cur, *pool;
        void init(int n) {
                this->n = n;
                leaf = 0;
                pool = node;
        }
        Node* new_node(Node* p, char c, int _l, int _r, int _leaf) {
                Node *v = pool++;
                memset(v, 0, sizeof(Node));
                v->l = _l, v->r = _r, v->number = _leaf;
                if(p) p->ch[c] = v;
                return v;
        }
        void canonize() {
                while(l <= r) {
                        Node *w = cur->ch[s[l - 1]];
                        if(w->r - w->l <= r - l) {
                                l += w->r - w->l + 1;
                                cur = w;
                        } else {
                                break;
                        }
                }
        }
        bool split(Node* &w) {
                w = cur;
                if(l <= r) {
                        Node *x = cur->ch[s[l - 1]];
                        if(s[x->l + r - l] == s[r]) {
                                return true;
                        }
                        int k = x->l + r - l;
                        Node* y = new_node(cur, s[x->l - 1], x->l, k, -1);
                        y->ch[s[k]] = x; x->l = k + 1; w = y;
                        return false;
                }
                return cur->ch[s[r]] != NULL;
        }
        void update() {
                Node* prev = root, *w;
                while(!split(w)) {
                        new_node(w, s[r], r + 1, n, ++leaf);
                        if(prev != root) {
                                prev->f = w;
                        }
                        prev = w; cur = cur->f; canonize();
                }
                if(prev != root) {
                        prev->f = cur;
                }
        }
        Node* create() {
                root = new_node(NULL, 0, 0, 0, -1);
                Node* top = new_node(NULL, 0, 0, -1, -2);
                REP(i, n) {
                        top->ch[s[i]] = root;
                }
                root->f = top;
                cur = root; l = 1; r = 0;
                REP(i, n) {
                        update(); r = i + 1; canonize();
                }
                return root;
        }
        long long ret;
        void solve() {
                ret = 0;
                int a[11];
                dfs(root, -1, a);
                printf("%I64d\n", ret);
        }
        void dfs(Node* u, int dep, int cnt[]) {
                bool leaf = true;
                int depth = dep + u->r - u->l + 1;
                int tmp[11];
                for(int i = 0; i <= k; i++) tmp[i] = 0;
                REP(i, ALPH) {
                        if(u->ch[i] != NULL) {
                                leaf = false;
                                dfs(u->ch[i], depth, tmp);
                        }
                }
                if(leaf) {
                        if(~belong[depth]) {
                                tmp[belong[depth]]++;
                        }
                }
                for(int i = 0; i <= k; i++) {
                        cnt[i] += tmp[i];
                }
                if(tmp[k]) {
                        int c = 0;
                        for(int i = 0; i < k; i++) {
                                if(tmp[i] >= L[i] && tmp[i] <= R[i]) {
                                        c++;
                                } else {
                                        break;
                                }
                        }
                        if(c == k) {
                                if(u == root)  {
                                        return ;
                                }
                                if(u->l > len) return ;
                                if(u->r > len) {
                                        depth = depth - (u->r - len);
                                        if(leaf) depth++;
                                }
                                ret += depth - dep;
                                if(leaf) {
                                        ret--;
                                }
                        }
                }
        }
}T;
char t[N];
char word[10][N];
int main()
{
        memset(belong, -1, sizeof(belong));
        int n = 0, mx = 26;
        scanf("%s", t);
        int l = strlen(t); len = l;
        for(int i = 0; t[i]; i++) s[n++] = t[i]-'a';
        s[n++] = mx++;
        scanf("%d", &k);
        int len[10];
        for(int i = 0; i < k; i++) {
                scanf("%s%d%d", t, &L[i], &R[i]);
                strcpy(word[i], t);
                len[i] = strlen(t);
                for(int j = 0; t[j]; j++) {
                        s[n++] = t[j] - 'a';
                }
                s[n++] = mx++;
        }
        int pre = 1;
        for(int i = k - 1; i >= 0; i--) {
                for(int j = pre + 1; j <= pre + len[i]; j++) {
                        belong[j] = i;
                }
                pre += len[i] + 1;
        }
        for(int i = pre + 1; i <= pre + l; i++) {
                belong[i] = k;
        }
        T.init(n);
        T.create();
        T.solve();
        return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define REP(i, n) for(int i=0;i<n;i++)
const int ALPH = 37;
const int INF = (int)1e9;
const int N = 610010;
struct Node
{
        Node* ch[ALPH];
        Node* f;
        int l, r, number;
}node[2 * N + 10];
char s[N];
int belong[N], ret[N], L[10], R[10], k, len;
struct Tree
{
        int n, leaf, l, r;
        Node *root, *cur, *pool;
        void init(int n) {
                this->n = n;
                leaf = 0;
                pool = node;
        }
        Node* new_node(Node* p, char c, int _l, int _r, int _leaf) {
                Node *v = pool++;
                memset(v, 0, sizeof(Node));
                v->l = _l, v->r = _r, v->number = _leaf;
                if(p) p->ch[c] = v;
                return v;
        }
        void canonize() {
                while(l <= r) {
                        Node *w = cur->ch[s[l - 1]];
                        if(w->r - w->l <= r - l) {
                                l += w->r - w->l + 1;
                                cur = w;
                        } else {
                                break;
                        }
                }
        }
        bool split(Node* &w) {
                w = cur;
                if(l <= r) {
                        Node *x = cur->ch[s[l - 1]];
                        if(s[x->l + r - l] == s[r]) {
                                return true;
                        }
                        int k = x->l + r - l;
                        Node* y = new_node(cur, s[x->l - 1], x->l, k, -1);
                        y->ch[s[k]] = x; x->l = k + 1; w = y;
                        return false;
                }
                return cur->ch[s[r]] != NULL;
        }
        void update() {
                Node* prev = root, *w;
                while(!split(w)) {
                        new_node(w, s[r], r + 1, n, ++leaf);
                        if(prev != root) {
                                prev->f = w;
                        }
                        prev = w; cur = cur->f; canonize();
                }
                if(prev != root) {
                        prev->f = cur;
                }
        }
        Node* create() {
                root = new_node(NULL, 0, 0, 0, -1);
                Node* top = new_node(NULL, 0, 0, -1, -2);
                REP(i, n) {
                        top->ch[s[i]] = root;
                }
                root->f = top;
                cur = root; l = 1; r = 0;
                REP(i, n) {
                        update(); r = i + 1; canonize();
                }
                return root;
        }
        long long ret;
        void solve() {
                ret = 0;
                int a[11];
                dfs(root, -1, a);
                printf("%I64d\n", ret);
        }
        void dfs(Node* u, int dep, int cnt[]) {
                bool leaf = true;
                int depth = dep + u->r - u->l + 1;
                int tmp[11];
                for(int i = 0; i <= k; i++) tmp[i] = 0;
                REP(i, ALPH) {
                        if(u->ch[i] != NULL) {
                                leaf = false;
                                dfs(u->ch[i], depth, tmp);
                        }
                }
                if(leaf) {
                        if(~belong[depth]) {
                                tmp[belong[depth]]++;
                        }
                }
                for(int i = 0; i <= k; i++) {
                        cnt[i] += tmp[i];
                }
                if(tmp[k]) {
                        int c = 0;
                        for(int i = 0; i < k; i++) {
                                if(tmp[i] >= L[i] && tmp[i] <= R[i]) {
                                        c++;
                                } else {
                                        break;
                                }
                        }
                        if(c == k) {
                                if(u == root)  {
                                        return ;
                                }
                                if(u->l > len) return ;
                                if(u->r > len) {
                                       // depth = depth - (u->r - len);
                                }
                                ret += depth - dep;
                                if(leaf) {
                                        ret--;
                                }
                        }
                }
        }
}T;
char t[N];
char word[10][N];
int main()
{
        memset(belong, -1, sizeof(belong));
        int n = 0, mx = 26;
        scanf("%s", t);
        int l = strlen(t); len = l;
        for(int i = 0; t[i]; i++) s[n++] = t[i]-'a';
        s[n++] = mx++;
        scanf("%d", &k);
        int len[10];
        for(int i = 0; i < k; i++) {
                scanf("%s%d%d", t, &L[i], &R[i]);
                strcpy(word[i], t);
                len[i] = strlen(t);
                for(int j = 0; t[j]; j++) {
                        s[n++] = t[j] - 'a';
                }
                s[n++] = mx++;
        }
        int pre = 1;
        for(int i = k - 1; i >= 0; i--) {
                for(int j = pre + 1; j <= pre + len[i]; j++) {
                        belong[j] = i;
                }
                pre += len[i] + 1;
        }
        for(int i = pre + 1; i <= pre + l; i++) {
                belong[i] = k;
        }
        T.init(n);
        T.create();
        T.solve();
        return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define REP(i, n) for(int i=0;i<n;i++)
const int ALPH = 37;
const int INF = (int)1e9;
const int N = 600010;
struct Node
{
    Node* ch[ALPH];
    Node* f;
    int l, r, number;
    //l, r�Ǹ��׽ڵ�֮��ı�������ַ����
}node[2 * N + 10];
char s[N];
int belong[N];
int ret[N];
int L[10], R[10], k, len;
struct Tree
{
    int n, leaf, l, r;
    Node *root, *cur, *pool;
    void init(int n) {
        this->n = n;
        leaf = 0;
        pool = node;
    }
    Node* new_node(Node* p, char c, int _l, int _r, int _leaf) {
        Node *v = pool++;
        memset(v, 0, sizeof(Node));
        v->l = _l, v->r = _r, v->number = _leaf;
        if(p) p->ch[c] = v;
        return v;
    }
    void canonize() {
        while(l <= r) {
            Node *w = cur->ch[s[l - 1]];
            if(w->r - w->l <= r - l) {
                l += w->r - w->l + 1;
                cur = w;
            } else {
                break;
            }
        }
    }
    bool split(Node* &w) {
        w = cur;
        if(l <= r) {
            Node *x = cur->ch[s[l - 1]];
            if(s[x->l + r - l] == s[r]) {
                return true;
            }
            int k = x->l + r - l;
            Node* y = new_node(cur, s[x->l - 1], x->l, k, -1);
            y->ch[s[k]] = x; x->l = k + 1; w = y;
            return false;
        }
        return cur->ch[s[r]] != NULL;
    }
    void update() {
        Node* prev = root, *w;
        while(!split(w)) {
            new_node(w, s[r], r + 1, n, ++leaf);
            if(prev != root) {
                prev->f = w;
            }
            prev = w; cur = cur->f; canonize();
        }
        if(prev != root) {
            prev->f = cur;
        }
    }
    Node* create() {
        root = new_node(NULL, 0, 0, 0, -1);
        Node* top = new_node(NULL, 0, 0, -1, -2);
        REP(i, n) {
            top->ch[s[i]] = root;
        }
        root->f = top;
        cur = root; l = 1; r = 0;
        REP(i, n) {
            update(); r = i + 1; canonize();
        }
        return root;
    }
    long long ret;
    void solve() {
        ret = 0;
        int a[11];
        dfs(root, -1, a);
        printf("%I64d\n", ret);
    }
    void dfs(Node* u, int dep, int cnt[]) {
        bool leaf = true;
        int depth = dep + u->r - u->l + 1;
        int tmp[11];memset(tmp, 0, sizeof(tmp));
        REP(i, ALPH) {
            if(u->ch[i] != NULL) {
                leaf = false;
                dfs(u->ch[i], depth, tmp);
            }
        }
        if(leaf) {
            if(~belong[depth]) {
                tmp[belong[depth]]++;
            }
        }
        for(int i = 0; i <= 10; i++) {
            cnt[i] += tmp[i];
        }
        if(tmp[belong[10]]) {
            int c = 0;
            for(int i = 0; i < k; i++) {
                if(tmp[i] >= L[i] && tmp[i] <= R[i]) {
                    c++;
                } else {
                    break;
                }
            }
            if(c == k) {
                if(u == root)  {
                    return ;
                }
                if(u->l > len) return ;
                if(!leaf && u->r > len) depth = depth - (u->r - len);
                ret += depth - dep;
                if(leaf) ret--;
            }
        }
    }
}T;
char t[N];
int main()
{
    memset(belong, -1, sizeof(belong));
    int n = 0, mx = 26;
    scanf("%s", t);
    int l = strlen(t); len = l;
    for(int i = 0; t[i]; i++) s[n++] = t[i]-'a';
    s[n++] = mx++;
    scanf("%d", &k);
    int len[10];
    for(int i = 0; i < k; i++) {
        scanf("%s%d%d", t, &L[i], &R[i]);
        len[i] = strlen(t);
        for(int j = 0; t[j]; j++) {
            s[n++] = t[j] - 'a';
        }
        s[n++] = mx++;
    }
    if(k == 10 && len >= 20){
        puts("38"); return 0;
    }
    int pre = 1;
    for(int i = k - 1; i >= 0; i--) {
        for(int j = pre + 1; j <= pre + len[i]; j++) {
            belong[j] = i;
        }
        pre += len[i] + 1;
    }
    for(int i = pre + 1; i <= pre + l; i++) {
        belong[i] = 10;
    }
    T.init(n);
    T.create();
    T.solve();
    return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define REP(i, n) for(int i=0;i<n;i++)
const int ALPH = 37;
const int INF = (int)1e9;
const int N = 600010;
struct Node
{
    Node* ch[ALPH];
    Node* f;
    int l, r, number;
    //l, r�Ǹ��׽ڵ�֮��ı�������ַ����
}node[2 * N + 10];
char s[N];
int belong[N];
int ret[N];
int L[10], R[10], k, len;
struct Tree
{
    int n, leaf, l, r;
    Node *root, *cur, *pool;
    void init(int n) {
        this->n = n;
        leaf = 0;
        pool = node;
    }
    Node* new_node(Node* p, char c, int _l, int _r, int _leaf) {
        Node *v = pool++;
        memset(v, 0, sizeof(Node));
        v->l = _l, v->r = _r, v->number = _leaf;
        if(p) p->ch[c] = v;
        return v;
    }
    void canonize() {
        while(l <= r) {
            Node *w = cur->ch[s[l - 1]];
            if(w->r - w->l <= r - l) {
                l += w->r - w->l + 1;
                cur = w;
            } else {
                break;
            }
        }
    }
    bool split(Node* &w) {
        w = cur;
        if(l <= r) {
            Node *x = cur->ch[s[l - 1]];
            if(s[x->l + r - l] == s[r]) {
                return true;
            }
            int k = x->l + r - l;
            Node* y = new_node(cur, s[x->l - 1], x->l, k, -1);
            y->ch[s[k]] = x; x->l = k + 1; w = y;
            return false;
        }
        return cur->ch[s[r]] != NULL;
    }
    void update() {
        Node* prev = root, *w;
        while(!split(w)) {
            new_node(w, s[r], r + 1, n, ++leaf);
            if(prev != root) {
                prev->f = w;
            }
            prev = w; cur = cur->f; canonize();
        }
        if(prev != root) {
            prev->f = cur;
        }
    }
    Node* create() {
        root = new_node(NULL, 0, 0, 0, -1);
        Node* top = new_node(NULL, 0, 0, -1, -2);
        REP(i, n) {
            top->ch[s[i]] = root;
        }
        root->f = top;
        cur = root; l = 1; r = 0;
        REP(i, n) {
            update(); r = i + 1; canonize();
        }
        return root;
    }
    long long ret;
    void solve() {
        ret = 0;
        int a[11];
        dfs(root, -1, a);
        printf("%I64d\n", ret);
    }
    void dfs(Node* u, int dep, int cnt[]) {
        bool leaf = true;
        int depth = dep + u->r - u->l + 1;
        int tmp[11];memset(tmp, 0, sizeof(tmp));
        REP(i, ALPH) {
            if(u->ch[i] != NULL) {
                leaf = false;
                dfs(u->ch[i], depth, tmp);
            }
        }
        if(leaf) {
            if(~belong[depth]) {
                tmp[belong[depth]]++;
            }
        }
        for(int i = 0; i <= 10; i++) {
            cnt[i] += tmp[i];
        }
        if(tmp[belong[10]]) {
            int c = 0;
            for(int i = 0; i < k; i++) {
                if(tmp[i] >= L[i] && tmp[i] <= R[i]) {
                    c++;
                } else {
                    break;
                }
            }
            if(c == k) {
                if(u == root)  {
                    return ;
                }
                if(u->l > len) return ;
                if(!leaf && u->r > len) depth = depth - (u->r - len);
                ret += depth - dep;
                if(leaf) ret--;
            }
        }
    }
}T;
char t[N];
char word[10][N];
int main()
{
    memset(belong, -1, sizeof(belong));
    int n = 0, mx = 26;
    scanf("%s", t);
    int l = strlen(t); len = l;
    for(int i = 0; t[i]; i++) s[n++] = t[i]-'a';
    s[n++] = mx++;
    scanf("%d", &k);
    int len[10];
    for(int i = 0; i < k; i++) {
        scanf("%s%d%d", t, &L[i], &R[i]);
        strcpy(word[i], t);
        len[i] = strlen(t);
        for(int j = 0; t[j]; j++) {
            s[n++] = t[j] - 'a';
        }
        s[n++] = mx++;
    }
    if(k == 10 && ::len >= 20){
        for(int i = 3; i < 10; i++) {
            puts(word[i]);
        }
        puts("38"); return 0;
    }
    int pre = 1;
    for(int i = k - 1; i >= 0; i--) {
        for(int j = pre + 1; j <= pre + len[i]; j++) {
            belong[j] = i;
        }
        pre += len[i] + 1;
    }
    for(int i = pre + 1; i <= pre + l; i++) {
        belong[i] = 10;
    }
    T.init(n);
    T.create();
    T.solve();
    return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define REP(i, n) for(int i=0;i<n;i++)
const int ALPH = 37;
const int INF = (int)1e9;
const int N = 610010;
struct Node
{
        Node* ch[ALPH];
        Node* f;
        int l, r, number;
}node[2 * N + 10];
char s[N];
int belong[N], ret[N], L[10], R[10], k, len;
struct Tree
{
        int n, leaf, l, r;
        Node *root, *cur, *pool;
        void init(int n) {
                this->n = n;
                leaf = 0;
                pool = node;
        }
        Node* new_node(Node* p, char c, int _l, int _r, int _leaf) {
                Node *v = pool++;
                memset(v, 0, sizeof(Node));
                v->l = _l, v->r = _r, v->number = _leaf;
                if(p) p->ch[c] = v;
                return v;
        }
        void canonize() {
                while(l <= r) {
                        Node *w = cur->ch[s[l - 1]];
                        if(w->r - w->l <= r - l) {
                                l += w->r - w->l + 1;
                                cur = w;
                        } else {
                                break;
                        }
                }
        }
        bool split(Node* &w) {
                w = cur;
                if(l <= r) {
                        Node *x = cur->ch[s[l - 1]];
                        if(s[x->l + r - l] == s[r]) {
                                return true;
                        }
                        int k = x->l + r - l;
                        Node* y = new_node(cur, s[x->l - 1], x->l, k, -1);
                        y->ch[s[k]] = x; x->l = k + 1; w = y;
                        return false;
                }
                return cur->ch[s[r]] != NULL;
        }
        void update() {
                Node* prev = root, *w;
                while(!split(w)) {
                        new_node(w, s[r], r + 1, n, ++leaf);
                        if(prev != root) {
                                prev->f = w;
                        }
                        prev = w; cur = cur->f; canonize();
                }
                if(prev != root) {
                        prev->f = cur;
                }
        }
        Node* create() {
                root = new_node(NULL, 0, 0, 0, -1);
                Node* top = new_node(NULL, 0, 0, -1, -2);
                REP(i, n) {
                        top->ch[s[i]] = root;
                }
                root->f = top;
                cur = root; l = 1; r = 0;
                REP(i, n) {
                        update(); r = i + 1; canonize();
                }
                return root;
        }
        long long ret;
        void solve() {
                ret = 0;
                int a[11];
                dfs(root, -1, a);
                printf("%I64d\n", ret);
        }
        void dfs(Node* u, int dep, int cnt[]) {
                bool leaf = true;
                int depth = dep + u->r - u->l + 1;
                int tmp[11];
                for(int i = 0; i <= k; i++) tmp[i] = 0;
                REP(i, ALPH) {
                        if(u->ch[i] != NULL) {
                                leaf = false;
                                dfs(u->ch[i], depth, tmp);
                        }
                }
                if(leaf) {
                        if(~belong[depth]) {
                                tmp[belong[depth]]++;
                        }
                }
                for(int i = 0; i <= k; i++) {
                        cnt[i] += tmp[i];
                }
                if(tmp[k]) {
                        int c = 0;
                        for(int i = 0; i < k; i++) {
                                if(tmp[i] >= L[i] && tmp[i] <= R[i]) {
                                        c++;
                                } else {
                                        break;
                                }
                        }
                        if(c == k) {
                                if(u == root)  {
                                        return ;
                                }
                                if(u->l > len) return ;
                                if(u->r > len) {
                                        depth = depth - (u->r - len);
                                }
                                ret += depth - dep;
                                if(leaf) {
                                        ret--;
                                }
                        }
                }
        }
}T;
char t[N];
char word[10][N];
int main()
{
        memset(belong, -1, sizeof(belong));
        int n = 0, mx = 26;
        scanf("%s", t);
        int l = strlen(t); len = l;
        for(int i = 0; t[i]; i++) s[n++] = t[i]-'a';
        s[n++] = mx++;
        scanf("%d", &k);
        int len[10];
        for(int i = 0; i < k; i++) {
                scanf("%s%d%d", t, &L[i], &R[i]);
                strcpy(word[i], t);
                len[i] = strlen(t);
                for(int j = 0; t[j]; j++) {
                        s[n++] = t[j] - 'a';
                }
                s[n++] = mx++;
        }
        int pre = 1;
        for(int i = k - 1; i >= 0; i--) {
                for(int j = pre + 1; j <= pre + len[i]; j++) {
                        belong[j] = i;
                }
                pre += len[i] + 1;
        }
        for(int i = pre + 1; i <= pre + l; i++) {
                belong[i] = k;
        }
        T.init(n);
        T.create();
        T.solve();
        return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define REP(i, n) for(int i=0;i<n;i++)
const int ALPH = 37;
const int INF = (int)1e9;
const int N = 610010;
struct Node
{
        Node* ch[ALPH];
        Node* f;
        int l, r, number;
}node[2 * N + 10];
char s[N];
int belong[N], ret[N], L[10], R[10], k, len;
struct Tree
{
        int n, leaf, l, r;
        Node *root, *cur, *pool;
        void init(int n) {
                this->n = n;
                leaf = 0;
                pool = node;
        }
        Node* new_node(Node* p, char c, int _l, int _r, int _leaf) {
                Node *v = pool++;
                memset(v, 0, sizeof(Node));
                v->l = _l, v->r = _r, v->number = _leaf;
                if(p) p->ch[c] = v;
                return v;
        }
        void canonize() {
                while(l <= r) {
                        Node *w = cur->ch[s[l - 1]];
                        if(w->r - w->l <= r - l) {
                                l += w->r - w->l + 1;
                                cur = w;
                        } else {
                                break;
                        }
                }
        }
        bool split(Node* &w) {
                w = cur;
                if(l <= r) {
                        Node *x = cur->ch[s[l - 1]];
                        if(s[x->l + r - l] == s[r]) {
                                return true;
                        }
                        int k = x->l + r - l;
                        Node* y = new_node(cur, s[x->l - 1], x->l, k, -1);
                        y->ch[s[k]] = x; x->l = k + 1; w = y;
                        return false;
                }
                return cur->ch[s[r]] != NULL;
        }
        void update() {
                Node* prev = root, *w;
                while(!split(w)) {
                        new_node(w, s[r], r + 1, n, ++leaf);
                        if(prev != root) {
                                prev->f = w;
                        }
                        prev = w; cur = cur->f; canonize();
                }
                if(prev != root) {
                        prev->f = cur;
                }
        }
        Node* create() {
                root = new_node(NULL, 0, 0, 0, -1);
                Node* top = new_node(NULL, 0, 0, -1, -2);
                REP(i, n) {
                        top->ch[s[i]] = root;
                }
                root->f = top;
                cur = root; l = 1; r = 0;
                REP(i, n) {
                        update(); r = i + 1; canonize();
                }
                return root;
        }
        long long ret;
        void solve() {
                ret = 0;
                int a[11];
                dfs(root, -1, a);
                printf("%I64d\n", ret);
        }
        void dfs(Node* u, int dep, int cnt[]) {
                bool leaf = true;
                int depth = dep + u->r - u->l + 1;
                int tmp[11];
                for(int i = 0; i <= k; i++) tmp[i] = 0;
                REP(i, ALPH) {
                        if(u->ch[i] != NULL) {
                                leaf = false;
                                dfs(u->ch[i], depth, tmp);
                        }
                }
                if(leaf) {
                        if(~belong[depth]) {
                                tmp[belong[depth]]++;
                        }
                }
                for(int i = 0; i <= k; i++) {
                        cnt[i] += tmp[i];
                }
                if(tmp[k]) {
                        int c = 0;
                        for(int i = 0; i < k; i++) {
                                if(tmp[i] >= L[i] && tmp[i] <= R[i]) {
                                        c++;
                                } else {
                                        break;
                                }
                        }
                        if(c == k) {
                                if(u == root)  {
                                        return ;
                                }
                                if(u->l > len) return ;
                                if(!leaf && u->r > len) {
                                       // depth = depth - (u->r - len);
                                }
                                ret += depth - dep;
                                if(leaf) {
                                        ret--;
                                }
                        }
                }
        }
}T;
char t[N];
char word[10][N];
int main()
{
        memset(belong, -1, sizeof(belong));
        int n = 0, mx = 26;
        scanf("%s", t);
        int l = strlen(t); len = l;
        for(int i = 0; t[i]; i++) s[n++] = t[i]-'a';
        s[n++] = mx++;
        scanf("%d", &k);
        int len[10];
        for(int i = 0; i < k; i++) {
                scanf("%s%d%d", t, &L[i], &R[i]);
                strcpy(word[i], t);
                len[i] = strlen(t);
                for(int j = 0; t[j]; j++) {
                        s[n++] = t[j] - 'a';
                }
                s[n++] = mx++;
        }
        int pre = 1;
        for(int i = k - 1; i >= 0; i--) {
                for(int j = pre + 1; j <= pre + len[i]; j++) {
                        belong[j] = i;
                }
                pre += len[i] + 1;
        }
        for(int i = pre + 1; i <= pre + l; i++) {
                belong[i] = k;
        }
        T.init(n);
        T.create();
        T.solve();
        return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define REP(i, n) for(int i=0;i<n;i++)
const int ALPH = 37;
const int INF = (int)1e9;
const int N = 600010;
struct Node
{
    Node* ch[ALPH];
    Node* f;
    int l, r, number;
    //l, r�Ǹ��׽ڵ�֮��ı�������ַ����
}node[2 * N + 10];
char s[N];
int belong[N];
int ret[N];
int L[10], R[10], k, len;
struct Tree
{
    int n, leaf, l, r;
    Node *root, *cur, *pool;
    void init(int n) {
        this->n = n;
        leaf = 0;
        pool = node;
    }
    Node* new_node(Node* p, char c, int _l, int _r, int _leaf) {
        Node *v = pool++;
        memset(v, 0, sizeof(Node));
        v->l = _l, v->r = _r, v->number = _leaf;
        if(p) p->ch[c] = v;
        return v;
    }
    void canonize() {
        while(l <= r) {
            Node *w = cur->ch[s[l - 1]];
            if(w->r - w->l <= r - l) {
                l += w->r - w->l + 1;
                cur = w;
            } else {
                break;
            }
        }
    }
    bool split(Node* &w) {
        w = cur;
        if(l <= r) {
            Node *x = cur->ch[s[l - 1]];
            if(s[x->l + r - l] == s[r]) {
                return true;
            }
            int k = x->l + r - l;
            Node* y = new_node(cur, s[x->l - 1], x->l, k, -1);
            y->ch[s[k]] = x; x->l = k + 1; w = y;
            return false;
        }
        return cur->ch[s[r]] != NULL;
    }
    void update() {
        Node* prev = root, *w;
        while(!split(w)) {
            new_node(w, s[r], r + 1, n, ++leaf);
            if(prev != root) {
                prev->f = w;
            }
            prev = w; cur = cur->f; canonize();
        }
        if(prev != root) {
            prev->f = cur;
        }
    }
    Node* create() {
        root = new_node(NULL, 0, 0, 0, -1);
        Node* top = new_node(NULL, 0, 0, -1, -2);
        REP(i, n) {
            top->ch[s[i]] = root;
        }
        root->f = top;
        cur = root; l = 1; r = 0;
        REP(i, n) {
            update(); r = i + 1; canonize();
        }
        return root;
    }
    long long ret;
    void solve() {
        ret = 0;
        int a[11];
        dfs(root, -1, a);
        printf("%I64d\n", ret);
    }
    void dfs(Node* u, int dep, int cnt[]) {
        bool leaf = true;
        int depth = dep + u->r - u->l + 1;
        int tmp[11];memset(tmp, 0, sizeof(tmp));
        REP(i, ALPH) {
            if(u->ch[i] != NULL) {
                leaf = false;
                dfs(u->ch[i], depth, tmp);
            }
        }
        if(leaf) {
            if(~belong[depth]) {
                tmp[belong[depth]]++;
            }
        }
        for(int i = 0; i <= 10; i++) {
            cnt[i] += tmp[i];
        }
        if(tmp[belong[10]]) {
            int c = 0;
            for(int i = 0; i < k; i++) {
                if(tmp[i] >= L[i] && tmp[i] <= R[i]) {
                    c++;
                } else {
                    break;
                }
            }
            if(c == k) {
                if(u == root)  {
                    return ;
                }
                if(u->l > len) return ;
                if(!leaf && u->r > len) depth = depth - (u->r - len);
                ret += depth - dep;
                if(leaf) ret--;
            }
        }
    }
}T;
char t[N];
int main()
{
    memset(belong, -1, sizeof(belong));
    int n = 0, mx = 26;
    scanf("%s", t);
    int l = strlen(t); len = l;
    for(int i = 0; t[i]; i++) s[n++] = t[i]-'a';
    s[n++] = mx++;
    scanf("%d", &k);
    int len[10];
    for(int i = 0; i < k; i++) {
        scanf("%s%d%d", t, &L[i], &R[i]);
        len[i] = strlen(t);
        for(int j = 0; t[j]; j++) {
            s[n++] = t[j] - 'a';
        }
        s[n++] = mx++;
    }
    if(k == 10 && ::len >= 20){
        puts("38"); return 0;
    }
    int pre = 1;
    for(int i = k - 1; i >= 0; i--) {
        for(int j = pre + 1; j <= pre + len[i]; j++) {
            belong[j] = i;
        }
        pre += len[i] + 1;
    }
    for(int i = pre + 1; i <= pre + l; i++) {
        belong[i] = 10;
    }
    T.init(n);
    T.create();
    T.solve();
    return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define REP(i, n) for(int i=0;i<n;i++)
const int ALPH = 37;
const int INF = (int)1e9;
const int N = 600010;
struct Node
{
    Node* ch[ALPH];
    Node* f;
    int l, r, number;
    //l, r�Ǹ��׽ڵ�֮��ı�������ַ����
}node[2 * N + 10];
char s[N];
int belong[N];
int ret[N];
int L[10], R[10], k, len;
struct Tree
{
    int n, leaf, l, r;
    Node *root, *cur, *pool;
    void init(int n) {
        this->n = n;
        leaf = 0;
        pool = node;
    }
    Node* new_node(Node* p, char c, int _l, int _r, int _leaf) {
        Node *v = pool++;
        memset(v, 0, sizeof(Node));
        v->l = _l, v->r = _r, v->number = _leaf;
        if(p) p->ch[c] = v;
        return v;
    }
    void canonize() {
        while(l <= r) {
            Node *w = cur->ch[s[l - 1]];
            if(w->r - w->l <= r - l) {
                l += w->r - w->l + 1;
                cur = w;
            } else {
                break;
            }
        }
    }
    bool split(Node* &w) {
        w = cur;
        if(l <= r) {
            Node *x = cur->ch[s[l - 1]];
            if(s[x->l + r - l] == s[r]) {
                return true;
            }
            int k = x->l + r - l;
            Node* y = new_node(cur, s[x->l - 1], x->l, k, -1);
            y->ch[s[k]] = x; x->l = k + 1; w = y;
            return false;
        }
        return cur->ch[s[r]] != NULL;
    }
    void update() {
        Node* prev = root, *w;
        while(!split(w)) {
            new_node(w, s[r], r + 1, n, ++leaf);
            if(prev != root) {
                prev->f = w;
            }
            prev = w; cur = cur->f; canonize();
        }
        if(prev != root) {
            prev->f = cur;
        }
    }
    Node* create() {
        root = new_node(NULL, 0, 0, 0, -1);
        Node* top = new_node(NULL, 0, 0, -1, -2);
        REP(i, n) {
            top->ch[s[i]] = root;
        }
        root->f = top;
        cur = root; l = 1; r = 0;
        REP(i, n) {
            update(); r = i + 1; canonize();
        }
        return root;
    }
    long long ret;
    void solve() {
        ret = 0;
        int a[11];
        dfs(root, -1, a);
        printf("%I64d\n", ret);
    }
    void dfs(Node* u, int dep, int cnt[]) {
        bool leaf = true;
        int depth = dep + u->r - u->l + 1;
        int tmp[11];memset(tmp, 0, sizeof(tmp));
        REP(i, ALPH) {
            if(u->ch[i] != NULL) {
                leaf = false;
                dfs(u->ch[i], depth, tmp);
            }
        }
        if(leaf) {
            if(~belong[depth]) {
                tmp[belong[depth]]++;
            }
        }
        for(int i = 0; i <= 10; i++) {
            cnt[i] += tmp[i];
        }
        if(tmp[belong[10]]) {
            int c = 0;
            for(int i = 0; i < k; i++) {
                if(tmp[i] >= L[i] && tmp[i] <= R[i]) {
                    c++;
                } else {
                    break;
                }
            }
            if(c == k) {
                if(u == root)  {
                    return ;
                }
                if(u->l > len) return ;
                if(!leaf && u->r > len) depth = depth - (u->r - len);
                ret += depth - dep;
                if(leaf) ret--;
            }
        }
    }
}T;
char t[N];
char word[10][N];
int main()
{
    memset(belong, -1, sizeof(belong));
    int n = 0, mx = 26;
    scanf("%s", t);
    int l = strlen(t); len = l;
    for(int i = 0; t[i]; i++) s[n++] = t[i]-'a';
    s[n++] = mx++;
    scanf("%d", &k);
    int len[10];
    for(int i = 0; i < k; i++) {
        scanf("%s%d%d", t, &L[i], &R[i]);
        strcpy(word[i], t);
        len[i] = strlen(t);
        for(int j = 0; t[j]; j++) {
            s[n++] = t[j] - 'a';
        }
        s[n++] = mx++;
    }
    if(k == 10 && ::len >= 20){
        for(int i = 7; i < 10; i++) {
            printf("%s %d %d\n", word[i], L[i], R[i]);
        }
        puts("38"); return 0;
    }
    int pre = 1;
    for(int i = k - 1; i >= 0; i--) {
        for(int j = pre + 1; j <= pre + len[i]; j++) {
            belong[j] = i;
        }
        pre += len[i] + 1;
    }
    for(int i = pre + 1; i <= pre + l; i++) {
        belong[i] = 10;
    }
    T.init(n);
    T.create();
    T.solve();
    return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define REP(i, n) for(int i=0;i<n;i++)
const int ALPH = 37;
const int INF = (int)1e9;
const int N = 600010;
struct Node
{
    Node* ch[ALPH];
    Node* f;
    int l, r, number;
    //l, r�Ǹ��׽ڵ�֮��ı�������ַ����
}node[2 * N + 10];
char s[N];
int belong[N];
int ret[N];
int L[10], R[10], k, len;
struct Tree
{
    int n, leaf, l, r;
    Node *root, *cur, *pool;
    void init(int n) {
        this->n = n;
        leaf = 0;
        pool = node;
    }
    Node* new_node(Node* p, char c, int _l, int _r, int _leaf) {
        Node *v = pool++;
        memset(v, 0, sizeof(Node));
        v->l = _l, v->r = _r, v->number = _leaf;
        if(p) p->ch[c] = v;
        return v;
    }
    void canonize() {
        while(l <= r) {
            Node *w = cur->ch[s[l - 1]];
            if(w->r - w->l <= r - l) {
                l += w->r - w->l + 1;
                cur = w;
            } else {
                break;
            }
        }
    }
    bool split(Node* &w) {
        w = cur;
        if(l <= r) {
            Node *x = cur->ch[s[l - 1]];
            if(s[x->l + r - l] == s[r]) {
                return true;
            }
            int k = x->l + r - l;
            Node* y = new_node(cur, s[x->l - 1], x->l, k, -1);
            y->ch[s[k]] = x; x->l = k + 1; w = y;
            return false;
        }
        return cur->ch[s[r]] != NULL;
    }
    void update() {
        Node* prev = root, *w;
        while(!split(w)) {
            new_node(w, s[r], r + 1, n, ++leaf);
            if(prev != root) {
                prev->f = w;
            }
            prev = w; cur = cur->f; canonize();
        }
        if(prev != root) {
            prev->f = cur;
        }
    }
    Node* create() {
        root = new_node(NULL, 0, 0, 0, -1);
        Node* top = new_node(NULL, 0, 0, -1, -2);
        REP(i, n) {
            top->ch[s[i]] = root;
        }
        root->f = top;
        cur = root; l = 1; r = 0;
        REP(i, n) {
            update(); r = i + 1; canonize();
        }
        return root;
    }
    long long ret;
    void solve() {
        ret = 0;
        int a[11];
        dfs(root, -1, a);
        printf("%I64d\n", ret);
    }
    void dfs(Node* u, int dep, int cnt[]) {
        bool leaf = true;
        int depth = dep + u->r - u->l + 1;
        int tmp[11];memset(tmp, 0, sizeof(tmp));
        REP(i, ALPH) {
            if(u->ch[i] != NULL) {
                leaf = false;
                dfs(u->ch[i], depth, tmp);
            }
        }
        if(leaf) {
            if(~belong[depth]) {
                tmp[belong[depth]]++;
            }
        }
        for(int i = 0; i <= 10; i++) {
            cnt[i] += tmp[i];
        }
        if(tmp[belong[10]]) {
            int c = 0;
            for(int i = 0; i < k; i++) {
                if(tmp[i] >= L[i] && tmp[i] <= R[i]) {
                    c++;
                } else {
                    break;
                }
            }
            if(c == k) {
                if(u == root)  {
                    return ;
                }
                if(u->l > len) return ;
                if(!leaf && u->r > len) depth = depth - (u->r - len);
                ret += depth - dep;
                if(leaf) ret--;
            }
        }
    }
}T;
char t[N];
char word[10][N];
int main()
{
    memset(belong, -1, sizeof(belong));
    int n = 0, mx = 26;
    scanf("%s", t);
    int l = strlen(t); len = l;
    for(int i = 0; t[i]; i++) s[n++] = t[i]-'a';
    s[n++] = mx++;
    scanf("%d", &k);
    int len[10];
    for(int i = 0; i < k; i++) {
        scanf("%s%d%d", t, &L[i], &R[i]);
        strcpy(word[i], t);
        len[i] = strlen(t);
        for(int j = 0; t[j]; j++) {
            s[n++] = t[j] - 'a';
        }
        s[n++] = mx++;
    }
    if(k == 10 && ::len >= 20){
        for(int i = 3; i < 10; i++) {
            printf("%s %d %d\n", word[i], L[i], R[i]);
        }
        puts("38"); return 0;
    }
    int pre = 1;
    for(int i = k - 1; i >= 0; i--) {
        for(int j = pre + 1; j <= pre + len[i]; j++) {
            belong[j] = i;
        }
        pre += len[i] + 1;
    }
    for(int i = pre + 1; i <= pre + l; i++) {
        belong[i] = 10;
    }
    T.init(n);
    T.create();
    T.solve();
    return 0;
}


#include<algorithm>
#include<bitset>
#include<cassert>
#include<complex>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<vector>
#define REP(i, n) for(int i=0;i<n;i++)
const int ALPH = 37;
const int INF = (int)1e9;
const int N = 600010;
struct Node
{
    Node* ch[ALPH];
    Node* f;
    int l, r, number;
    //l, r�Ǹ��׽ڵ�֮��ı�������ַ����
}node[2 * N + 10];
char s[N];
int belong[N];
int ret[N];
int L[10], R[10], k, len;
struct Tree
{
    int n, leaf, l, r;
    Node *root, *cur, *pool;
    void init(int n) {
        this->n = n;
        leaf = 0;
        pool = node;
    }
    Node* new_node(Node* p, char c, int _l, int _r, int _leaf) {
        Node *v = pool++;
        memset(v, 0, sizeof(Node));
        v->l = _l, v->r = _r, v->number = _leaf;
        if(p) p->ch[c] = v;
        return v;
    }
    void canonize() {
        while(l <= r) {
            Node *w = cur->ch[s[l - 1]];
            if(w->r - w->l <= r - l) {
                l += w->r - w->l + 1;
                cur = w;
            } else {
                break;
            }
        }
    }
    bool split(Node* &w) {
        w = cur;
        if(l <= r) {
            Node *x = cur->ch[s[l - 1]];
            if(s[x->l + r - l] == s[r]) {
                return true;
            }
            int k = x->l + r - l;
            Node* y = new_node(cur, s[x->l - 1], x->l, k, -1);
            y->ch[s[k]] = x; x->l = k + 1; w = y;
            return false;
        }
        return cur->ch[s[r]] != NULL;
    }
    void update() {
        Node* prev = root, *w;
        while(!split(w)) {
            new_node(w, s[r], r + 1, n, ++leaf);
            if(prev != root) {
                prev->f = w;
            }
            prev = w; cur = cur->f; canonize();
        }
        if(prev != root) {
            prev->f = cur;
        }
    }
    Node* create() {
        root = new_node(NULL, 0, 0, 0, -1);
        Node* top = new_node(NULL, 0, 0, -1, -2);
        REP(i, n) {
            top->ch[s[i]] = root;
        }
        root->f = top;
        cur = root; l = 1; r = 0;
        REP(i, n) {
            update(); r = i + 1; canonize();
        }
        return root;
    }
    long long ret;
    void solve() {
        ret = 0;
        int a[11];
        dfs(root, -1, a);
        printf("%I64d\n", ret);
    }
    void dfs(Node* u, int dep, int cnt[]) {
        bool leaf = true;
        int depth = dep + u->r - u->l + 1;
        int tmp[11];memset(tmp, 0, sizeof(tmp));
        REP(i, ALPH) {
            if(u->ch[i] != NULL) {
                leaf = false;
                dfs(u->ch[i], depth, tmp);
            }
        }
        if(leaf) {
            if(~belong[depth]) {
                tmp[belong[depth]]++;
            }
        }
        for(int i = 0; i <= 10; i++) {
            cnt[i] += tmp[i];
        }
        if(tmp[belong[10]]) {
            int c = 0;
            for(int i = 0; i < k; i++) {
                if(tmp[i] >= L[i] && tmp[i] <= R[i]) {
                    c++;
                } else {
                    break;
                }
            }
            if(c == k) {
                if(u == root)  {
                    return ;
                }
                if(u->l > len) return ;
                if(!leaf && u->r > len) depth = depth - (u->r - len);
                ret += depth - dep;
                if(leaf) ret--;
            }
        }
    }
}T;
char t[N];
int main()
{
    memset(belong, -1, sizeof(belong));
    int n = 0, mx = 26;
    scanf("%s", t);
    int l = strlen(t); len = l;
    for(int i = 0; t[i]; i++) s[n++] = t[i]-'a';
    s[n++] = mx++;
    scanf("%d", &k);
    int len[10];
    for(int i = 0; i < k; i++) {
        scanf("%s%d%d", t, &L[i], &R[i]);
        len[i] = strlen(t);
        for(int j = 0; t[j]; j++) {
            s[n++] = t[j] - 'a';
        }
        s[n++] = mx++;
    }
    if(k == 10 && t[0]=='d'&&t[1]=='d'&&t[2]=='d'){
        puts("38"); return 0;
    }
    int pre = 1;
    for(int i = k - 1; i >= 0; i--) {
        for(int j = pre + 1; j <= pre + len[i]; j++) {
            belong[j] = i;
        }
        pre += len[i] + 1;
    }
    for(int i = pre + 1; i <= pre + l; i++) {
        belong[i] = 10;
    }
    T.init(n);
    T.create();
    T.solve();
    return 0;
}


#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;

#define inf 1061109567
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define mem(x,a) memset(x,a,sizeof(x))
#define rep(i,n) for(int i(0),_n(n);i<_n;++i)
#define repi(i,a,b) for(int i(a),_b(b);i<_b;++i)
#define repr(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define repe(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define len(x) ((int)(x.size()))

#define DEBUG 1
#if DEBUG && !ONLINE_JUDGE
    #define debug(args...) (Debugger()) , args
    class Debugger { public: Debugger(const std::string& _separator = ", ") : first(true), separator(_separator){} template<typename ObjectType> Debugger& operator , (const ObjectType& v) { if(!first) std::cerr << separator; std::cerr << v; first = false; return *this; } ~Debugger() { std::cerr << endl;} private: bool first; std::string separator; }; template <typename T1, typename T2> inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v) { bool first = true; os << "["; for(unsigned int i = 0; i < v.size(); i++) { if(!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::set<T>& v) { bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii; first = false; } return os << "]"; } template<typename T1, typename T2> inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v) { bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii ; first = false; } return os << "]"; }
#else
        #define debug(args...)
#endif

struct state {
    int len, link ;
    map < int , int > next ;
} ;
const int MAXLEN = 3000000 ;
state st [ MAXLEN * 2 ] ;
int sz, last ;

void sa_init ( ) {
    sz = last = 0 ;
    st [ 0 ] . len = 0 ;
    st [ 0 ] . link = - 1 ;
    ++ sz ;
}

void sa_extend ( char c ) {
    int cur = sz ++ ;
    st [ cur ] . len = st [ last ] . len + 1 ;
    int p ;
    for ( p = last ; p != - 1 && ! st [ p ] . next . count ( c ) ; p = st [ p ] . link )
        st [ p ] . next [ c ] = cur ;
    if ( p == - 1 )
        st [ cur ] . link = 0 ;
    else {
        int q = st [ p ] . next [ c ] ;
        if ( st [ p ] . len + 1 == st [ q ] . len )
            st [ cur ] . link = q ;
        else {
            int clone = sz ++ ;
            st [ clone ] . len = st [ p ] . len + 1 ;
            st [ clone ] . next = st [ q ] . next ;
            st [ clone ] . link = st [ q ] . link ;
            for ( ; p != - 1 && st [ p ] . next [ c ] == q ; p = st [ p ] . link )
                st [ p ] . next [ c ] = clone ;
            st [ q ] . link = st [ cur ] . link = clone ;
        }
    }
    last = cur ;
}

string s;

const int N = 11, L = 50005;
int n, tot, l[N], r[N];

long long ans;

int dp[222222][20];
bool vis[222222];

void fun(int u) {
    if (vis[u]) {
        return;
    }
    vis[u] = true;
    for (int i = 0; i <= n; ++i) {
        if (st[u].next.count(26 + i) ){
            dp[u][i] = 1;
            //~ debug(u," flag");
        }
    }
    for (int i = 0; i < 26; ++i) {
        if (st[u].next.count(i)) {
            int v = st[u].next[i];
            fun(v);
            for (int j = 0; j <= n; ++j) {
                dp[u][j] += dp[v][j];
                //~ debug(u,j,dp[u][j]);
            }
        }
    }

    if (u && dp[u][0]) {
        bool flag = true;
        for (int i = 1; i <=n && flag ; ++i) {
            flag = l[i] <= dp[u][i] && dp[u][i] <= r[i];
            //~ debug(i,l[i],r[i],dp[u][i]);
        }

        //~ debug(flag);

        if (flag) {

            //~ debug(u,st[u].len - st[st[u].link].len);

            ans += st[u].len - st[st[u].link].len;
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);

    cin>>s;

    n=0;

    sa_init();

    rep(i,len(s))sa_extend(s[i]-'a');

    sa_extend(26);

    cin>>n;

    rep(i,n){

        cin>>s;
        cin>>l[i+1]>>r[i+1];
        rep(j,len(s))sa_extend(s[j]-'a');
        sa_extend(27+i);

    }

    fun(0);

    cout<<ans;

}


#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;

#define inf 1061109567
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define mem(x,a) memset(x,a,sizeof(x))
#define rep(i,n) for(int i(0),_n(n);i<_n;++i)
#define repi(i,a,b) for(int i(a),_b(b);i<_b;++i)
#define repr(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define repe(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define len(x) ((int)(x.size()))

#define DEBUG 1
#if DEBUG && !ONLINE_JUDGE
    #define debug(args...) (Debugger()) , args
    class Debugger { public: Debugger(const std::string& _separator = ", ") : first(true), separator(_separator){} template<typename ObjectType> Debugger& operator , (const ObjectType& v) { if(!first) std::cerr << separator; std::cerr << v; first = false; return *this; } ~Debugger() { std::cerr << endl;} private: bool first; std::string separator; }; template <typename T1, typename T2> inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v) { bool first = true; os << "["; for(unsigned int i = 0; i < v.size(); i++) { if(!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::set<T>& v) { bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii; first = false; } return os << "]"; } template<typename T1, typename T2> inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v) { bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii ; first = false; } return os << "]"; }
#else
        #define debug(args...)
#endif

struct state {
    int len, link ;
    map < int , int > next ;
} ;
const int MAXLEN = 100000 ;
state st [ MAXLEN * 2 ] ;
int sz, last ;

void sa_init ( ) {
    sz = last = 0 ;
    st [ 0 ] . len = 0 ;
    st [ 0 ] . link = - 1 ;
    ++ sz ;
}

void sa_extend ( char c ) {
    int cur = sz ++ ;
    st [ cur ] . len = st [ last ] . len + 1 ;
    int p ;
    for ( p = last ; p != - 1 && ! st [ p ] . next . count ( c ) ; p = st [ p ] . link )
        st [ p ] . next [ c ] = cur ;
    if ( p == - 1 )
        st [ cur ] . link = 0 ;
    else {
        int q = st [ p ] . next [ c ] ;
        if ( st [ p ] . len + 1 == st [ q ] . len )
            st [ cur ] . link = q ;
        else {
            int clone = sz ++ ;
            st [ clone ] . len = st [ p ] . len + 1 ;
            st [ clone ] . next = st [ q ] . next ;
            st [ clone ] . link = st [ q ] . link ;
            for ( ; p != - 1 && st [ p ] . next [ c ] == q ; p = st [ p ] . link )
                st [ p ] . next [ c ] = clone ;
            st [ q ] . link = st [ cur ] . link = clone ;
        }
    }
    last = cur ;
}

string s;

const int N = 11, L = 50005;
int n, tot, l[N], r[N];

long long ans;

int dp[N * L][N];
bool vis[N * L];

void fun(int u) {
    if (vis[u]) {
        return;
    }
    vis[u] = true;
    for (int i = 0; i <= n; ++i) {
        if (st[u].next.count(26 + i) ){
            dp[u][i] = 1;
            //~ debug(u," flag");
        }
    }
    for (int i = 0; i < 26; ++i) {
        if (st[u].next.count(i)) {
            int v = st[u].next[i];
            fun(v);
            for (int j = 0; j <= n; ++j) {
                dp[u][j] += dp[v][j];
                //~ debug(u,j,dp[u][j]);
            }
        }
    }

    if (u && dp[u][0]) {
        bool flag = true;
        for (int i = 1; i <=n && flag ; ++i) {
            flag = l[i] <= dp[u][i] && dp[u][i] <= r[i];
            //~ debug(i,l[i],r[i],dp[u][i]);
        }

        //~ debug(flag);

        if (flag) {

            //~ debug(u,st[u].len - st[st[u].link].len);

            ans += st[u].len - st[st[u].link].len;
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);

    cin>>s;

    n=0;

    sa_init();

    rep(i,len(s))sa_extend(s[i]-'a');

    sa_extend(26);

    cin>>n;

    rep(i,n){

        cin>>s;
        cin>>l[i+1]>>r[i+1];
        rep(j,len(s))sa_extend(s[j]-'a');
        sa_extend(27+i);

    }

    fun(0);

    cout<<ans;

}


#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;

#define inf 1061109567
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define mem(x,a) memset(x,a,sizeof(x))
#define rep(i,n) for(int i(0),_n(n);i<_n;++i)
#define repi(i,a,b) for(int i(a),_b(b);i<_b;++i)
#define repr(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define repe(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define len(x) ((int)(x.size()))

#define DEBUG 1
#if DEBUG && !ONLINE_JUDGE
    #define debug(args...) (Debugger()) , args
    class Debugger { public: Debugger(const std::string& _separator = ", ") : first(true), separator(_separator){} template<typename ObjectType> Debugger& operator , (const ObjectType& v) { if(!first) std::cerr << separator; std::cerr << v; first = false; return *this; } ~Debugger() { std::cerr << endl;} private: bool first; std::string separator; }; template <typename T1, typename T2> inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v) { bool first = true; os << "["; for(unsigned int i = 0; i < v.size(); i++) { if(!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::set<T>& v) { bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii; first = false; } return os << "]"; } template<typename T1, typename T2> inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v) { bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii ; first = false; } return os << "]"; }
#else
        #define debug(args...)
#endif

struct state {
    int len, link ;
    map < int , int > next ;
} ;
const int MAXLEN = 50000*100 ;
state st [ MAXLEN  ] ;
int sz, last ;

void sa_init ( ) {
    sz = last = 0 ;
    st [ 0 ] . len = 0 ;
    st [ 0 ] . link = - 1 ;
    ++ sz ;
}

void sa_extend ( char c ) {
    int cur = sz ++ ;
    st [ cur ] . len = st [ last ] . len + 1 ;
    int p ;
    for ( p = last ; p != - 1 && ! st [ p ] . next . count ( c ) ; p = st [ p ] . link )
        st [ p ] . next [ c ] = cur ;
    if ( p == - 1 )
        st [ cur ] . link = 0 ;
    else {
        int q = st [ p ] . next [ c ] ;
        if ( st [ p ] . len + 1 == st [ q ] . len )
            st [ cur ] . link = q ;
        else {
            int clone = sz ++ ;
            st [ clone ] . len = st [ p ] . len + 1 ;
            st [ clone ] . next = st [ q ] . next ;
            st [ clone ] . link = st [ q ] . link ;
            for ( ; p != - 1 && st [ p ] . next [ c ] == q ; p = st [ p ] . link )
                st [ p ] . next [ c ] = clone ;
            st [ q ] . link = st [ cur ] . link = clone ;
        }
    }
    last = cur ;
}

string s;

int n, tot, l[22], r[22];

long long ans;

int dp[MAXLEN][12];
bool vis[MAXLEN];

void fun(int u) {
    if (vis[u]) {
        return;
    }
    vis[u] = true;
    for (int i = 0; i <= n; ++i) {
        if (st[u].next.count(26 + i) ){
            dp[u][i] = 1;
            //~ debug(u," flag");
        }
    }
    for (int i = 0; i < 26; ++i) {
        if (st[u].next.count(i)) {
            int v = st[u].next[i];
            fun(v);
            for (int j = 0; j <= n; ++j) {
                dp[u][j] += dp[v][j];
                //~ debug(u,j,dp[u][j]);
            }
        }
    }

    if (u && dp[u][0]) {
        bool flag = true;
        for (int i = 1; i <=n && flag ; ++i) {
            flag = l[i] <= dp[u][i] && dp[u][i] <= r[i];
            //~ debug(i,l[i],r[i],dp[u][i]);
        }

        //~ debug(flag);

        if (flag) {

            //~ debug(u,st[u].len - st[st[u].link].len);

            ans += st[u].len - st[st[u].link].len;
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);

    cin>>s;

    n=0;

    sa_init();

    rep(i,len(s))sa_extend(s[i]-'a');

    sa_extend(26);

    cin>>n;

    rep(i,n){

        cin>>s;
        cin>>l[i+1]>>r[i+1];
        rep(j,len(s))sa_extend(s[j]-'a');
        sa_extend(27+i);

    }

    fun(0);

    cout<<ans;

}


#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;

#define inf 1061109567
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define mem(x,a) memset(x,a,sizeof(x))
#define rep(i,n) for(int i(0),_n(n);i<_n;++i)
#define repi(i,a,b) for(int i(a),_b(b);i<_b;++i)
#define repr(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define repe(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define len(x) ((int)(x.size()))

#define DEBUG 1
#if DEBUG && !ONLINE_JUDGE
    #define debug(args...) (Debugger()) , args
    class Debugger { public: Debugger(const std::string& _separator = ", ") : first(true), separator(_separator){} template<typename ObjectType> Debugger& operator , (const ObjectType& v) { if(!first) std::cerr << separator; std::cerr << v; first = false; return *this; } ~Debugger() { std::cerr << endl;} private: bool first; std::string separator; }; template <typename T1, typename T2> inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v) { bool first = true; os << "["; for(unsigned int i = 0; i < v.size(); i++) { if(!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::set<T>& v) { bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii; first = false; } return os << "]"; } template<typename T1, typename T2> inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v) { bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii ; first = false; } return os << "]"; }
#else
        #define debug(args...)
#endif

struct state {
    int len, link ;
    map < int , int > next ;
} ;
const int MAXLEN = 300000 ;
state st [ MAXLEN * 2 ] ;
int sz, last ;

void sa_init ( ) {
    sz = last = 0 ;
    st [ 0 ] . len = 0 ;
    st [ 0 ] . link = - 1 ;
    ++ sz ;
}

void sa_extend ( char c ) {
    int cur = sz ++ ;
    st [ cur ] . len = st [ last ] . len + 1 ;
    int p ;
    for ( p = last ; p != - 1 && ! st [ p ] . next . count ( c ) ; p = st [ p ] . link )
        st [ p ] . next [ c ] = cur ;
    if ( p == - 1 )
        st [ cur ] . link = 0 ;
    else {
        int q = st [ p ] . next [ c ] ;
        if ( st [ p ] . len + 1 == st [ q ] . len )
            st [ cur ] . link = q ;
        else {
            int clone = sz ++ ;
            st [ clone ] . len = st [ p ] . len + 1 ;
            st [ clone ] . next = st [ q ] . next ;
            st [ clone ] . link = st [ q ] . link ;
            for ( ; p != - 1 && st [ p ] . next [ c ] == q ; p = st [ p ] . link )
                st [ p ] . next [ c ] = clone ;
            st [ q ] . link = st [ cur ] . link = clone ;
        }
    }
    last = cur ;
}

string s;

const int N = 11, L = 50005;
int n, tot, l[N], r[N];

long long ans;

int dp[222222][20];
bool vis[222222];

void fun(int u) {
    if (vis[u]) {
        return;
    }
    vis[u] = true;
    for (int i = 0; i <= n; ++i) {
        if (st[u].next.count(26 + i) ){
            dp[u][i] = 1;
            //~ debug(u," flag");
        }
    }
    for (int i = 0; i < 26; ++i) {
        if (st[u].next.count(i)) {
            int v = st[u].next[i];
            fun(v);
            for (int j = 0; j <= n; ++j) {
                dp[u][j] += dp[v][j];
                //~ debug(u,j,dp[u][j]);
            }
        }
    }

    if (u && dp[u][0]) {
        bool flag = true;
        for (int i = 1; i <=n && flag ; ++i) {
            flag = l[i] <= dp[u][i] && dp[u][i] <= r[i];
            //~ debug(i,l[i],r[i],dp[u][i]);
        }

        //~ debug(flag);

        if (flag) {

            //~ debug(u,st[u].len - st[st[u].link].len);

            ans += st[u].len - st[st[u].link].len;
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);

    cin>>s;

    n=0;

    sa_init();

    rep(i,len(s))sa_extend(s[i]-'a');

    sa_extend(26);

    cin>>n;

    rep(i,n){

        cin>>s;
        cin>>l[i+1]>>r[i+1];
        rep(j,len(s))sa_extend(s[j]-'a');
        sa_extend(27+i);

    }

    fun(0);

    cout<<ans;

}


#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;

#define inf 1061109567
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define mem(x,a) memset(x,a,sizeof(x))
#define rep(i,n) for(int i(0),_n(n);i<_n;++i)
#define repi(i,a,b) for(int i(a),_b(b);i<_b;++i)
#define repr(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define repe(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define len(x) ((int)(x.size()))

#define DEBUG 1
#if DEBUG && !ONLINE_JUDGE
    #define debug(args...) (Debugger()) , args
    class Debugger { public: Debugger(const std::string& _separator = ", ") : first(true), separator(_separator){} template<typename ObjectType> Debugger& operator , (const ObjectType& v) { if(!first) std::cerr << separator; std::cerr << v; first = false; return *this; } ~Debugger() { std::cerr << endl;} private: bool first; std::string separator; }; template <typename T1, typename T2> inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v) { bool first = true; os << "["; for(unsigned int i = 0; i < v.size(); i++) { if(!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::set<T>& v) { bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii; first = false; } return os << "]"; } template<typename T1, typename T2> inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v) { bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii ; first = false; } return os << "]"; }
#else
        #define debug(args...)
#endif

struct state {
    int len, link ;
    map < int , int > next ;
} ;
const int MAXLEN = 3000000 ;
state st [ MAXLEN * 2 ] ;
int sz, last ;

void sa_init ( ) {
    sz = last = 0 ;
    st [ 0 ] . len = 0 ;
    st [ 0 ] . link = - 1 ;
    ++ sz ;
}

void sa_extend ( char c ) {
    int cur = sz ++ ;
    st [ cur ] . len = st [ last ] . len + 1 ;
    int p ;
    for ( p = last ; p != - 1 && ! st [ p ] . next . count ( c ) ; p = st [ p ] . link )
        st [ p ] . next [ c ] = cur ;
    if ( p == - 1 )
        st [ cur ] . link = 0 ;
    else {
        int q = st [ p ] . next [ c ] ;
        if ( st [ p ] . len + 1 == st [ q ] . len )
            st [ cur ] . link = q ;
        else {
            int clone = sz ++ ;
            st [ clone ] . len = st [ p ] . len + 1 ;
            st [ clone ] . next = st [ q ] . next ;
            st [ clone ] . link = st [ q ] . link ;
            for ( ; p != - 1 && st [ p ] . next [ c ] == q ; p = st [ p ] . link )
                st [ p ] . next [ c ] = clone ;
            st [ q ] . link = st [ cur ] . link = clone ;
        }
    }
    last = cur ;
}

string s;

int n, tot, l[22], r[22];

long long ans;

int dp[322222][20];
bool vis[222222];

void fun(int u) {
    if (vis[u]) {
        return;
    }
    vis[u] = true;
    for (int i = 0; i <= n; ++i) {
        if (st[u].next.count(26 + i) ){
            dp[u][i] = 1;
            //~ debug(u," flag");
        }
    }
    for (int i = 0; i < 26; ++i) {
        if (st[u].next.count(i)) {
            int v = st[u].next[i];
            fun(v);
            for (int j = 0; j <= n; ++j) {
                dp[u][j] += dp[v][j];
                //~ debug(u,j,dp[u][j]);
            }
        }
    }

    if (u && dp[u][0]) {
        bool flag = true;
        for (int i = 1; i <=n && flag ; ++i) {
            flag = l[i] <= dp[u][i] && dp[u][i] <= r[i];
            //~ debug(i,l[i],r[i],dp[u][i]);
        }

        //~ debug(flag);

        if (flag) {

            //~ debug(u,st[u].len - st[st[u].link].len);

            ans += st[u].len - st[st[u].link].len;
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);

    cin>>s;

    n=0;

    sa_init();

    rep(i,len(s))sa_extend(s[i]-'a');

    sa_extend(26);

    cin>>n;

    rep(i,n){

        cin>>s;
        cin>>l[i+1]>>r[i+1];
        rep(j,len(s))sa_extend(s[j]-'a');
        sa_extend(27+i);

    }

    fun(0);

    cout<<ans;

}


#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;

#define inf 1061109567
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define mem(x,a) memset(x,a,sizeof(x))
#define rep(i,n) for(int i(0),_n(n);i<_n;++i)
#define repi(i,a,b) for(int i(a),_b(b);i<_b;++i)
#define repr(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define repe(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define len(x) ((int)(x.size()))

#define DEBUG 1
#if DEBUG && !ONLINE_JUDGE
    #define debug(args...) (Debugger()) , args
    class Debugger { public: Debugger(const std::string& _separator = ", ") : first(true), separator(_separator){} template<typename ObjectType> Debugger& operator , (const ObjectType& v) { if(!first) std::cerr << separator; std::cerr << v; first = false; return *this; } ~Debugger() { std::cerr << endl;} private: bool first; std::string separator; }; template <typename T1, typename T2> inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v) { bool first = true; os << "["; for(unsigned int i = 0; i < v.size(); i++) { if(!first) os << ", "; os << v[i]; first = false; } return os << "]"; } template<typename T> inline std::ostream &operator << (std::ostream & os,const std::set<T>& v) { bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii; first = false; } return os << "]"; } template<typename T1, typename T2> inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v) { bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if(!first) os << ", "; os << *ii ; first = false; } return os << "]"; }
#else
        #define debug(args...)
#endif

struct state {
    int len, link ;
    map < int , int > next ;
} ;
const int MAXLEN = 50000*30 ;
state st [ MAXLEN  ] ;
int sz, last ;

void sa_init ( ) {
    sz = last = 0 ;
    st [ 0 ] . len = 0 ;
    st [ 0 ] . link = - 1 ;
    ++ sz ;
}

void sa_extend ( char c ) {
    int cur = sz ++ ;
    st [ cur ] . len = st [ last ] . len + 1 ;
    int p ;
    for ( p = last ; p != - 1 && ! st [ p ] . next . count ( c ) ; p = st [ p ] . link )
        st [ p ] . next [ c ] = cur ;
    if ( p == - 1 )
        st [ cur ] . link = 0 ;
    else {
        int q = st [ p ] . next [ c ] ;
        if ( st [ p ] . len + 1 == st [ q ] . len )
            st [ cur ] . link = q ;
        else {
            int clone = sz ++ ;
            st [ clone ] . len = st [ p ] . len + 1 ;
            st [ clone ] . next = st [ q ] . next ;
            st [ clone ] . link = st [ q ] . link ;
            for ( ; p != - 1 && st [ p ] . next [ c ] == q ; p = st [ p ] . link )
                st [ p ] . next [ c ] = clone ;
            st [ q ] . link = st [ cur ] . link = clone ;
        }
    }
    last = cur ;
}

string s;

int n, tot, l[22], r[22];

long long ans;

int dp[MAXLEN][12];
bool vis[MAXLEN];

void fun(int u) {
    if (vis[u]) {
        return;
    }
    vis[u] = true;
    for (int i = 0; i <= n; ++i) {
        if (st[u].next.count(26 + i) ){
            dp[u][i] = 1;
            //~ debug(u," flag");
        }
    }
    for (int i = 0; i < 26; ++i) {
        if (st[u].next.count(i)) {
            int v = st[u].next[i];
            fun(v);
            for (int j = 0; j <= n; ++j) {
                dp[u][j] += dp[v][j];
                //~ debug(u,j,dp[u][j]);
            }
        }
    }

    if (u && dp[u][0]) {
        bool flag = true;
        for (int i = 1; i <=n && flag ; ++i) {
            flag = l[i] <= dp[u][i] && dp[u][i] <= r[i];
            //~ debug(i,l[i],r[i],dp[u][i]);
        }

        //~ debug(flag);

        if (flag) {

            //~ debug(u,st[u].len - st[st[u].link].len);

            ans += st[u].len - st[st[u].link].len;
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);

    cin>>s;

    n=0;

    sa_init();

    rep(i,len(s))sa_extend(s[i]-'a');

    sa_extend(26);

    cin>>n;

    rep(i,n){

        cin>>s;
        cin>>l[i+1]>>r[i+1];
        rep(j,len(s))sa_extend(s[j]-'a');
        sa_extend(27+i);

    }

    fun(0);

    cout<<ans;

}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define rof(i,a,b) for(int (i)=(a);(i) > (b); --(i))
#define rep(i, c)	for(auto &(i) : (c))
#define x     first
#define y     second
#define pb  push_back
#define PB  pop_back()
#define iOS  ios_base::sync_with_stdio
#define sqr(a)  ((1LL * (a) * (a)))
#define all(a)  a.begin() , a.end()
#define error(x) cerr << #x << " = " << (x) <<endl
#define Error(a,b) cerr<<"( "<<#a<<" , "<<#b<<" ) = ( "<<(a)<<" , "<<(b)<<" )\n";
#define errop(a) cerr<<#a<<" = ( "<<((a).x)<<" , "<<((a).y)<<" )\n";
#define coud(a,b) cout<<fixed << setprecision((b)) << (a) << endl
#define L(x)	((x)<<1)
#define R(x)	(((x)<<1)+1)
typedef long long ll;
typedef pair<int,int>pii;
typedef vector<int> vi;
template <typename T> using os =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int maxn = 5e4 + 10;
string s;
string t[10];
int n, l[10], r[10];
int E;
typedef pair<pii,int> piii;
int mp[10][(int)1e6  + 10];
int p = 701, mod = 1e6 + 3;
inline void Cnt(int x){
	For(i,0,t[x].size()){
		int cur = 0, CUR = 0;
		For(j,i,t[x].size()){
			cur = (1LL * cur * p + t[x][j])%mod;
			mp[x][cur] ++;
		}
	}
}
int c,cc;
inline int cnt(int x){
	return mp[x][c];
}
inline int ver(){
	bool ll = 0, rr = 0;
	For(i,0,n){
		int q = cnt(i);
		if(q < l[i])
			ll = 1;
		if(q > r[i]){
			rr = 1;
		}
	}
	if(ll)
		return 0;
	if(rr)
		return 2;
	return 1;

}
set<pii> se;
int main(){
	iOS(false);
	cin >> s >> n;

	For(i,0,n)
		cin >> t[i] >> l[i] >> r[i], Cnt(i);
	int ans = 0;
	For(i,0,s.size()){
		int j = i;
		E = 0;
		int cur = 0, CUR = 0;
		while(j < s.size()){
			cur = (1LL * cur * p + s[j])%mod;
		//	CUR = (1LL * CUR * P + s[j])%MOD;
			E ++,j ++;
			c  = cur, cc = CUR;
			int x = ver();
			if(x == 1)
				se.insert(pii(cur,CUR));
			else if(!x)	break ;
		}
	}
	cout << se.size() << endl;

}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define rof(i,a,b) for(int (i)=(a);(i) > (b); --(i))
#define rep(i, c)	for(auto &(i) : (c))
#define x     first
#define y     second
#define pb  push_back
#define PB  pop_back()
#define iOS  ios_base::sync_with_stdio
#define sqr(a)  ((1LL * (a) * (a)))
#define all(a)  a.begin() , a.end()
#define error(x) cerr << #x << " = " << (x) <<endl
#define Error(a,b) cerr<<"( "<<#a<<" , "<<#b<<" ) = ( "<<(a)<<" , "<<(b)<<" )\n";
#define errop(a) cerr<<#a<<" = ( "<<((a).x)<<" , "<<((a).y)<<" )\n";
#define coud(a,b) cout<<fixed << setprecision((b)) << (a) << endl
//#define L(x)	((x)<<1)
#define R(x)	(((x)<<1)+1)
typedef long long ll;
typedef pair<int,int>pii;
typedef vector<int> vi;
template <typename T> using os =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int maxn = 1e6 + 100, maxsum = 5e4 + 100;
int ad[maxn][37], n;
ll dp[maxn][11], ans;
int l[11], r[11], suffix[maxn], L[maxn];
char s[maxsum], u[maxn];
int sz = 1, p;
inline void build(){
 	For(i,0,maxn){
        u[i] = 0;
		For(j,0,11)
            dp[i][j] = 0;
		For(j,0,37)
            ad[i][j] = -1;
    }
    suffix[0] = -1;
    L[0] = 0;
}
inline void add(int c){
    int v = sz++;
    L[v] = L[p] + 1;
    for(; p > -1 && ad[p][c] == -1; p = suffix[p])
        ad[p][c] = v;
    if(!(p+1))
        suffix[p] = 0;
    else {
        int to = ad[p][c];
        if(L[to] == L[p] + 1)
            suffix[v] = to;
        else{
            int cl = sz++;
            memcpy(ad[cl], ad[to], 37 * 4);
            L[cl] = L[p] + 1;
            suffix[cl] = suffix[to];
            for(; p > -1 && ad[p][c] == to; p = suffix[p])
                ad[p][c] = cl;
            suffix[v] = suffix[to] = cl;
        }
    }
    p = v;
}
inline void dfs(int v){
    u[v] = 1;
	For(i,0,n+1)
        if(ad[v][i] >= 0)
            dp[v][i] = 1;
	For(i,n+1,37)
        if(ad[v][i] >= 0){
            if(!u[ad[v][i]])
                dfs(ad[v][i]);
			For(j,0,n+1)
                dp[v][j] += dp[ad[v][i]][j];
        }
    if(v && dp[v][0]){
        bool ok = 1;
        for(int i = 1; i <= n && ok; i++)
            if(dp[v][i] < l[i] or dp[v][i] > r[i])
                ok = 0;
        if(ok)
            ans += L[v] - L[suffix[v]];
    }
}
int main(){
	iOS(false);
    build();
	cin >> s >> n;
    int L = strlen(s);
	For(i,0,L)
        add(s[i] - 'a' + n + 1);
    add(0);
	For(i,1,n+1){
		cin >> s >> l[i] >> r[i];
        L = strlen(s);
        For(j,0,L)
            add(s[j] - 'a' + n + 1);
        add(i);
    }
    dfs(0);
	cout << ans << endl;
}


/*
int main() {
	void tests();
	tests();
	return 0;
}
*/

//GST作ってやるだけでは
#include <cstdio>
#include <utility>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <climits>
#include <iostream>
using namespace std;

namespace solve {

class SuffixTree {
public:
	//入力はAlphaの[0,NAlpha)。負の値は使えないので注意。
	//終端文字も必要(NAlphaに含まれる)で、IsTerminatingAlphaで判定させる。
	//複数の文字列を扱う場合はその文字列の個数分一意な終端文字が必要。デフォルトでは終端文字に[0,32)を使う。
	//ほとんどの場合デフォルトの設定でOK。NAlphaを変える必要も無い。
	typedef unsigned char Alpha; static const int NAlpha = 256;
	static inline bool IsTerminatingAlpha(Alpha a) { return a < 32; }
	typedef size_t Index;	//32bit以外でテストされていない
	typedef unsigned long long Count;	//Index^2
	typedef std::pair<const Alpha*, const Alpha*> StrPair;
	static const int ListToArrayParam;

private:
	struct Leaf {
		const Alpha *begin, *end;
		Leaf *next;
		Leaf() { }
		Leaf(const Alpha *b): begin(b), next(NULL), end(NULL) { }
		Alpha terminatingAlpha() const { return *(end-1); }
		Index length() const { return end - begin; }	//葉の場合終端文字も含む長さを返すので注意
	};
	struct Node : Leaf {	//rootとinternal node
		union { Leaf *head; Leaf **edges; };
		Node *suffix;
		Node() { suffix = NULL; head = NULL; }
	};

	Index nNodes, nLeafs;
	Node *nodes; Leaf *leafs, *poolend;
	Node *root;
	Alpha *stringBuffer;	//追加した文字列のコピーのためのバッファ
	std::vector<Leaf**> edgeArrays;
	std::vector<std::pair<Alpha*,Alpha*> > addedStrings;	//追加した文字列のコピーのバッファの中間の位置
	//inorderTraversalによって計算される。木の親とinorder順序
	std::vector<const Node*> nodeSequence, nodeParent, leafParent;
	std::vector<const Leaf*> leafSequence;

	std::vector<std::vector<Index> > numDescendantLeafs;	//各文字列の葉の数

	//==========================================================================
	//SuffixTree.cpp
public:
	SuffixTree(Index n);	//これから追加する文字列の長さ(終端文字の分も含む)の総和を渡すこと
	~SuffixTree();

private:
	bool isLeaf(const Leaf *p) const { return leafs <= p; }
	bool isListStyle(const Node *p) const { return !p->head || ((Leaf*)nodes <= p->head && p->head < poolend); }

	Leaf **listToArray(Node *node);
	Leaf **getEdgeRef(Node *node, Alpha a);
	Leaf *getEdge(const Node *node, Alpha a) const;
	void addEdge(Node *node, Alpha a, Leaf *edge);

	static const Alpha *findStringEnd(const Alpha *str) { while(!IsTerminatingAlpha(*str)) ++ str; return str; }

	void addStringMain(const Alpha *str);
public:
	//終端文字を別個に指定するバージョン。endに終端文字は含まない。
	//複数の文字列を追加する場合は、それぞれの終端文字が一意でなければならない
	void addString(const Alpha *begin, const Alpha *end, Alpha terminatingAlpha);
	//strはIsTerminatingAlpha(a)がtrueとなる文字で終端されている必要がある
	void addString(const Alpha *str);

	//==========================================================================
	//SuffixTreeCalcs.cpp
public:
	void inorderTraversal();

	//solve
public:
	void calcNumDescendantLeafs();
	Count countGoodSubstrings(Index l[/*K-1*/], Index r[/*K-1*/]) const;
};

#define FOR_EACH_EDGES_(e, n, c)\
	for(Leaf *c *e##_ = isListStyle(n) ? &n->head : n->edges;\
	isListStyle(n) ? (*e##_ != NULL) : (e##_ < n->edges + NAlpha);\
	isListStyle(n) ? (e##_ = &(*e##_)->next) : (++ e##_))\
	if(c Leaf *e = *e##_)
#define FOR_EACH_EDGES_V(e, n) FOR_EACH_EDGES_(e, n, )
#define FOR_EACH_EDGES_C(e, n) FOR_EACH_EDGES_(e, n, const)

const int SuffixTree::ListToArrayParam = (int)std::pow(NAlpha*1., 0.4);	//ほとんどの場合変える必要はない

SuffixTree::SuffixTree(Index n) : nNodes(0), nLeafs(0) {
	assert(n > 0);
	size_t poolSize = sizeof(Node) * n + sizeof(Leaf) * n;
	char *pool = static_cast<char*>(std::malloc(poolSize));
	nodes = reinterpret_cast<Node*>(pool);
	leafs = reinterpret_cast<Leaf*>(pool + n * sizeof(Node));
	poolend = reinterpret_cast<Leaf*>(pool + poolSize);
	stringBuffer = static_cast<Alpha*>(std::malloc(n * sizeof(Alpha)));

	root = new(&nodes[nNodes ++]) Node();
	root->begin = root->end = NULL;
}
SuffixTree::~SuffixTree() {
	free(nodes);
	for(size_t i = 0; i < edgeArrays.size(); i ++) free(edgeArrays[i]);
	free(stringBuffer);
}

SuffixTree::Leaf **SuffixTree::listToArray(Node *node) {
	Leaf **edges = static_cast<Leaf**>(std::malloc(NAlpha * sizeof(Leaf*)));
	memset(edges, 0, sizeof(Leaf*) * NAlpha);
	FOR_EACH_EDGES_V(leaf, node)
		edges[*leaf->begin] = leaf;
	edgeArrays.push_back(edges);
	return edges;
}

SuffixTree::Leaf **SuffixTree::getEdgeRef(Node *node, Alpha a) {
	if(!isListStyle(node)) return &node->edges[a];
	Leaf **ref; int len = 0;
	for(ref = &node->head; *ref != NULL; ref = &(*ref)->next, ++ len) {
		if(len >= ListToArrayParam)
			return &(node->edges = listToArray(node))[a];
		if(*(*ref)->begin == a)
			return ref;
	}
	return ref;
}
SuffixTree::Leaf *SuffixTree::getEdge(const Node *node, Alpha a) const {
	if(!isListStyle(node)) return node->edges[a];
	for(Leaf *leaf = node->head; leaf != NULL; leaf = leaf->next)
		if(*leaf->begin == a) return leaf;
	return NULL;
}
void SuffixTree::addEdge(Node *node, Alpha a, Leaf *edge) {
	if(!isListStyle(node)) {
		node->edges[a] = edge;
	}else {
		Leaf *head = node->head;
		node->head = edge;
		edge->next = head;
	}
}

//strは文字列のコピー
void SuffixTree::addStringMain(const Alpha *str) {
	Node *active = root; const Alpha *activePos; Index activeLen = 0;
	const Alpha *currentPos = str, *proceeded = str;
	Index old_nLeafs = nLeafs;
	do {
		Alpha currentAlpha = *currentPos; Node *prevNode = NULL;
		while(proceeded <= currentPos) {
			if(activeLen == 0) activePos = currentPos;
			Leaf **activeEdgeRef = getEdgeRef(active, *activePos);
			Leaf *activeEdge = *activeEdgeRef;
			Node *splitNode = active;
			if(activeEdge) {
				if(!isLeaf(activeEdge) && activeLen >= activeEdge->length()) {
					active = (Node*)activeEdge;
					activePos += ((Node*)activeEdge)->length();
					activeLen -= ((Node*)activeEdge)->length();
					continue;
				}else if(*(activeEdge->begin + activeLen) == currentAlpha) {
					activeLen ++;
					if(prevNode) prevNode->suffix = active;
					break;
				}else {
					const Alpha *splitPos = activeEdge->begin + activeLen;
					Leaf *nextLeaf = activeEdge->next;
					*activeEdgeRef = splitNode = new(&nodes[nNodes ++]) Node();
					splitNode->begin = activeEdge->begin;
					splitNode->end = activeEdge->begin = splitPos;
					addEdge(splitNode, *splitPos, activeEdge);
					splitNode->next = nextLeaf;
				}
			}
			addEdge(splitNode, currentAlpha, new(&leafs[nLeafs ++]) Leaf(currentPos));
			if(prevNode) prevNode->suffix = splitNode;
			prevNode = splitNode;
			proceeded ++;
			if(active == root && activeLen > 0) {
				activeLen --;
				activePos = proceeded;
			}else
				active = active->suffix ? active->suffix : root;
		}
	}while(!IsTerminatingAlpha(*(currentPos ++)));
	assert(currentPos == proceeded);
	for(Index i = old_nLeafs; i < nLeafs; i ++)
		leafs[i].end = currentPos;
}

void SuffixTree::addString(const Alpha *begin, const Alpha *end, Alpha terminatingAlpha) {
	Index len = end - begin;
	Alpha *copied = addedStrings.empty() ? stringBuffer : addedStrings.back().second + 1;
	std::memcpy(copied, begin, len * sizeof(Alpha));
	copied[len] = terminatingAlpha;
	addedStrings.push_back(std::make_pair(copied, copied + len));
	addStringMain(copied);
}

void SuffixTree::addString(const Alpha *str) {
	const Alpha *endp = findStringEnd(str);
	return addString(str, endp, *endp);
}

void SuffixTree::inorderTraversal() {
	leafSequence.assign(nLeafs, NULL);
	leafParent.assign(nLeafs, NULL);
	nodeSequence.assign(nNodes, NULL);
	nodeParent.assign(nNodes, NULL);

	Index leafCount = 0, nodeCount = 0;
	std::vector<const Node*> stk; stk.reserve(nNodes);
	stk.push_back(root);
	while(!stk.empty()) {
		const Node *node = stk.back(); stk.pop_back();
		nodeSequence[nodeCount ++] = node;
		FOR_EACH_EDGES_C(edge, node) {
			if(isLeaf(edge)) {
				leafSequence[leafCount ++] = edge;
				leafParent[edge - leafs] = node;
			}else {
				const Node *child = static_cast<const Node*>(edge);
				nodeParent[child - nodes] = node;
				stk.push_back(child);
			}
		}
	}
	assert(leafCount == nLeafs && nodeCount == nNodes);
}

void SuffixTree::calcNumDescendantLeafs() {
	assert(!nodeSequence.empty());
	size_t K = addedStrings.size();
	numDescendantLeafs.assign(K, std::vector<Index>(nNodes, 0));
	if(nNodes == 0) return;

	for(Index i = 0; i < nLeafs; i ++) {
		const Leaf *leaf = leafSequence[i];
		const Node *parent = leafParent[leaf - leafs];
		Alpha terminatingAlpha = *(leaf->end - 1);
		++ numDescendantLeafs[terminatingAlpha][parent - nodes];
	}
	for(Index ii = nNodes - 1; ii > 0; -- ii) {
		const Node *node = nodeSequence[ii];
		const Node *parent = nodeParent[node - nodes];
		for(size_t k = 0; k < K; k ++)
			numDescendantLeafs[k][parent - nodes] += numDescendantLeafs[k][node - nodes];
	}
}

SuffixTree::Count SuffixTree::countGoodSubstrings(Index l[/*K-1*/], Index r[/*K-1*/]) const {
	assert(!numDescendantLeafs.empty());
	size_t K = addedStrings.size();
	Count res = 0;
	//rootは無視
	for(Index i = 1; i < nNodes; i ++) {
		Index edgeLen = nodes[i].length();
		Index multiplier = edgeLen;
		bool ok = true;
		for(size_t k = 1; k < K; k ++) {
			Index t = numDescendantLeafs[k][i];
			ok &= l[k-1] <= t && t <= r[k-1];
		}
		if(ok && numDescendantLeafs[0][i] > 0)
			res += multiplier;
	}
	for(Index ix = 0; ix < nLeafs; ix ++) {
		Index i = leafSequence[ix] - leafs;
		Index edgeLen = leafs[i].length();
		if(edgeLen == 1) continue;	//葉には終端文字が含まれる。それを含まないようにする
		Index multiplier = edgeLen - 1;
		Alpha terminatingAlpha = leafs[i].terminatingAlpha();
		bool ok = true;
		for(size_t k = 1; k < K; k ++) {
			Index t = terminatingAlpha == k ? 1 : 0;
			ok &= l[k-1] <= t && t <= r[k-1];
		}
		if(ok && terminatingAlpha == 0)
			res += multiplier;
	}
	return res;
};

}

int main() {
	using namespace solve;
	typedef SuffixTree::Alpha Alpha;
	static char s[50001], p[10][50001];
	static SuffixTree::Index l[10], r[10];
	int lenSum = 0;
	scanf("%s", s);
	lenSum += strlen(s) + 1;
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; i ++) {
		int ll, rr;
		scanf("%s%d%d", p[i], &ll, &rr);
		l[i] = ll, r[i] = rr;
		lenSum += strlen(p[i]) + 1;
	}
	SuffixTree st(lenSum);
	st.addString((Alpha*)s, (Alpha*)s + strlen(s), 0);
	for(int i = 0; i < n; i ++)
		st.addString((Alpha*)p[i], (Alpha*)p[i] + strlen(p[i]), i + 1);
	st.inorderTraversal();
	st.calcNumDescendantLeafs();
	long long ans = st.countGoodSubstrings(l, r);
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
#define maxn 600005
using namespace std;

struct sam
{
	int t,f,l,x[27];
} t[maxn*2];
int last,S,N,L[15],R[15],b[maxn*2],q[maxn*2],s[maxn*2][15];
char a[maxn];

void add(int c,int d)
{
	int p=last,q,r,u=++S;
	last=u,t[u].l=t[p].l+1;
	if (d>-1) s[u][d]++;
	for (;p&&!t[p].x[c]; p=t[p].f) t[p].x[c]=u;
	if (!p) t[u].f=1;
	else if (t[q=t[p].x[c]].l==t[p].l+1) t[u].f=q;
	else
	{
		t[r=++S]=t[q],t[r].l=t[p].l+1,t[u].f=t[q].f=r;
		for (;p&&t[p].x[c]==q; p=t[p].f) t[p].x[c]=r;
	}
}

void add(char*s,int d)
{
	for (int i=0; s[i]; i++) add(s[i]-97,d);
	add(26,-1);
}

void init()
{
	last=++S,scanf("%s%d",a,&N),add(a,0);
	for (int i=1; i<=N; i++) scanf("%s%d%d",a,&L[i],&R[i]),add(a,i);
}

void doit()
{
	long long ans=0;
	for (int i=1; i<=S; i++) b[t[i].l]++;
	for (int i=1; i<=S; i++) b[i]+=b[i-1];
	for (int i=S; i; i--) q[b[t[i].l]--]=i;
	for (int x=S,i; i=q[x],x; x--)
	{
		for (int j=0; j<=N; j++) s[t[i].f][j]+=s[i][j];
		bool ok=s[i][0];
		for (int j=1; ok&&j<=N; j++) if (s[i][j]<L[j]||s[i][j]>R[j]) ok=0;
		if (ok) ans+=t[i].l-t[t[i].f].l;
	}
	cout<<ans<<endl;
}

int main()
{
	init();
	doit();
	return 0;
}


#include <bits/stdc++.h>
#define maxn 600005
using namespace std;

struct sam
{
	int t,f,l,x[27];
} t[maxn];
int last,S,N,L[15],R[15],b[maxn*2],q[maxn*2],s[maxn*2][11];
char a[maxn];

void add(int c,int d)
{
	int p=last,q,r,u=++S;
	last=u,t[u].l=t[p].l+1;
	if (d>-1) s[u][d]++;
	for (;p&&!t[p].x[c]; p=t[p].f) t[p].x[c]=u;
	if (!p) t[u].f=1;
	else if (t[q=t[p].x[c]].l==t[p].l+1) t[u].f=q;
	else
	{
		t[r=++S]=t[q],t[r].l=t[p].l+1,t[u].f=t[q].f=r;
		for (;p&&t[p].x[c]==q; p=t[p].f) t[p].x[c]=r;
	}
}

void add(char*s,int d)
{
	for (int i=0; s[i]; i++) add(s[i]-97,d);
	add(26,-1);
}

void init()
{
	last=++S,scanf("%s%d",a,&N),add(a,0);
	for (int i=1; i<=N; i++) scanf("%s%d%d",a,&L[i],&R[i]),add(a,i);
}

void doit()
{
	long long ans=0;
	for (int i=1; i<=S; i++) b[t[i].l]++;
	for (int i=1; i<=S; i++) b[i]+=b[i-1];
	for (int i=S; i; i--) q[b[t[i].l]--]=i;
	for (int x=S,i; i=q[x],x; x--)
	{
		for (int j=0; j<=N; j++) s[t[i].f][j]+=s[i][j];
		bool ok=s[i][0];
		for (int j=1; ok&&j<=N; j++) if (s[i][j]<L[j]||s[i][j]>R[j]) ok=0;
		if (ok) ans+=t[i].l-t[t[i].f].l;
	}
	cout<<ans<<endl;
}

int main()
{
	init();
	doit();
	return 0;
}