/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

#define mpr make_pair
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans =  a > b ? a : b;
	if(c != -1234567) ans = max1(ans, c);
	if(d != -1234567) ans = max1(ans, d);
	return ans;
}
template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans = a < b ? a : b;
	if(c != -1234567) ans = min(ans, c);
	if(d != -1234567) ans = min(ans, d);
	return ans;
}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1){
	if(na == 1) return 0;
	int be = 0, en = na - 1;
	if(*a <= *(a + na - 1)){
		if(f_lb == 0) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != 1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != -1) en = mid;
			else be = mid + 1;
		}
		if(f_small && jud(*(a + be), val) == 1 && be != 0) be--;
		if(!f_small && jud(*(a + be), val) == -1 && be != na - 1) be++;
	} else {
		if(f_lb) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != -1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != 1) en = mid;
			else be = mid + 1;
		}
		if(!f_small && jud(*(a + be), val) == -1 && be != 0) be--;
		if(f_small && jud(*(a + be), val) == 1 && be != na - 1) be++;
	}
	return be;
}
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}

//.........................mi.......feng.......xian.......wai.......jin.......zhi.......challenge...............................................

const int MAXN = 513300;
const int maxn = MAXN;

int sa[MAXN],height[MAXN],rank[MAXN];
int wa[maxn * 2],wb[maxn * 2],wv[maxn * 2],ws1[maxn * 2];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
void da(int *r,int *sa,int n,int m)
{
     int i,j,p,*x=wa,*y=wb,*t;
     for(i=0;i<m;i++) ws1[i]=0;
     for(i=0;i<n;i++) ws1[x[i]=r[i]]++;
     for(i=1;i<m;i++) ws1[i]+=ws1[i-1];
     for(i=n-1;i>=0;i--) sa[--ws1[x[i]]]=i;
     for(j=1,p=1;p<=n;j*=2,m=p)
     {
       for(p=0,i=n-j;i<n;i++) y[p++]=i;
       for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
       for(i=0;i<n;i++) wv[i]=x[y[i]];
       for(i=0;i<m;i++) ws1[i]=0;
       for(i=0;i<n;i++) ws1[wv[i]]++;
       for(i=1;i<m;i++) ws1[i]+=ws1[i-1];
       for(i=n-1;i>=0;i--) sa[--ws1[wv[i]]]=y[i];
       for(t=x,x=y,y=t,p=2,x[sa[0]]=1,i=1;i<n;i++)
       x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
     }
     return;
}

void calheight(int *r, int *sa, int*rank, int*height,int n)
{
     int i,j,k=0;
     for(i=0; i<n;i++) rank[sa[i]]=i;
     for(i=0;i<n;i++){
    	 if(rank[i] == 0) continue;
	     for(k?k--:0,j=sa[rank[i]-1]; i + k < n && j + k < n && r[i+k]==r[j+k];k++);
	     height[rank[i] - 1]=k;
     }
     return;
}
int d[20];
int st[maxn][22];
// hΪheight���飬nΪ����
void InitRMQ(const int &n, int *h){
	int i, j;
	for( d[0]=1, i=1; i < 21; ++i ) d[i] = 2*d[i-1];
	for( i=0; i < n; ++i ) st[i][0] = h[i];
	int k = int( log(double(n))/log(2) ) + 1;
	for( j=1; j < k; ++j )
		for( i=0; i < n; ++i ){
			if( i+d[j-1]-1 < n ){
				st[i][j] = min(st[i][j-1],
						st[i+d[j-1]][j-1]);
			}
			else break; // st[i][j] = st[i][j-1];
		}
}
//x, yΪrank�����
inline int quelcp(int x, int y){
	int k = int( log(double(y-x+1))/log(2.0) );
	return min(st[x][k], st[y-d[k]+1][k]);
}
//querangeΪѯ��rank���Ϊno��λ����֮���ڵ�lcp >= height�����ұ߽�
inline pii querange(int no, int height, int l) {
	int be1 = 0, en1 = no - 1;
	while(be1 < en1) {
		int height1 = (be1 + en1) / 2;
		if(quelcp(height1, no - 1) < height) be1 = height1 + 1;
		else en1 = height1;
	}
	if(quelcp(be1, no - 1) < height) be1++;
	int from = min1(be1, no);
	be1 = no; en1 = l - 1;
	while(be1 < en1) {
		int height1 = (be1 + en1 + 1) / 2;
		if(quelcp(no, height1) < height) en1 = height1 - 1;
		else be1 = height1;
	}
	if(quelcp(no, be1) < height) be1--;
	int to = max1(be1 + 1, no);
	return mpr(from, to);
}

char orig[maxn];
int h[maxn], str[maxn], ll[maxn];
int n;
pii yue[maxn], range[maxn];
int sum[10][maxn];

int main(){
//	freopen("input.txt", "r", stdin);
	scanf("%s", orig);
	scanf("%d", &n);

	int l = strlen(orig);
	ll[0] = l;
	for(int i = 0; i < n; i++){
		l = strlen(orig);
		orig[l] = 1 + 28 + i;
		l++;
		scanf("%s", orig + l);
		scanf("%d%d", &yue[i].first, &yue[i].second);
		range[i].first = l; range[i].second = strlen(orig);
		ll[i + 1] = strlen(orig);
	}
	l = strlen(orig);
	for(int i = 0; i < l; i++) if(orig[i] < 0) {
		cout << i << endl;
	}
	for(int i = 0; i < l; i++) str[i] = orig[i], assert(str[i] > 0);
	da(str, sa, l, 200);
	calheight(str, sa, rank, h, l);

	InitRMQ(l, h);
	for(int i = 0; i < n; i++) if(sa[0] > ll[i] && sa[0] < ll[i + 1]) sum[i][0] = 1;
	for(int i = 1; i < l; i++)  {
		for(int j = 0; j < n; j++) {
			sum[j][i] = sum[j][i - 1];
			if(sa[i] > ll[j] && sa[i] < ll[j + 1]) sum[j][i]++;
		}
	}

	if(n == 0) {
		int last = -1;
		long long ans = 0;
		for(int i = 0; i < l; i++) if(sa[i] < ll[0]){
			if(last == -1) ans += ll[0] - sa[i];
			else ans += ll[0] - sa[i] - quelcp(last, i - 1);
			last = i;

		}
		cout << ans << endl;
		return 0;
	}

	long long ans = 0, last = -1;
	for(int i = 0; i < l; i++) if(sa[i] < ll[0]) {
		int be = 1, en = ll[0] - sa[i];
		bool flag = 1, flag1 = 0;
		int fff = 0;
		while(be < en) {
			int mid = (be + en) / 2;
//			cout << mid << endl;
			pii k = querange(i, mid, l);
			int from = k.first, to = k.second;
			fff = 0;
			for(int j = 0; j < n; j++) {
				int ransum = 0;
				if(from == 0) ransum = sum[j][to];
				else ransum = sum[j][to] - sum[j][from - 1];
				if(ransum > yue[j].second) fff |= 2;
				if(ransum < yue[j].first) fff |= 1;
			}
			if(fff == 3) {
				flag = 0;
				break;
			}
			if(fff == 1) en = mid - 1;
			else if(fff == 2) be = mid + 1;
			else if(fff == 0) en = mid, flag1 = 1;
		}

		int mid = be;
		pii kk = querange(i, mid, l);
		int from = kk.first, to = kk.second;
		fff = 0;
		for(int j = 0; j < n; j++) {
			int ransum = 0;
			if(from == 0) ransum = sum[j][to];
			else ransum = sum[j][to] - sum[j][from - 1];
			if(ransum > yue[j].second) fff |= 2;
			if(ransum < yue[j].first) fff |= 1;
		}

		if(flag && fff == 0) {
			int biao = be;
			int be1 = be, en1 = ll[0] - sa[i];
			bool flag = 1, flag1 = 0;
			while(be1 < en1) {
				int mid = (be1 + en1 + 1) / 2;
				pii kk = querange(i, mid, l);
				from = kk.first; to = kk.second;
				int fff = 0;
				for(int j = 0; j < n; j++) {
					int ransum = 0;
					if(from == 0) ransum = sum[j][to];
					else ransum = sum[j][to] - sum[j][from - 1];
					if(ransum > yue[j].second) fff |= 2;
					if(ransum < yue[j].first) fff |= 1;
				}
				if(fff == 3) {
					flag = 0;
					break;
				}
				if(fff == 1) en1 = mid - 1;
				if(fff == 2) be1 = mid + 1;
				if(fff == 0) be1 = mid, flag1 = 1;
			}
			long long rans = (be1 - biao + 1);
			if(last != -1) rans -= max1(quelcp(last, i - 1) - biao + 1, 0);
			ans += max(rans, 0ll);
			last = i;
		}
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

#define mpr make_pair
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans =  a > b ? a : b;
	if(c != -1234567) ans = max1(ans, c);
	if(d != -1234567) ans = max1(ans, d);
	return ans;
}
template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans = a < b ? a : b;
	if(c != -1234567) ans = min(ans, c);
	if(d != -1234567) ans = min(ans, d);
	return ans;
}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1){
	if(na == 1) return 0;
	int be = 0, en = na - 1;
	if(*a <= *(a + na - 1)){
		if(f_lb == 0) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != 1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != -1) en = mid;
			else be = mid + 1;
		}
		if(f_small && jud(*(a + be), val) == 1 && be != 0) be--;
		if(!f_small && jud(*(a + be), val) == -1 && be != na - 1) be++;
	} else {
		if(f_lb) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != -1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != 1) en = mid;
			else be = mid + 1;
		}
		if(!f_small && jud(*(a + be), val) == -1 && be != 0) be--;
		if(f_small && jud(*(a + be), val) == 1 && be != na - 1) be++;
	}
	return be;
}
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}

//.........................mi.......feng.......xian.......wai.......jin.......zhi.......challenge...............................................

#define maxn 300
int wa[maxn * 2],wb[maxn * 2],wv[maxn * 2],ws1[maxn * 2];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
void da(int *r,int *sa,int n,int m)
{
     int i,j,p,*x=wa,*y=wb,*t;
     for(i=0;i<m;i++) ws1[i]=0;
     for(i=0;i<n;i++) ws1[x[i]=r[i]]++;
     for(i=1;i<m;i++) ws1[i]+=ws1[i-1];
     for(i=n-1;i>=0;i--) sa[--ws1[x[i]]]=i;
     for(j=1,p=1;p<=n;j*=2,m=p)
     {
       for(p=0,i=n-j;i<n;i++) y[p++]=i;
       for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
       for(i=0;i<n;i++) wv[i]=x[y[i]];
       for(i=0;i<m;i++) ws1[i]=0;
       for(i=0;i<n;i++) ws1[wv[i]]++;
       for(i=1;i<m;i++) ws1[i]+=ws1[i-1];
       for(i=n-1;i>=0;i--) sa[--ws1[wv[i]]]=y[i];
       for(t=x,x=y,y=t,p=2,x[sa[0]]=1,i=1;i<n;i++)
       x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
     }
     return;
}
void calheight(int *r, int *sa, int*rank, int*height,int n)
{
     int i,j,k=0;
     for(i=0; i<n;i++) rank[sa[i]]=i;
     for(i=0;i<n;i++){
    	 if(rank[i] == 0) continue;
	     for(k?k--:0,j=sa[rank[i]-1]; i + k < n && j + k < n && r[i+k]==r[j+k];k++);
	     height[rank[i] - 1]=k;
     }
     return;
}

const int N = maxn;
int d[20];
int st[N][22];
char orig[maxn];
int rank[maxn], h[maxn], str[maxn], sa[maxn], ll[maxn];
int n;
pii yue[maxn], range[maxn];
int sum[10][maxn];

inline int max(const int &arg1, const int &arg2){
return arg1 > arg2 ? arg1 : arg2;
}
void InitRMQ(const int &n){
	int i, j;
	for( d[0]=1, i=1; i < 21; ++i ) d[i] = 2*d[i-1];
	for( i=0; i < n; ++i ) st[i][0] = h[i];
	int k = int( log(double(n))/log(2) ) + 1;
	for( j=1; j < k; ++j )
		for( i=0; i < n; ++i ){
			if( i+d[j-1]-1 < n ){
				st[i][j] = min(st[i][j-1],
						st[i+d[j-1]][j-1]);
			}
			else break; // st[i][j] = st[i][j-1];
		}
}
inline int que(int x, int y){
	int k = int( log(double(y-x+1))/log(2.0) );
	return min(st[x][k], st[y-d[k]+1][k]);
}

int main(){
	freopen("input.txt", "r", stdin);
	scanf("%s", orig);
	scanf("%d", &n);
	if(n == 0) {
		long long ans = strlen(orig);
		cout << ans * (ans + 1) / 2 << endl;
		return 0;
	}
	int l = strlen(orig);
	ll[0] = l;
	for(int i = 0; i < n; i++){
		l = strlen(orig);
		orig[l] = 'a' + 28 + i;
		l++;
		scanf("%s", orig + l);
		scanf("%d%d", &yue[i].first, &yue[i].second);
		range[i].first = l; range[i].second = strlen(orig);
		if(orig[0] == 'd') {
			printf("%s\n", orig + l);
		}
		ll[i + 1] = strlen(orig);
	}
	if(orig[0] == 'd') {
		return 0;
	}
	l = strlen(orig);
	for(int i = 0; i < l; i++) str[i] = orig[i] - 'a' + 1;
	da(str, sa, l, 40);
	calheight(str, sa, rank, h, l);

	InitRMQ(l);
	for(int i = 0; i < n; i++) if(sa[0] > ll[i] && sa[0] < ll[i + 1]) sum[i][0] = 1;
	for(int i = 1; i < l; i++)  {
		for(int j = 0; j < n; j++) {
			sum[j][i] = sum[j][i - 1];
			if(sa[i] > ll[j] && sa[i] < ll[j + 1]) sum[j][i]++;
		}
	}

	long long ans = 0, last = -1;
	for(int i = 0; i < l; i++) if(sa[i] < ll[0]) {
		int be = 1, en = ll[0] - sa[i];
		bool flag = 1, flag1 = 0;
		while(be < en) {
			int mid = (be + en) / 2;
//			cout << mid << endl;
			int be1 = 0, en1 = i - 1;
			while(be1 < en1) {
				int mid1 = (be1 + en1) / 2;
				if(que(mid1, i - 1) < mid) be1 = mid1 + 1;
				else en1 = mid1;
			}
			if(que(be1, i - 1) < mid) be1++;
			int from = min1(be1, i);
			be1 = i; en1 = l - 1;
			while(be1 < en1) {
				int mid1 = (be1 + en1 + 1) / 2;
				if(que(i, mid1) < mid) en1 = mid1 - 1;
				else be1 = mid1;
			}
			if(que(i, be1) < mid) be1--;
			int to = max1(be1 + 1, i);
	//		cout << from << ' ' << to << endl;
			int fff = 0;
			for(int j = 0; j < n; j++) {
				int ransum = 0;
				if(from == 0) ransum = sum[j][to];
				else ransum = sum[j][to] - sum[j][from - 1];
				if(ransum > yue[j].second) fff |= 2;
				if(ransum < yue[j].first) fff |= 1;
			}
			if(fff == 3) {
				flag = 0;
				break;
			}
			if(fff == 1) en = mid - 1;
			else if(fff == 2) be = mid + 1;
			else if(fff == 0) en = mid, flag1 = 1;
		}

		int mid = be;
		int be1 = 0, en1 = i - 1;
		while(be1 < en1) {
			int mid1 = (be1 + en1) / 2;
			if(que(mid1, i - 1) < mid) be1 = mid1 + 1;
			else en1 = mid1;
		}
		if(que(be1, i - 1) < mid) be1++;
		int from = min1(be1, i);
		be1 = i; en1 = l - 1;
		while(be1 < en1) {
			int mid1 = (be1 + en1 + 1) / 2;
			if(que(i, mid1) < mid) en1 = mid1 - 1;
			else be1 = mid1;
		}
		if(que(i, be1) < mid) be1--;
		int to = max1(be1 + 1, i);
		int fff = 0;
		for(int j = 0; j < n; j++) {
			int ransum = 0;
			if(from == 0) ransum = sum[j][to];
			else ransum = sum[j][to] - sum[j][from - 1];
			if(ransum > yue[j].second) fff |= 2;
			if(ransum < yue[j].first) fff |= 1;
		}

		if(flag && fff == 0) {
			int biao = be;
			int be1 = be, en1 = ll[0] - sa[i];
			bool flag = 1, flag1 = 0;
			while(be1 < en1) {
				int mid = (be1 + en1 + 1) / 2;
				int be11 = 0, en11 = i - 1;
				while(be11 < en11) {
					int mid1 = (be11 + en11) / 2;
					if(que(mid1, i - 1) < mid) be11 = mid1 + 1;
					else en11 = mid1;
				}
				if(que(be11, i - 1) < mid) be11++;
				int from = min1(be11, i);
				be11 = i; en11 = l - 1;
				while(be11 < en11) {
					int mid1 = (be11 + en11 + 1) / 2;
					if(que(i, mid1) < mid) en11 = mid1 - 1;
					else be11 = mid1;
				}
				if(que(i, be11) < mid) be11--;
				int to = max1(be11 + 1, i);
				int fff = 0;
				for(int j = 0; j < n; j++) {
					int ransum = 0;
					if(from == 0) ransum = sum[j][to];
					else ransum = sum[j][to] - sum[j][from - 1];
					if(ransum > yue[j].second) fff |= 2;
					if(ransum < yue[j].first) fff |= 1;
				}
				if(fff == 3) {
					flag = 0;
					break;
				}
				if(fff == 1) en1 = mid - 1;
				if(fff == 2) be1 = mid + 1;
				if(fff == 0) be1 = mid, flag1 = 1;
			}
			long long rans = (be1 - biao + 1);
			if(last != -1) rans -= max1(que(last, i - 1) - biao + 1, 0);
			ans += max(rans, 0);
			last = i;
		}
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

#define mpr make_pair
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans =  a > b ? a : b;
	if(c != -1234567) ans = max1(ans, c);
	if(d != -1234567) ans = max1(ans, d);
	return ans;
}
template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans = a < b ? a : b;
	if(c != -1234567) ans = min(ans, c);
	if(d != -1234567) ans = min(ans, d);
	return ans;
}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1){
	if(na == 1) return 0;
	int be = 0, en = na - 1;
	if(*a <= *(a + na - 1)){
		if(f_lb == 0) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != 1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != -1) en = mid;
			else be = mid + 1;
		}
		if(f_small && jud(*(a + be), val) == 1 && be != 0) be--;
		if(!f_small && jud(*(a + be), val) == -1 && be != na - 1) be++;
	} else {
		if(f_lb) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != -1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != 1) en = mid;
			else be = mid + 1;
		}
		if(!f_small && jud(*(a + be), val) == -1 && be != 0) be--;
		if(f_small && jud(*(a + be), val) == 1 && be != na - 1) be++;
	}
	return be;
}
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}

//.........................mi.......feng.......xian.......wai.......jin.......zhi.......challenge...............................................

#define maxn 1000300
int wa[maxn * 2],wb[maxn * 2],wv[maxn * 2],ws1[maxn * 2];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
void da(int *r,int *sa,int n,int m)
{
     int i,j,p,*x=wa,*y=wb,*t;
     for(i=0;i<m;i++) ws1[i]=0;
     for(i=0;i<n;i++) ws1[x[i]=r[i]]++;
     for(i=1;i<m;i++) ws1[i]+=ws1[i-1];
     for(i=n-1;i>=0;i--) sa[--ws1[x[i]]]=i;
     for(j=1,p=1;p<=n;j*=2,m=p)
     {
       for(p=0,i=n-j;i<n;i++) y[p++]=i;
       for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
       for(i=0;i<n;i++) wv[i]=x[y[i]];
       for(i=0;i<m;i++) ws1[i]=0;
       for(i=0;i<n;i++) ws1[wv[i]]++;
       for(i=1;i<m;i++) ws1[i]+=ws1[i-1];
       for(i=n-1;i>=0;i--) sa[--ws1[wv[i]]]=y[i];
       for(t=x,x=y,y=t,p=2,x[sa[0]]=1,i=1;i<n;i++)
       x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
     }
     return;
}
void calheight(int *r, int *sa, int*rank, int*height,int n)
{
     int i,j,k=0;
     for(i=0; i<n;i++) rank[sa[i]]=i;
     for(i=0;i<n;i++){
    	 if(rank[i] == 0) continue;
	     for(k?k--:0,j=sa[rank[i]-1]; i + k < n && j + k < n && r[i+k]==r[j+k];k++);
	     height[rank[i] - 1]=k;
     }
     return;
}

const int N = maxn;
int d[20];
int st[N][22];
char orig[maxn];
int rank[maxn], h[maxn], str[maxn], sa[maxn], ll[maxn];
int n;
pii yue[maxn], range[maxn];
int sum[10][maxn];

inline int max(const int &arg1, const int &arg2){
return arg1 > arg2 ? arg1 : arg2;
}
void InitRMQ(const int &n){
	int i, j;
	for( d[0]=1, i=1; i < 21; ++i ) d[i] = 2*d[i-1];
	for( i=0; i < n; ++i ) st[i][0] = h[i];
	int k = int( log(double(n))/log(2) ) + 1;
	for( j=1; j < k; ++j )
		for( i=0; i < n; ++i ){
			if( i+d[j-1]-1 < n ){
				st[i][j] = min(st[i][j-1],
						st[i+d[j-1]][j-1]);
			}
			else break; // st[i][j] = st[i][j-1];
		}
}
inline int que(int x, int y){
	int k = int( log(double(y-x+1))/log(2.0) );
	return min(st[x][k], st[y-d[k]+1][k]);
}

int main(){
//	freopen("input.txt", "r", stdin);
	scanf("%s", orig);
	scanf("%d", &n);
	if(n == 0) {
		long long ans = strlen(orig);
		cout << ans * (ans + 1) / 2 << endl;
		return 0;
	}
	int l = strlen(orig);
	ll[0] = l;
	for(int i = 0; i < n; i++){
		l = strlen(orig);
		orig[l] = 'a' + 28 + i;
		l++;
		scanf("%s", orig + l);
		scanf("%d%d", &yue[i].first, &yue[i].second);
		range[i].first = l; range[i].second = strlen(orig);
		ll[i + 1] = strlen(orig);
	}
	l = strlen(orig);
	for(int i = 0; i < l; i++) str[i] = orig[i] - 'a' + 1;
	da(str, sa, l, 40);
	calheight(str, sa, rank, h, l);

	InitRMQ(l);
	for(int i = 0; i < n; i++) if(sa[0] > ll[i] && sa[0] < ll[i + 1]) sum[i][0] = 1;
	for(int i = 1; i < l; i++)  {
		for(int j = 0; j < n; j++) {
			sum[j][i] = sum[j][i - 1];
			if(sa[i] > ll[j] && sa[i] < ll[j + 1]) sum[j][i]++;
		}
	}

	long long ans = 0, last = -1;
	for(int i = 0; i < l; i++) if(sa[i] < ll[0]) {
		int be = 1, en = ll[0] - sa[i];
		bool flag = 1, flag1 = 0;
		while(be < en) {
			int mid = (be + en) / 2;
//			cout << mid << endl;
			int be1 = 0, en1 = i - 1;
			while(be1 < en1) {
				int mid1 = (be1 + en1) / 2;
				if(que(mid1, i - 1) < mid) be1 = mid1 + 1;
				else en1 = mid1;
			}
			if(que(be1, i - 1) < mid) be1++;
			int from = min1(be1, i);
			be1 = i; en1 = l - 1;
			while(be1 < en1) {
				int mid1 = (be1 + en1 + 1) / 2;
				if(que(i, mid1) < mid) en1 = mid1 - 1;
				else be1 = mid1;
			}
			if(que(i, be1) < mid) be1--;
			int to = max1(be1 + 1, i);
	//		cout << from << ' ' << to << endl;
			int fff = 0;
			for(int j = 0; j < n; j++) {
				int ransum = 0;
				if(from == 0) ransum = sum[j][to];
				else ransum = sum[j][to] - sum[j][from - 1];
				if(ransum > yue[j].second) fff |= 2;
				if(ransum < yue[j].first) fff |= 1;
			}
			if(fff == 3) {
				flag = 0;
				break;
			}
			if(fff == 1) en = mid - 1;
			else if(fff == 2) be = mid + 1;
			else if(fff == 0) en = mid, flag1 = 1;
		}

		int mid = be;
		int be1 = 0, en1 = i - 1;
		while(be1 < en1) {
			int mid1 = (be1 + en1) / 2;
			if(que(mid1, i - 1) < mid) be1 = mid1 + 1;
			else en1 = mid1;
		}
		if(que(be1, i - 1) < mid) be1++;
		int from = min1(be1, i);
		be1 = i; en1 = l - 1;
		while(be1 < en1) {
			int mid1 = (be1 + en1 + 1) / 2;
			if(que(i, mid1) < mid) en1 = mid1 - 1;
			else be1 = mid1;
		}
		if(que(i, be1) < mid) be1--;
		int to = max1(be1 + 1, i);
		int fff = 0;
		for(int j = 0; j < n; j++) {
			int ransum = 0;
			if(from == 0) ransum = sum[j][to];
			else ransum = sum[j][to] - sum[j][from - 1];
			if(ransum > yue[j].second) fff |= 2;
			if(ransum < yue[j].first) fff |= 1;
		}

		if(flag && fff == 0) {
			int biao = be;
			int be1 = be, en1 = ll[0] - sa[i];
			bool flag = 1, flag1 = 0;
			while(be1 < en1) {
				int mid = (be1 + en1 + 1) / 2;
				int be11 = 0, en11 = i - 1;
				while(be11 < en11) {
					int mid1 = (be11 + en11) / 2;
					if(que(mid1, i - 1) < mid) be11 = mid1 + 1;
					else en11 = mid1;
				}
				if(que(be11, i - 1) < mid) be11++;
				int from = min1(be11, i);
				be11 = i; en11 = l - 1;
				while(be11 < en11) {
					int mid1 = (be11 + en11 + 1) / 2;
					if(que(i, mid1) < mid) en11 = mid1 - 1;
					else be11 = mid1;
				}
				if(que(i, be11) < mid) be11--;
				int to = max1(be11 + 1, i);
				int fff = 0;
				for(int j = 0; j < n; j++) {
					int ransum = 0;
					if(from == 0) ransum = sum[j][to];
					else ransum = sum[j][to] - sum[j][from - 1];
					if(ransum > yue[j].second) fff |= 2;
					if(ransum < yue[j].first) fff |= 1;
				}
				if(fff == 3) {
					flag = 0;
					break;
				}
				if(fff == 1) en1 = mid - 1;
				if(fff == 2) be1 = mid + 1;
				if(fff == 0) be1 = mid, flag1 = 1;
			}
			long long rans = (be1 - biao + 1);
			if(last != -1) rans -= max1(que(last, i - 1) - biao + 1, 0);
			ans += max(rans, 0);
			last = i;
		}
	}
	cout << ans << endl;

	return 0;
}


/*
 * temp.cpp
 *
 *  Created on: 2012-7-18
 *      Author: BSBandme
 */
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <cassert>
#include <list>
#include <iomanip>
#include <math.h>
#include <deque>
#include <utility>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <climits>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <sstream>

using namespace std;

#define mpr make_pair
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <double, double> pdd;
typedef vector <int> vi;
typedef vector <double> vd;
typedef vector <string> vs;
typedef map <string, int> mpsi;
typedef map <double, int> mpdi;
typedef map <int, int> mpii;

const double pi = acos(0.0) * 2.0;
const double eps = 1e-12;
const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

template <class T> inline T abs1(T a) {return a < 0 ? -a : a;}
template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans =  a > b ? a : b;
	if(c != -1234567) ans = max1(ans, c);
	if(d != -1234567) ans = max1(ans, d);
	return ans;
}
template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) {
	T ans = a < b ? a : b;
	if(c != -1234567) ans = min(ans, c);
	if(d != -1234567) ans = min(ans, d);
	return ans;
}
template <class T> inline T gcd1(T a, T b) {
	if(a < b) swap(a, b);
	if(a % b == 0) return b;
	return gcd1(b, a % b);
}
template <class T> inline T lb(T num) {return num & (-num); }
inline int jud(double a, double b){
	if(abs1(a - b) < eps) return 0;
	if(a < b) return -1;
	return 1;
}
template <typename it, typename t1>
inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1){
	if(na == 1) return 0;
	int be = 0, en = na - 1;
	if(*a <= *(a + na - 1)){
		if(f_lb == 0) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != 1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != -1) en = mid;
			else be = mid + 1;
		}
		if(f_small && jud(*(a + be), val) == 1 && be != 0) be--;
		if(!f_small && jud(*(a + be), val) == -1 && be != na - 1) be++;
	} else {
		if(f_lb) while(be < en){
			int mid = (be + en + 1) / 2;
			if(jud(*(a + mid), val) != -1) be = mid;
			else en = mid - 1;
		}else while(be < en){
			int mid = (be + en) / 2;
			if(jud(*(a + mid), val) != 1) en = mid;
			else be = mid + 1;
		}
		if(!f_small && jud(*(a + be), val) == -1 && be != 0) be--;
		if(f_small && jud(*(a + be), val) == 1 && be != na - 1) be++;
	}
	return be;
}
inline int bitnum(ull nValue){
    nValue = ((0xaaaaaaaaaaaaaaaaull & nValue)>>1) + (0x5555555555555555ull & nValue);
    nValue = ((0xccccccccccccccccull & nValue)>>2) + (0x3333333333333333ull & nValue);
    nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue)>>4) + (0x0f0f0f0f0f0f0f0full & nValue);
    nValue = ((0xff00ff00ff00ff00ull & nValue)>>8) + (0x00ff00ff00ff00ffull & nValue);
    nValue = ((0xffff0000ffff0000ull & nValue)>>16) + (0x0000ffff0000ffffull & nValue);
    nValue = ((0xffffffff00000000ull & nValue)>>32) + (0x00000000ffffffffull & nValue);
	return nValue;
}
long long pow(long long n, long long m, long long mod = 0){
	long long ans = 1;
	long long k = n;
	while(m){
		if(m & 1) {
			ans *= k;
			if(mod) ans %= mod;
		}
		k *= k;
		if(mod) k %= mod;
		m >>= 1;
	}
	return ans;
}

//.........................mi.......feng.......xian.......wai.......jin.......zhi.......challenge...............................................

#define maxn 300
int wa[maxn * 2],wb[maxn * 2],wv[maxn * 2],ws1[maxn * 2];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
void da(int *r,int *sa,int n,int m)
{
     int i,j,p,*x=wa,*y=wb,*t;
     for(i=0;i<m;i++) ws1[i]=0;
     for(i=0;i<n;i++) ws1[x[i]=r[i]]++;
     for(i=1;i<m;i++) ws1[i]+=ws1[i-1];
     for(i=n-1;i>=0;i--) sa[--ws1[x[i]]]=i;
     for(j=1,p=1;p<=n;j*=2,m=p)
     {
       for(p=0,i=n-j;i<n;i++) y[p++]=i;
       for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
       for(i=0;i<n;i++) wv[i]=x[y[i]];
       for(i=0;i<m;i++) ws1[i]=0;
       for(i=0;i<n;i++) ws1[wv[i]]++;
       for(i=1;i<m;i++) ws1[i]+=ws1[i-1];
       for(i=n-1;i>=0;i--) sa[--ws1[wv[i]]]=y[i];
       for(t=x,x=y,y=t,p=2,x[sa[0]]=1,i=1;i<n;i++)
       x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
     }
     return;
}
void calheight(int *r, int *sa, int*rank, int*height,int n)
{
     int i,j,k=0;
     for(i=0; i<n;i++) rank[sa[i]]=i;
     for(i=0;i<n;i++){
    	 if(rank[i] == 0) continue;
	     for(k?k--:0,j=sa[rank[i]-1]; i + k < n && j + k < n && r[i+k]==r[j+k];k++);
	     height[rank[i] - 1]=k;
     }
     return;
}

const int N = maxn;
int d[20];
int st[N][22];
char orig[maxn];
int rank[maxn], h[maxn], str[maxn], sa[maxn], ll[maxn];
int n;
pii yue[maxn], range[maxn];
int sum[10][maxn];

inline int max(const int &arg1, const int &arg2){
return arg1 > arg2 ? arg1 : arg2;
}
void InitRMQ(const int &n){
	int i, j;
	for( d[0]=1, i=1; i < 21; ++i ) d[i] = 2*d[i-1];
	for( i=0; i < n; ++i ) st[i][0] = h[i];
	int k = int( log(double(n))/log(2) ) + 1;
	for( j=1; j < k; ++j )
		for( i=0; i < n; ++i ){
			if( i+d[j-1]-1 < n ){
				st[i][j] = min(st[i][j-1],
						st[i+d[j-1]][j-1]);
			}
			else break; // st[i][j] = st[i][j-1];
		}
}
inline int que(int x, int y){
	int k = int( log(double(y-x+1))/log(2.0) );
	return min(st[x][k], st[y-d[k]+1][k]);
}

int main(){
//	freopen("input.txt", "r", stdin);
	scanf("%s", orig);
	scanf("%d", &n);
	if(n == 0) {
		long long ans = strlen(orig);
		cout << ans * (ans + 1) / 2 << endl;
		return 0;
	}
	int l = strlen(orig);
	ll[0] = l;
	for(int i = 0; i < n; i++){
		l = strlen(orig);
		orig[l] = 'a' + 28 + i;
		l++;
		scanf("%s", orig + l);
		scanf("%d%d", &yue[i].first, &yue[i].second);
		range[i].first = l; range[i].second = strlen(orig);
		if(orig[0] == 'd') {
			printf("%s\n", orig + l);
		}
		ll[i + 1] = strlen(orig);
	}
	if(orig[0] == 'd') {
		return 0;
	}
	l = strlen(orig);
	for(int i = 0; i < l; i++) str[i] = orig[i] - 'a' + 1;
	da(str, sa, l, 40);
	calheight(str, sa, rank, h, l);

	InitRMQ(l);
	for(int i = 0; i < n; i++) if(sa[0] > ll[i] && sa[0] < ll[i + 1]) sum[i][0] = 1;
	for(int i = 1; i < l; i++)  {
		for(int j = 0; j < n; j++) {
			sum[j][i] = sum[j][i - 1];
			if(sa[i] > ll[j] && sa[i] < ll[j + 1]) sum[j][i]++;
		}
	}

	long long ans = 0, last = -1;
	for(int i = 0; i < l; i++) if(sa[i] < ll[0]) {
		int be = 1, en = ll[0] - sa[i];
		bool flag = 1, flag1 = 0;
		while(be < en) {
			int mid = (be + en) / 2;
//			cout << mid << endl;
			int be1 = 0, en1 = i - 1;
			while(be1 < en1) {
				int mid1 = (be1 + en1) / 2;
				if(que(mid1, i - 1) < mid) be1 = mid1 + 1;
				else en1 = mid1;
			}
			if(que(be1, i - 1) < mid) be1++;
			int from = min1(be1, i);
			be1 = i; en1 = l - 1;
			while(be1 < en1) {
				int mid1 = (be1 + en1 + 1) / 2;
				if(que(i, mid1) < mid) en1 = mid1 - 1;
				else be1 = mid1;
			}
			if(que(i, be1) < mid) be1--;
			int to = max1(be1 + 1, i);
	//		cout << from << ' ' << to << endl;
			int fff = 0;
			for(int j = 0; j < n; j++) {
				int ransum = 0;
				if(from == 0) ransum = sum[j][to];
				else ransum = sum[j][to] - sum[j][from - 1];
				if(ransum > yue[j].second) fff |= 2;
				if(ransum < yue[j].first) fff |= 1;
			}
			if(fff == 3) {
				flag = 0;
				break;
			}
			if(fff == 1) en = mid - 1;
			else if(fff == 2) be = mid + 1;
			else if(fff == 0) en = mid, flag1 = 1;
		}

		int mid = be;
		int be1 = 0, en1 = i - 1;
		while(be1 < en1) {
			int mid1 = (be1 + en1) / 2;
			if(que(mid1, i - 1) < mid) be1 = mid1 + 1;
			else en1 = mid1;
		}
		if(que(be1, i - 1) < mid) be1++;
		int from = min1(be1, i);
		be1 = i; en1 = l - 1;
		while(be1 < en1) {
			int mid1 = (be1 + en1 + 1) / 2;
			if(que(i, mid1) < mid) en1 = mid1 - 1;
			else be1 = mid1;
		}
		if(que(i, be1) < mid) be1--;
		int to = max1(be1 + 1, i);
		int fff = 0;
		for(int j = 0; j < n; j++) {
			int ransum = 0;
			if(from == 0) ransum = sum[j][to];
			else ransum = sum[j][to] - sum[j][from - 1];
			if(ransum > yue[j].second) fff |= 2;
			if(ransum < yue[j].first) fff |= 1;
		}

		if(flag && fff == 0) {
			int biao = be;
			int be1 = be, en1 = ll[0] - sa[i];
			bool flag = 1, flag1 = 0;
			while(be1 < en1) {
				int mid = (be1 + en1 + 1) / 2;
				int be11 = 0, en11 = i - 1;
				while(be11 < en11) {
					int mid1 = (be11 + en11) / 2;
					if(que(mid1, i - 1) < mid) be11 = mid1 + 1;
					else en11 = mid1;
				}
				if(que(be11, i - 1) < mid) be11++;
				int from = min1(be11, i);
				be11 = i; en11 = l - 1;
				while(be11 < en11) {
					int mid1 = (be11 + en11 + 1) / 2;
					if(que(i, mid1) < mid) en11 = mid1 - 1;
					else be11 = mid1;
				}
				if(que(i, be11) < mid) be11--;
				int to = max1(be11 + 1, i);
				int fff = 0;
				for(int j = 0; j < n; j++) {
					int ransum = 0;
					if(from == 0) ransum = sum[j][to];
					else ransum = sum[j][to] - sum[j][from - 1];
					if(ransum > yue[j].second) fff |= 2;
					if(ransum < yue[j].first) fff |= 1;
				}
				if(fff == 3) {
					flag = 0;
					break;
				}
				if(fff == 1) en1 = mid - 1;
				if(fff == 2) be1 = mid + 1;
				if(fff == 0) be1 = mid, flag1 = 1;
			}
			long long rans = (be1 - biao + 1);
			if(last != -1) rans -= max1(que(last, i - 1) - biao + 1, 0);
			ans += max(rans, 0);
			last = i;
		}
	}
	cout << ans << endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cctype>
#define maxm 20
#define maxn 600000
using namespace std;
int Sa[maxn],rank[maxn],height[maxn],near[maxn];
int l[maxm],r[maxm],pos[maxn],Log[maxn];
int n,m,len,sum[maxn][maxm],F[maxm][maxn];
char str[maxn];
void read()
{
    scanf("%s %d",str+1,&n);
    m=strlen(str+1),len=m;
    for(int i=1;i<=n;++i)
    {
        str[++m]='$';
        scanf("%s %d %d",str+m+1,l+i,r+i);
        for(;str[m];++m)
            pos[m]=i;
        --m;
    }
}
void sort(int *a,int *b,int *c,int n,int m)
{
    static int cnt[maxn];
    memset(cnt,0,sizeof(int)*(m+1));
    for(int i=1;i<=n;++i)
        ++cnt[c[i]];
    for(int i=1;i<=m;++i)
        cnt[i]+=cnt[i-1];
    for(int i=n;i>=1;--i)
        b[cnt[c[a[i]]]--]=a[i];
}
void make_Sa(int n)
{
    static int x[maxn],y[maxn];
    for(int i=1;i<=n;++i)
        rank[i]=i,x[i]=str[i];
    sort(rank,Sa,x,n,256);
    rank[Sa[1]]=1;
    for(int i=2;i<=n;++i)
        rank[Sa[i]]=rank[Sa[i-1]]+(x[Sa[i]]!=x[Sa[i-1]]);
    for(int i=1;i<=n;i<<=1)
    {
        for(int j=1;j<=n;++j)
            Sa[j]=j,x[j]=rank[j],y[j]=j+i<=n?rank[j+i]:0;
        sort(Sa,rank,y,n,n),sort(rank,Sa,x,n,n);
        rank[Sa[1]]=1;
        for(int j=2;j<=n;++j)
            rank[Sa[j]]=rank[Sa[j-1]]+(x[Sa[j]]!=x[Sa[j-1]]||y[Sa[j]]!=y[Sa[j-1]]);
        if(rank[Sa[n]]==n)
            return ;
    }
}
void make_Height(int n)
{
    for(int i=1,j=0;i<=n;++i)
    {
        if(j)
            --j;
        if(rank[i]!=1)
            while(str[i+j]==str[Sa[rank[i]-1]+j])
                ++j;
        height[rank[i]]=j;
    }
}
int LCP(int a,int b)
{
    ++a;
    int h=Log[b-a+1];
    return min(F[h][a],F[h][b-(1<<h)+1]);
}
int calc(int a,int b,int x)
{
    int L,R,l,r,id=rank[a];
    l=1,r=id;
    while(l<r)
    {
        int mid=(l+r)>>1;
        LCP(mid,id)<b-a+1?l=mid+1:r=mid;
    }
    L=l;
    l=id+1,r=m+1;
    while(l<r)
    {
        int mid=(l+r)>>1;
        LCP(id,mid)>=b-a+1?l=mid+1:r=mid;
    }
    R=--r;
    return sum[R][x]-sum[L-1][x];
}
int work()
{
    static int tol[maxm],tor[maxm];
    int ans=0;
    for(int i=1;isalpha(str[i]);++i)
    {
        int L=i,R=len+1;
        for(int j=1;j<=n;++j)
        {
            tol[j]=max(tol[j],i),tor[j]=max(tor[j],i);
            while(isalpha(str[tol[j]])&&calc(i,tol[j],j)>=l[j])
                ++tol[j];
            while(isalpha(str[tor[j]])&&calc(i,tor[j],j)>r[j])
                ++tor[j];
            L=max(L,tor[j]),R=min(R,tol[j]);
        }
        if(near[rank[i]])
            L=max(L,i+LCP(near[rank[i]],rank[i]));
        ans+=max(0,R-L);
    }
    return ans;
}
void Prepare()
{
    for(int i=2;i<=m;++i)
        Log[i]=Log[i-1]+(i==(i&-i));
    for(int i=1;i<=m;++i)
    {
        F[0][i]=height[i];
        for(int j=1;j<=n;++j)
            sum[i][j]=sum[i-1][j];
        ++sum[i][pos[Sa[i]]];
        near[i]=Sa[i-1]<=len?i-1:near[i-1];
    }
    for(int i=1;i<=Log[m];++i)
        for(int j=1;j+(1<<i)-1<=m;++j)
            F[i][j]=min(F[i-1][j],F[i-1][j+(1<<(i-1))]);
}
int main()
{
    read();
    make_Sa(m);
    make_Height(m);
    Prepare();
    printf("%d\n",work());
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cctype>
#define maxm 20
#define maxn 600000
using namespace std;
int Sa[maxn],rank[maxn],height[maxn],near[maxn];
int l[maxm],r[maxm],pos[maxn],Log[maxn];
int n,m,len,sum[maxn][maxm],F[maxm][maxn];
char str[maxn];
void read()
{
    scanf("%s %d",str+1,&n);
    m=strlen(str+1),len=m;
    for(int i=1;i<=n;++i)
    {
        str[++m]='$';
        scanf("%s %d %d",str+m+1,l+i,r+i);
        for(;str[m];++m)
            pos[m]=i;
        --m;
    }
}
void sort(int *a,int *b,int *c,int n,int m)
{
    static int cnt[maxn];
    memset(cnt,0,sizeof(int)*(m+1));
    for(int i=1;i<=n;++i)
        ++cnt[c[i]];
    for(int i=1;i<=m;++i)
        cnt[i]+=cnt[i-1];
    for(int i=n;i>=1;--i)
        b[cnt[c[a[i]]]--]=a[i];
}
void make_Sa(int n)
{
    static int x[maxn],y[maxn];
    for(int i=1;i<=n;++i)
        rank[i]=i,x[i]=str[i];
    sort(rank,Sa,x,n,256);
    rank[Sa[1]]=1;
    for(int i=2;i<=n;++i)
        rank[Sa[i]]=rank[Sa[i-1]]+(x[Sa[i]]!=x[Sa[i-1]]);
    for(int i=1;i<=n;i<<=1)
    {
        for(int j=1;j<=n;++j)
            Sa[j]=j,x[j]=rank[j],y[j]=j+i<=n?rank[j+i]:0;
        sort(Sa,rank,y,n,n),sort(rank,Sa,x,n,n);
        rank[Sa[1]]=1;
        for(int j=2;j<=n;++j)
            rank[Sa[j]]=rank[Sa[j-1]]+(x[Sa[j]]!=x[Sa[j-1]]||y[Sa[j]]!=y[Sa[j-1]]);
        if(rank[Sa[n]]==n)
            return ;
    }
}
void make_Height(int n)
{
    for(int i=1,j=0;i<=n;++i)
    {
        if(j)
            --j;
        if(rank[i]!=1)
            while(str[i+j]==str[Sa[rank[i]-1]+j])
                ++j;
        height[rank[i]]=j;
    }
}
int LCP(int a,int b)
{
    ++a;
    int h=Log[b-a+1];
    return min(F[h][a],F[h][b-(1<<h)+1]);
}
int calc(int a,int b,int x)
{
    int L,R,l,r,id=rank[a];
    l=1,r=id;
    while(l<r)
    {
        int mid=(l+r)>>1;
        LCP(mid,id)<b-a+1?l=mid+1:r=mid;
    }
    L=l;
    l=id+1,r=m+1;
    while(l<r)
    {
        int mid=(l+r)>>1;
        LCP(id,mid)>=b-a+1?l=mid+1:r=mid;
    }
    R=--r;
    return sum[R][x]-sum[L-1][x];
}
int work()
{
    static int tol[maxm],tor[maxm];
    int ans=0;
    for(int i=1;isalpha(str[i]);++i)
    {
        int L=i,R=len+1;
        for(int j=1;j<=n;++j)
        {
            tol[j]=max(tol[j],i),tor[j]=max(tor[j],i);
            while(isalpha(str[tol[j]])&&calc(i,tol[j],j)>=l[j])
                ++tol[j];
            while(isalpha(str[tor[j]])&&calc(i,tor[j],j)>r[j])
                ++tor[j];
            L=max(L,tor[j]),R=min(R,tol[j]);
        }
        if(near[rank[i]])
            L=max(L,i+LCP(near[rank[i]],rank[i]));
        ans+=max(0,R-L);
        if(R>L)
            printf("%d %d %d\n",i,L,R);
    }
    return ans;
}
void Prepare()
{
    for(int i=2;i<=m;++i)
        Log[i]=Log[i-1]+(i==(i&-i));
    for(int i=1;i<=m;++i)
    {
        F[0][i]=height[i];
        for(int j=1;j<=n;++j)
            sum[i][j]=sum[i-1][j];
        ++sum[i][pos[Sa[i]]];
        near[i]=Sa[i-1]<=len?i-1:near[i-1];
    }
    for(int i=1;i<=Log[m];++i)
        for(int j=1;j+(1<<i)-1<=m;++j)
            F[i][j]=min(F[i-1][j],F[i-1][j+(1<<(i-1))]);
}
int main()
{
    read();
    make_Sa(m);
    make_Height(m);
    Prepare();
    printf("%d\n",work());
    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
#include<cassert>
//#include<bits/stdc++.h>适用于CF,UOJ，但不适用于poj

using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef pair<DB,DB> PDD;
typedef complex<DB> CP;
typedef vector<int> VI;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define fi first
#define se second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))
#define SZ(x) (int(x.size()))
#define all(x) (x).begin(),(x).end()
#define ire(i,v,x) for(i=0,v=i<SZ(x)?x[i]:0;i<SZ(x);v=x[++i])

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

inline int sgn(DB x){if(abs(x)<1e-9)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

int gint()
  {
		int res=0;bool neg=0;char z;
		for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
		if(z==EOF)return 0;
		if(z=='-'){neg=1;z=getchar();}
		for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
		return (neg)?-res:res;
	}
LL gll()
  {
  	LL res=0;bool neg=0;char z;
		for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
		if(z==EOF)return 0;
		if(z=='-'){neg=1;z=getchar();}
		for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
		return (neg)?-res:res;
	}

const int maxn=51000*2;

char s[maxn];
int m,lo[13],ro[13];

struct SAM
  {
  	int ch[13*maxn][26],fail[13*maxn],len[13*maxn],idx,cnt[13*maxn][13];
  	int add(int x,int last)
  	  {
  	  	int now=++idx,p=last;
  	  	len[now]=len[p]+1;
  	  	for(;p && !ch[p][x];p=fail[p])ch[p][x]=now;
  	  	if(!p)
  	  	  fail[now]=1;
  	  	else
  	  	  {
  	  	  	int q=ch[p][x];
  	  	  	if(len[p]+1==len[q])
  	  	  	  fail[now]=q;
  	  	  	else
  	  	  	  {
  	  	  	  	int nq=++idx;
  	  	  	  	mmcy(ch[nq],ch[q]);fail[nq]=fail[q];
  	  	  	  	len[nq]=len[p]+1;
  	  	  	  	fail[q]=fail[now]=nq;
  	  	  	  	for(;p && ch[p][x]==q;p=fail[p])ch[p][x]=nq;
  	  	  	  }
  	  	  }
  	  	return now;
  	  }
  	void insert(char *s,int idx)
  	  {
  	  	int i,n=strlen(s+1),last=1;
  	  	re(i,1,n)last=add(s[i]-'a',last),cnt[last][idx]++;
  	  }
  	queue<int>Q;int deg[10*maxn];
  	int calc()
  	  {
  	  	int res=0,i,j;
  	  	re(i,1,idx)deg[fail[i]]++;
  	  	re(i,1,idx)if(!deg[i])Q.push(i);
  	  	while(!Q.empty())
  	  	  {
  	  	  	int u=Q.front();Q.pop();
  	  	  	if(u<=1)continue;
  	  	  	int ok=1;
  	  	  	re(j,1,m)if(cnt[u][j]<lo[j] || ro[j]<cnt[u][j]){ok=0;break;}
  	  	  	if(!cnt[u][0])ok=0;
  	  	  	if(ok)res+=len[u]-len[fail[u]];
  	  	  	re(j,0,m)cnt[fail[u]][j]+=cnt[u][j];
  	  	  	deg[fail[u]]--;
  	  	  	if(!deg[fail[u]])Q.push(fail[u]);
  	  	  }
  	  	return res;
  	  }
  }sam;

int main()
  {
  	/*freopen("CF316G3.in","r",stdin);
  	freopen("CF316G3.out","w",stdout);*/
  	int i;
  	sam.idx=1;
  	SF("%s\n",s+1);
  	sam.insert(s,0);
  	m=gint();
  	re(i,1,m)
  	  {
  	  	SF("%s",s+1);lo[i]=gint(),ro[i]=gint();
				sam.insert(s,i);
  	  }
  	cout<<sam.calc()<<endl;
  	return 0;
  }


//program 316-G

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

struct Suffix
{
  int Num,X;
};

int Get()
{
  char c;
  while(c=getchar(),(c<'0'||c>'9')&&(c!='-'));
  bool Flag=(c=='-');
  if(Flag)
    c=getchar();
  int X=0;
  while(c>='0'&&c<='9')
    {
      X=X*10+c-48;
      c=getchar();
    }
  return Flag?-X:X;
}

void Output(int X)
{
  if(X<0)
    {
      putchar('-');
      X=-X;
    }
  int Len=0,Data[10];
  while(X)
    {
      Data[Len++]=X%10;
      X/=10;
    }
  if(!Len)
    Data[Len++]=0;
  while(Len--)
    putchar(Data[Len]+48);
  putchar('\n');
}

const int Base=19961021;
const int inf=1000000000;

int N;
string S[11];
long long Hash[11][50001];
long long Pow[50001];
Suffix SA[11][100000];
int Height[11][100000],Pos[11][50000],Sum[11][100000];
int ST[17][11][100000],Log[100000];

long long GetHash(int P,int X,int Len)
{
  return Hash[P][X+Len]-Hash[P][X]*Pow[Len];
}

int LCP(Suffix A,Suffix B)
{
  int Left=0,Right=min(S[A.Num].size()-A.X,S[B.Num].size()-B.X);
  while(Left<Right)
    {
      int Mid=(Left+Right+1)/2;
      if(GetHash(A.Num,A.X,Mid)==GetHash(B.Num,B.X,Mid))
        Left=Mid;
      else
        Right=Mid-1;
    }
  return Left;
}

bool operator <(Suffix A,Suffix B)
{
  int Len=LCP(A,B);
  if(B.X+Len==S[B.Num].size())
    return false;
  if(A.X+Len==S[A.Num].size())
    return true;
  return S[A.Num][A.X+Len]<S[B.Num][B.X+Len];
}

int GetMin(int T,int L,int R)
{
  int P=Log[R-L+1];
  return min(ST[T][P][L],ST[T][P][R-(1<<P)+1]);
}

int Solve(int T,int L,int R)
{
  if(L>R)
    return inf;
  if(R==S[0].size())
    return -inf;
  int Len=R-L+1,X=Pos[T][L];
  int Left=0,Right=X;
  while(Left<Right)
    {
      int Mid=(Left+Right)/2;
      if(GetMin(T,Mid+1,X)>=Len)
        Right=Mid;
      else
        Left=Mid+1;
    }
  int LB=Left;
  Left=X;
  Right=S[0].size()+S[T].size();
  while(Left<Right)
    {
      int Mid=(Left+Right+1)/2;
      if(GetMin(T,X+1,Mid)>=Len)
        Left=Mid;
      else
        Right=Mid-1;
    }
  int UB=Left;
  return Sum[T][UB+1]-Sum[T][LB];
}

int main()
{
  cin>>S[0];
  N=Get();
  static int LB[11],UB[11];
  for(int i=1;i<=N;i++)
    cin>>S[i]>>LB[i]>>UB[i];
  for(int i=0;i<=N;i++)
    {
      Hash[i][0]=0;
      for(int j=0;j<S[i].size();j++)
        Hash[i][j+1]=Hash[i][j]*Base+S[i][j];
    }
  Pow[0]=1;
  for(int i=0;i<50000;i++)
    Pow[i+1]=Pow[i]*Base;
  for(int i=1;i<=N;i++)
    {
      for(int j=0;j<S[0].size();j++)
        SA[i][j]=(Suffix){0,j};
      for(int j=0;j<S[i].size();j++)
        SA[i][S[0].size()+j]=(Suffix){i,j};
      int M=S[0].size()+S[i].size();
      sort(SA[i],SA[i]+M);
      for(int j=0;j<M;j++)
        if(!SA[i][j].Num)
          Pos[i][SA[i][j].X]=j;
      for(int j=1;j<M;j++)
        Height[i][j]=LCP(SA[i][j],SA[i][j-1]);
      Sum[i][0]=0;
      for(int j=0;j<M;j++)
        {
          Sum[i][j+1]=Sum[i][j];
          if(SA[i][j].Num)
            Sum[i][j+1]++;
        }
      for(int j=0;j<M;j++)
        ST[i][0][j]=Height[i][j];
      for(int j=1;j<17;j++)
        for(int k=0;k<=M-(1<<j);k++)
          ST[i][j][k]=min(ST[i][j-1][k],ST[i][j-1][k+(1<<(j-1))]);
    }
  for(int i=1,j=0;i<=100000;i++)
    {
      if((1<<(j+1))<=i)
        j++;
      Log[i]=j;
    }
  static int L[50000],R[50000];
  int Len0=S[0].size();
  for(int i=0,P=-1,Q=-1;i<Len0;i++)
    {
      while(P<Len0)
        {
          bool Flag=true;
          for(int j=1;j<=N;j++)
            if(Solve(j,i,P)>UB[j])
              Flag=false;
          if(Flag)
            break;
          P++;
        }
      while(Q<Len0)
        {
          bool Flag=true;
          for(int j=1;j<=N;j++)
            if(Solve(j,i,Q+1)<LB[j])
              Flag=false;
          if(!Flag)
            break;
          Q++;
        }
      L[i]=P;
      R[i]=Q;
    }
  static Suffix Data[50000];
  for(int i=0;i<Len0;i++)
    Data[i]=(Suffix){0,i};
  sort(Data,Data+Len0);
  long long Ans=0;
  for(int i=0;i<Len0;i++)
    {
      int Min=L[Data[i].X],Max=R[Data[i].X];
      Min=max(Min,Data[i].X);
      Max=min(Max,Len0-1);
      if(i)
        Min=max(Min,Data[i].X+LCP(Data[i],Data[i-1]));
      Ans+=max(Max-Min+1,0);
    }
  cout<<Ans<<endl;
  return 0;
}


//program 316-G

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

struct Suffix
{
  int Num,X;
};

int Get()
{
  char c;
  while(c=getchar(),(c<'0'||c>'9')&&(c!='-'));
  bool Flag=(c=='-');
  if(Flag)
    c=getchar();
  int X=0;
  while(c>='0'&&c<='9')
    {
      X=X*10+c-48;
      c=getchar();
    }
  return Flag?-X:X;
}

void Output(int X)
{
  if(X<0)
    {
      putchar('-');
      X=-X;
    }
  int Len=0,Data[10];
  while(X)
    {
      Data[Len++]=X%10;
      X/=10;
    }
  if(!Len)
    Data[Len++]=0;
  while(Len--)
    putchar(Data[Len]+48);
  putchar('\n');
}

const int Base=19961021;
const int inf=1000000000;

int N;
string S[11];
long long Hash[11][50001];
long long Pow[50001];
Suffix SA[11][100000];
int Height[11][100000],Pos[11][50000],Sum[11][100001];
int ST[11][17][100000],Log[100001];

long long GetHash(int P,int X,int Len)
{
  return Hash[P][X+Len]-Hash[P][X]*Pow[Len];
}

int LCP(Suffix A,Suffix B)
{
  int Left=0,Right=min(S[A.Num].size()-A.X,S[B.Num].size()-B.X);
  while(Left<Right)
    {
      int Mid=(Left+Right+1)/2;
      if(GetHash(A.Num,A.X,Mid)==GetHash(B.Num,B.X,Mid))
        Left=Mid;
      else
        Right=Mid-1;
    }
  return Left;
}

bool operator <(Suffix A,Suffix B)
{
  int Len=LCP(A,B);
  if(B.X+Len==S[B.Num].size())
    return false;
  if(A.X+Len==S[A.Num].size())
    return true;
  return S[A.Num][A.X+Len]<S[B.Num][B.X+Len];
}

int GetMin(int T,int L,int R)
{
  int P=Log[R-L+1];
  return min(ST[T][P][L],ST[T][P][R-(1<<P)+1]);
}

int Solve(int T,int L,int R)
{
  if(L>R)
    return inf;
  if(R==S[0].size())
    return -inf;
  int Len=R-L+1,X=Pos[T][L];
  int Left=0,Right=X;
  while(Left<Right)
    {
      int Mid=(Left+Right)/2;
      if(GetMin(T,Mid+1,X)>=Len)
        Right=Mid;
      else
        Left=Mid+1;
    }
  int LB=Left;
  Left=X;
  Right=S[0].size()+S[T].size()-1;
  while(Left<Right)
    {
      int Mid=(Left+Right+1)/2;
      if(GetMin(T,X+1,Mid)>=Len)
        Left=Mid;
      else
        Right=Mid-1;
    }
  int UB=Left;
  return Sum[T][UB+1]-Sum[T][LB];
}

int main()
{
  cin>>S[0];
  N=Get();
  static int LB[11],UB[11];
  for(int i=1;i<=N;i++)
    cin>>S[i]>>LB[i]>>UB[i];
  for(int i=0;i<=N;i++)
    {
      Hash[i][0]=0;
      for(int j=0;j<S[i].size();j++)
        Hash[i][j+1]=Hash[i][j]*Base+S[i][j];
    }
  Pow[0]=1;
  for(int i=0;i<50000;i++)
    Pow[i+1]=Pow[i]*Base;
  for(int i=1;i<=N;i++)
    {
      for(int j=0;j<S[0].size();j++)
        SA[i][j]=(Suffix){0,j};
      for(int j=0;j<S[i].size();j++)
        SA[i][S[0].size()+j]=(Suffix){i,j};
      int M=S[0].size()+S[i].size();
      sort(SA[i],SA[i]+M);
      for(int j=0;j<M;j++)
        if(!SA[i][j].Num)
          Pos[i][SA[i][j].X]=j;
      for(int j=1;j<M;j++)
        Height[i][j]=LCP(SA[i][j],SA[i][j-1]);
      Sum[i][0]=0;
      for(int j=0;j<M;j++)
        {
          Sum[i][j+1]=Sum[i][j];
          if(SA[i][j].Num)
            Sum[i][j+1]++;
        }
      for(int j=0;j<M;j++)
        ST[i][0][j]=Height[i][j];
      for(int j=1;j<17;j++)
        for(int k=0;k<=M-(1<<j);k++)
          ST[i][j][k]=min(ST[i][j-1][k],ST[i][j-1][k+(1<<(j-1))]);
    }
  for(int i=1,j=0;i<=100000;i++)
    {
      if((1<<(j+1))<=i)
        j++;
      Log[i]=j;
    }
  static int L[50000],R[50000];
  int Len0=S[0].size();
  for(int i=0,P=-1,Q=-1;i<Len0;i++)
    {
      while(P<Len0)
        {
          bool Flag=true;
          for(int j=1;j<=N;j++)
            if(Solve(j,i,P)>UB[j])
              Flag=false;
          if(Flag)
            break;
          P++;
        }
      while(Q+1<Len0)
        {
          bool Flag=true;
          for(int j=1;j<=N;j++)
            if(Solve(j,i,Q+1)<LB[j])
              Flag=false;
          if(!Flag)
            break;
          Q++;
        }
      L[i]=P;
      R[i]=Q;
    }
  static Suffix Data[50000];
  for(int i=0;i<Len0;i++)
    Data[i]=(Suffix){0,i};
  sort(Data,Data+Len0);
  long long Ans=0;
  for(int i=0;i<Len0;i++)
    {
      int Min=L[Data[i].X],Max=R[Data[i].X];
      Min=max(Min,Data[i].X);
      Max=min(Max,Len0-1);
      if(i)
        Min=max(Min,Data[i].X+LCP(Data[i],Data[i-1]));
      Ans+=max(Max-Min+1,0);
    }
  cout<<Ans<<endl;
  return 0;
}


//program 316-G

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

struct Suffix
{
  int Num,X;
};

int Get()
{
  char c;
  while(c=getchar(),(c<'0'||c>'9')&&(c!='-'));
  bool Flag=(c=='-');
  if(Flag)
    c=getchar();
  int X=0;
  while(c>='0'&&c<='9')
    {
      X=X*10+c-48;
      c=getchar();
    }
  return Flag?-X:X;
}

void Output(int X)
{
  if(X<0)
    {
      putchar('-');
      X=-X;
    }
  int Len=0,Data[10];
  while(X)
    {
      Data[Len++]=X%10;
      X/=10;
    }
  if(!Len)
    Data[Len++]=0;
  while(Len--)
    putchar(Data[Len]+48);
  putchar('\n');
}

const int Base=19961021;
const int inf=1000000000;

int N;
string S[11];
long long Hash[11][50001];
long long Pow[50001];
Suffix SA[11][100000];
int Height[11][100000],Pos[11][50000],Sum[11][100001];
int ST[17][11][100000],Log[100001];

long long GetHash(int P,int X,int Len)
{
  return Hash[P][X+Len]-Hash[P][X]*Pow[Len];
}

int LCP(Suffix A,Suffix B)
{
  int Left=0,Right=min(S[A.Num].size()-A.X,S[B.Num].size()-B.X);
  while(Left<Right)
    {
      int Mid=(Left+Right+1)/2;
      if(GetHash(A.Num,A.X,Mid)==GetHash(B.Num,B.X,Mid))
        Left=Mid;
      else
        Right=Mid-1;
    }
  return Left;
}

bool operator <(Suffix A,Suffix B)
{
  int Len=LCP(A,B);
  if(B.X+Len==S[B.Num].size())
    return false;
  if(A.X+Len==S[A.Num].size())
    return true;
  return S[A.Num][A.X+Len]<S[B.Num][B.X+Len];
}

int GetMin(int T,int L,int R)
{
  int P=Log[R-L+1];
  return min(ST[T][P][L],ST[T][P][R-(1<<P)+1]);
}

int Solve(int T,int L,int R)
{
  if(L>R)
    return inf;
  if(R==S[0].size())
    return -inf;
  int Len=R-L+1,X=Pos[T][L];
  int Left=0,Right=X;
  while(Left<Right)
    {
      int Mid=(Left+Right)/2;
      if(GetMin(T,Mid+1,X)>=Len)
        Right=Mid;
      else
        Left=Mid+1;
    }
  int LB=Left;
  Left=X;
  Right=S[0].size()+S[T].size()-1;
  while(Left<Right)
    {
      int Mid=(Left+Right+1)/2;
      if(GetMin(T,X+1,Mid)>=Len)
        Left=Mid;
      else
        Right=Mid-1;
    }
  int UB=Left;
  return Sum[T][UB+1]-Sum[T][LB];
}

int main()
{
  cin>>S[0];
  N=Get();
  static int LB[11],UB[11];
  for(int i=1;i<=N;i++)
    cin>>S[i]>>LB[i]>>UB[i];
  for(int i=0;i<=N;i++)
    {
      Hash[i][0]=0;
      for(int j=0;j<S[i].size();j++)
        Hash[i][j+1]=Hash[i][j]*Base+S[i][j];
    }
  Pow[0]=1;
  for(int i=0;i<50000;i++)
    Pow[i+1]=Pow[i]*Base;
  for(int i=1;i<=N;i++)
    {
      for(int j=0;j<S[0].size();j++)
        SA[i][j]=(Suffix){0,j};
      for(int j=0;j<S[i].size();j++)
        SA[i][S[0].size()+j]=(Suffix){i,j};
      int M=S[0].size()+S[i].size();
      sort(SA[i],SA[i]+M);
      for(int j=0;j<M;j++)
        if(!SA[i][j].Num)
          Pos[i][SA[i][j].X]=j;
      for(int j=1;j<M;j++)
        Height[i][j]=LCP(SA[i][j],SA[i][j-1]);
      Sum[i][0]=0;
      for(int j=0;j<M;j++)
        {
          Sum[i][j+1]=Sum[i][j];
          if(SA[i][j].Num)
            Sum[i][j+1]++;
        }
      for(int j=0;j<M;j++)
        ST[i][0][j]=Height[i][j];
      for(int j=1;j<17;j++)
        for(int k=0;k<=M-(1<<j);k++)
          ST[i][j][k]=min(ST[i][j-1][k],ST[i][j-1][k+(1<<(j-1))]);
    }
  for(int i=1,j=0;i<=100000;i++)
    {
      if((1<<(j+1))<=i)
        j++;
      Log[i]=j;
    }
  static int L[50000],R[50000];
  int Len0=S[0].size();
  for(int i=0,P=-1,Q=-1;i<Len0;i++)
    {
      while(P<Len0)
        {
          bool Flag=true;
          for(int j=1;j<=N;j++)
            if(Solve(j,i,P)>UB[j])
              Flag=false;
          if(Flag)
            break;
          P++;
        }
      while(Q<Len0)
        {
          bool Flag=true;
          for(int j=1;j<=N;j++)
            if(Solve(j,i,Q+1)<LB[j])
              Flag=false;
          if(!Flag)
            break;
          Q++;
        }
      L[i]=P;
      R[i]=Q;
    }
  static Suffix Data[50000];
  for(int i=0;i<Len0;i++)
    Data[i]=(Suffix){0,i};
  sort(Data,Data+Len0);
  long long Ans=0;
  for(int i=0;i<Len0;i++)
    {
      int Min=L[Data[i].X],Max=R[Data[i].X];
      Min=max(Min,Data[i].X);
      Max=min(Max,Len0-1);
      if(i)
        Min=max(Min,Data[i].X+LCP(Data[i],Data[i-1]));
      Ans+=max(Max-Min+1,0);
    }
  cout<<Ans<<endl;
  return 0;
}


//program 316-G

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

struct Suffix
{
  int Num,X;
};

int Get()
{
  char c;
  while(c=getchar(),(c<'0'||c>'9')&&(c!='-'));
  bool Flag=(c=='-');
  if(Flag)
    c=getchar();
  int X=0;
  while(c>='0'&&c<='9')
    {
      X=X*10+c-48;
      c=getchar();
    }
  return Flag?-X:X;
}

void Output(int X)
{
  if(X<0)
    {
      putchar('-');
      X=-X;
    }
  int Len=0,Data[10];
  while(X)
    {
      Data[Len++]=X%10;
      X/=10;
    }
  if(!Len)
    Data[Len++]=0;
  while(Len--)
    putchar(Data[Len]+48);
  putchar('\n');
}

const int Base=19961021;
const int inf=1000000000;

int N;
string S[11];
long long Hash[11][50001];
long long Pow[50001];
Suffix SA[11][100000];
int Height[11][100000],Pos[11][50000],Sum[11][100001];
int ST[17][11][100000],Log[100000];

long long GetHash(int P,int X,int Len)
{
  return Hash[P][X+Len]-Hash[P][X]*Pow[Len];
}

int LCP(Suffix A,Suffix B)
{
  int Left=0,Right=min(S[A.Num].size()-A.X,S[B.Num].size()-B.X);
  while(Left<Right)
    {
      int Mid=(Left+Right+1)/2;
      if(GetHash(A.Num,A.X,Mid)==GetHash(B.Num,B.X,Mid))
        Left=Mid;
      else
        Right=Mid-1;
    }
  return Left;
}

bool operator <(Suffix A,Suffix B)
{
  int Len=LCP(A,B);
  if(B.X+Len==S[B.Num].size())
    return false;
  if(A.X+Len==S[A.Num].size())
    return true;
  return S[A.Num][A.X+Len]<S[B.Num][B.X+Len];
}

int GetMin(int T,int L,int R)
{
  int P=Log[R-L+1];
  return min(ST[T][P][L],ST[T][P][R-(1<<P)+1]);
}

int Solve(int T,int L,int R)
{
  if(L>R)
    return inf;
  if(R==S[0].size())
    return -inf;
  int Len=R-L+1,X=Pos[T][L];
  int Left=0,Right=X;
  while(Left<Right)
    {
      int Mid=(Left+Right)/2;
      if(GetMin(T,Mid+1,X)>=Len)
        Right=Mid;
      else
        Left=Mid+1;
    }
  int LB=Left;
  Left=X;
  Right=S[0].size()+S[T].size()-1;
  while(Left<Right)
    {
      int Mid=(Left+Right+1)/2;
      if(GetMin(T,X+1,Mid)>=Len)
        Left=Mid;
      else
        Right=Mid-1;
    }
  int UB=Left;
  return Sum[T][UB+1]-Sum[T][LB];
}

int main()
{
  cin>>S[0];
  N=Get();
  static int LB[11],UB[11];
  for(int i=1;i<=N;i++)
    cin>>S[i]>>LB[i]>>UB[i];
  for(int i=0;i<=N;i++)
    {
      Hash[i][0]=0;
      for(int j=0;j<S[i].size();j++)
        Hash[i][j+1]=Hash[i][j]*Base+S[i][j];
    }
  Pow[0]=1;
  for(int i=0;i<50000;i++)
    Pow[i+1]=Pow[i]*Base;
  for(int i=1;i<=N;i++)
    {
      for(int j=0;j<S[0].size();j++)
        SA[i][j]=(Suffix){0,j};
      for(int j=0;j<S[i].size();j++)
        SA[i][S[0].size()+j]=(Suffix){i,j};
      int M=S[0].size()+S[i].size();
      sort(SA[i],SA[i]+M);
      for(int j=0;j<M;j++)
        if(!SA[i][j].Num)
          Pos[i][SA[i][j].X]=j;
      for(int j=1;j<M;j++)
        Height[i][j]=LCP(SA[i][j],SA[i][j-1]);
      Sum[i][0]=0;
      for(int j=0;j<M;j++)
        {
          Sum[i][j+1]=Sum[i][j];
          if(SA[i][j].Num)
            Sum[i][j+1]++;
        }
      for(int j=0;j<M;j++)
        ST[i][0][j]=Height[i][j];
      for(int j=1;j<17;j++)
        for(int k=0;k<=M-(1<<j);k++)
          ST[i][j][k]=min(ST[i][j-1][k],ST[i][j-1][k+(1<<(j-1))]);
    }
  for(int i=1,j=0;i<=100000;i++)
    {
      if((1<<(j+1))<=i)
        j++;
      Log[i]=j;
    }
  static int L[50000],R[50000];
  int Len0=S[0].size();
  for(int i=0,P=-1,Q=-1;i<Len0;i++)
    {
      while(P<Len0)
        {
          bool Flag=true;
          for(int j=1;j<=N;j++)
            if(Solve(j,i,P)>UB[j])
              Flag=false;
          if(Flag)
            break;
          P++;
        }
      while(Q<Len0)
        {
          bool Flag=true;
          for(int j=1;j<=N;j++)
            if(Solve(j,i,Q+1)<LB[j])
              Flag=false;
          if(!Flag)
            break;
          Q++;
        }
      L[i]=P;
      R[i]=Q;
    }
  static Suffix Data[50000];
  for(int i=0;i<Len0;i++)
    Data[i]=(Suffix){0,i};
  sort(Data,Data+Len0);
  long long Ans=0;
  for(int i=0;i<Len0;i++)
    {
      int Min=L[Data[i].X],Max=R[Data[i].X];
      Min=max(Min,Data[i].X);
      Max=min(Max,Len0-1);
      if(i)
        Min=max(Min,Data[i].X+LCP(Data[i],Data[i-1]));
      Ans+=max(Max-Min+1,0);
    }
  cout<<Ans<<endl;
  return 0;
}


//program 316-G

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

struct Suffix
{
  int Num,X;
};

int Get()
{
  char c;
  while(c=getchar(),(c<'0'||c>'9')&&(c!='-'));
  bool Flag=(c=='-');
  if(Flag)
    c=getchar();
  int X=0;
  while(c>='0'&&c<='9')
    {
      X=X*10+c-48;
      c=getchar();
    }
  return Flag?-X:X;
}

void Output(int X)
{
  if(X<0)
    {
      putchar('-');
      X=-X;
    }
  int Len=0,Data[10];
  while(X)
    {
      Data[Len++]=X%10;
      X/=10;
    }
  if(!Len)
    Data[Len++]=0;
  while(Len--)
    putchar(Data[Len]+48);
  putchar('\n');
}

const int Base=19961021;
const int inf=1000000000;

int N;
string S[11];
long long Hash[11][50001];
long long Pow[50001];
Suffix SA[11][100000];
int Height[11][100000],Pos[11][50000],Sum[11][100001];
int ST[17][11][100000],Log[100000];

long long GetHash(int P,int X,int Len)
{
  return Hash[P][X+Len]-Hash[P][X]*Pow[Len];
}

int LCP(Suffix A,Suffix B)
{
  int Left=0,Right=min(S[A.Num].size()-A.X,S[B.Num].size()-B.X);
  while(Left<Right)
    {
      int Mid=(Left+Right+1)/2;
      if(GetHash(A.Num,A.X,Mid)==GetHash(B.Num,B.X,Mid))
        Left=Mid;
      else
        Right=Mid-1;
    }
  return Left;
}

bool operator <(Suffix A,Suffix B)
{
  int Len=LCP(A,B);
  if(B.X+Len==S[B.Num].size())
    return false;
  if(A.X+Len==S[A.Num].size())
    return true;
  return S[A.Num][A.X+Len]<S[B.Num][B.X+Len];
}

int GetMin(int T,int L,int R)
{
  int P=Log[R-L+1];
  return min(ST[T][P][L],ST[T][P][R-(1<<P)+1]);
}

int Solve(int T,int L,int R)
{
  if(L>R)
    return inf;
  if(R==S[0].size())
    return -inf;
  int Len=R-L+1,X=Pos[T][L];
  int Left=0,Right=X;
  while(Left<Right)
    {
      int Mid=(Left+Right)/2;
      if(GetMin(T,Mid+1,X)>=Len)
        Right=Mid;
      else
        Left=Mid+1;
    }
  int LB=Left;
  Left=X;
  Right=S[0].size()+S[T].size();
  while(Left<Right)
    {
      int Mid=(Left+Right+1)/2;
      if(GetMin(T,X+1,Mid)>=Len)
        Left=Mid;
      else
        Right=Mid-1;
    }
  int UB=Left;
  return Sum[T][UB+1]-Sum[T][LB];
}

int main()
{
  cin>>S[0];
  N=Get();
  static int LB[11],UB[11];
  for(int i=1;i<=N;i++)
    cin>>S[i]>>LB[i]>>UB[i];
  for(int i=0;i<=N;i++)
    {
      Hash[i][0]=0;
      for(int j=0;j<S[i].size();j++)
        Hash[i][j+1]=Hash[i][j]*Base+S[i][j];
    }
  Pow[0]=1;
  for(int i=0;i<50000;i++)
    Pow[i+1]=Pow[i]*Base;
  for(int i=1;i<=N;i++)
    {
      for(int j=0;j<S[0].size();j++)
        SA[i][j]=(Suffix){0,j};
      for(int j=0;j<S[i].size();j++)
        SA[i][S[0].size()+j]=(Suffix){i,j};
      int M=S[0].size()+S[i].size();
      sort(SA[i],SA[i]+M);
      for(int j=0;j<M;j++)
        if(!SA[i][j].Num)
          Pos[i][SA[i][j].X]=j;
      for(int j=1;j<M;j++)
        Height[i][j]=LCP(SA[i][j],SA[i][j-1]);
      Sum[i][0]=0;
      for(int j=0;j<M;j++)
        {
          Sum[i][j+1]=Sum[i][j];
          if(SA[i][j].Num)
            Sum[i][j+1]++;
        }
      for(int j=0;j<M;j++)
        ST[i][0][j]=Height[i][j];
      for(int j=1;j<17;j++)
        for(int k=0;k<=M-(1<<j);k++)
          ST[i][j][k]=min(ST[i][j-1][k],ST[i][j-1][k+(1<<(j-1))]);
    }
  for(int i=1,j=0;i<=100000;i++)
    {
      if((1<<(j+1))<=i)
        j++;
      Log[i]=j;
    }
  static int L[50000],R[50000];
  int Len0=S[0].size();
  for(int i=0,P=-1,Q=-1;i<Len0;i++)
    {
      while(P<Len0)
        {
          bool Flag=true;
          for(int j=1;j<=N;j++)
            if(Solve(j,i,P)>UB[j])
              Flag=false;
          if(Flag)
            break;
          P++;
        }
      while(Q<Len0)
        {
          bool Flag=true;
          for(int j=1;j<=N;j++)
            if(Solve(j,i,Q+1)<LB[j])
              Flag=false;
          if(!Flag)
            break;
          Q++;
        }
      L[i]=P;
      R[i]=Q;
    }
  static Suffix Data[50000];
  for(int i=0;i<Len0;i++)
    Data[i]=(Suffix){0,i};
  sort(Data,Data+Len0);
  long long Ans=0;
  for(int i=0;i<Len0;i++)
    {
      int Min=L[Data[i].X],Max=R[Data[i].X];
      Min=max(Min,Data[i].X);
      Max=min(Max,Len0-1);
      if(i)
        Min=max(Min,Data[i].X+LCP(Data[i],Data[i-1]));
      Ans+=max(Max-Min+1,0);
    }
  cout<<Ans<<endl;
  return 0;
}


//program 316-G

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

struct Suffix
{
  int Num,X;
};

int Get()
{
  char c;
  while(c=getchar(),(c<'0'||c>'9')&&(c!='-'));
  bool Flag=(c=='-');
  if(Flag)
    c=getchar();
  int X=0;
  while(c>='0'&&c<='9')
    {
      X=X*10+c-48;
      c=getchar();
    }
  return Flag?-X:X;
}

void Output(int X)
{
  if(X<0)
    {
      putchar('-');
      X=-X;
    }
  int Len=0,Data[10];
  while(X)
    {
      Data[Len++]=X%10;
      X/=10;
    }
  if(!Len)
    Data[Len++]=0;
  while(Len--)
    putchar(Data[Len]+48);
  putchar('\n');
}

const int Base=19961021;
const int inf=1000000000;

int N;
string S[11];
long long Hash[11][50001];
long long Pow[50001];
Suffix SA[11][100000];
int Height[11][100000],Pos[11][50000],Sum[11][100001];
int ST[17][11][100000],Log[100001];

long long GetHash(int P,int X,int Len)
{
  return Hash[P][X+Len]-Hash[P][X]*Pow[Len];
}

int LCP(Suffix A,Suffix B)
{
  int Left=0,Right=min(S[A.Num].size()-A.X,S[B.Num].size()-B.X);
  while(Left<Right)
    {
      int Mid=(Left+Right+1)/2;
      if(GetHash(A.Num,A.X,Mid)==GetHash(B.Num,B.X,Mid))
        Left=Mid;
      else
        Right=Mid-1;
    }
  return Left;
}

bool operator <(Suffix A,Suffix B)
{
  int Len=LCP(A,B);
  if(B.X+Len==S[B.Num].size())
    return false;
  if(A.X+Len==S[A.Num].size())
    return true;
  return S[A.Num][A.X+Len]<S[B.Num][B.X+Len];
}

int GetMin(int T,int L,int R)
{
  int P=Log[R-L+1];
  return min(ST[T][P][L],ST[T][P][R-(1<<P)+1]);
}

int Solve(int T,int L,int R)
{
  if(L>R)
    return inf;
  if(R==S[0].size())
    return -inf;
  int Len=R-L+1,X=Pos[T][L];
  int Left=0,Right=X;
  while(Left<Right)
    {
      int Mid=(Left+Right)/2;
      if(GetMin(T,Mid+1,X)>=Len)
        Right=Mid;
      else
        Left=Mid+1;
    }
  int LB=Left;
  Left=X;
  Right=S[0].size()+S[T].size()-1;
  while(Left<Right)
    {
      int Mid=(Left+Right+1)/2;
      if(GetMin(T,X+1,Mid)>=Len)
        Left=Mid;
      else
        Right=Mid-1;
    }
  int UB=Left;
  return Sum[T][UB+1]-Sum[T][LB];
}

int main()
{
  cin>>S[0];
  N=Get();
  static int LB[11],UB[11];
  for(int i=1;i<=N;i++)
    cin>>S[i]>>LB[i]>>UB[i];
  for(int i=0;i<=N;i++)
    {
      Hash[i][0]=0;
      for(int j=0;j<S[i].size();j++)
        Hash[i][j+1]=Hash[i][j]*Base+S[i][j];
    }
  Pow[0]=1;
  for(int i=0;i<50000;i++)
    Pow[i+1]=Pow[i]*Base;
  for(int i=1;i<=N;i++)
    {
      for(int j=0;j<S[0].size();j++)
        SA[i][j]=(Suffix){0,j};
      for(int j=0;j<S[i].size();j++)
        SA[i][S[0].size()+j]=(Suffix){i,j};
      int M=S[0].size()+S[i].size();
      sort(SA[i],SA[i]+M);
      for(int j=0;j<M;j++)
        if(!SA[i][j].Num)
          Pos[i][SA[i][j].X]=j;
      for(int j=1;j<M;j++)
        Height[i][j]=LCP(SA[i][j],SA[i][j-1]);
      Sum[i][0]=0;
      for(int j=0;j<M;j++)
        {
          Sum[i][j+1]=Sum[i][j];
          if(SA[i][j].Num)
            Sum[i][j+1]++;
        }
      for(int j=0;j<M;j++)
        ST[i][0][j]=Height[i][j];
      for(int j=1;j<17;j++)
        for(int k=0;k<=M-(1<<j);k++)
          ST[i][j][k]=min(ST[i][j-1][k],ST[i][j-1][k+(1<<(j-1))]);
    }
  for(int i=1,j=0;i<=100000;i++)
    {
      if((1<<(j+1))<=i)
        j++;
      Log[i]=j;
    }
  static int L[50000],R[50000];
  int Len0=S[0].size();
  for(int i=0,P=-1,Q=-1;i<Len0;i++)
    {
      while(P<Len0)
        {
          bool Flag=true;
          for(int j=1;j<=N;j++)
            if(Solve(j,i,P)>UB[j])
              Flag=false;
          if(Flag)
            break;
          P++;
        }
      while(Q+1<Len0)
        {
          bool Flag=true;
          for(int j=1;j<=N;j++)
            if(Solve(j,i,Q+1)<LB[j])
              Flag=false;
          if(!Flag)
            break;
          Q++;
        }
      L[i]=P;
      R[i]=Q;
    }
  static Suffix Data[50000];
  for(int i=0;i<Len0;i++)
    Data[i]=(Suffix){0,i};
  sort(Data,Data+Len0);
  long long Ans=0;
  for(int i=0;i<Len0;i++)
    {
      int Min=L[Data[i].X],Max=R[Data[i].X];
      Min=max(Min,Data[i].X);
      Max=min(Max,Len0-1);
      if(i)
        Min=max(Min,Data[i].X+LCP(Data[i],Data[i-1]));
      Ans+=max(Max-Min+1,0);
    }
  cout<<Ans<<endl;
  return 0;
}


#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <ctime>
#include <map>
#include <queue>
#include <set>
using namespace std;

const int maxlen=550000*2+15;
const int maxn=50000+15;
struct Node
{
	Node *son[30],*par;
	int len;
}pp[maxlen],*las;
int c[maxlen][14],tot;
char st[14][maxn];
int degre[maxlen];
int n;
int len[14];
int hh,tt,line[maxlen];
int l[14],r[14];
long long ans;
int inser(int x,int y)
{
	Node *now=&pp[++tot];c[tot][y]++;
	now->len=las->len+1;
	Node *u=las;
	for (;u && !u->son[x];u->son[x]=now,u=u->par);
	if (!u) now->par=&pp[0];
	else
	{
		Node *nxt=u->son[x];
		if (nxt->len==u->len+1) now->par=nxt;
		else
		{
			Node *ng=&pp[++tot];
			*ng=*nxt;
			ng->len=u->len+1;
			nxt->par=now->par=ng;
			for (;u && u->son[x]==nxt;u->son[x]=ng,u=u->par);
		}
	}
	las=now;
	return 0;
}
int check(int x)
{
	for (int i=0;i<=n;i++)
	 if (c[x][i]<l[i] || c[x][i]>r[i]) return 0;
	return 1;
}
int main()
{
	scanf("%s%d",st[0],&n);
	len[0]=strlen(st[0]);
	l[0]=1;r[0]=len[0];
	for (int i=1;i<=n;i++) scanf("%s%d%d",st[i],&l[i],&r[i]),len[i]=strlen(st[i]);
	las=&pp[0];
	for (int i=0;i<len[0];i++) inser(st[0][i]-'a'+1,0);
	for (int i=1;i<=n;i++)
	{
		inser(0,n+1);
		for (int j=0;j<len[i];j++) inser(st[i][j]-'a'+1,i);
	}
    for (int i=1;i<=tot;i++) degre[pp[i].par-pp]++;
    for (int i=1;i<=tot;i++)
     if (degre[i]==0) line[++tt]=i;
    for (hh=1;hh<=tot && hh<=tt;hh++)
    {
    	Node *x=&pp[line[hh]],*y=x->par;
    	ans+=(x->len-y->len)*check(x-pp);
    	for (int i=0;i<=n;i++) c[y-pp][i]+=c[x-pp][i];
    	degre[y-pp]--;
    	if (degre[y-pp]==0) line[++tt]=y-pp;
    }
    printf("%I64d\n",ans);
	return 0;
}


#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <ctime>
#include <map>
#include <queue>
#include <set>
using namespace std;

const int maxlen=550000*2+15;
const int maxn=50000+15;
struct Node
{
	Node *son[30],*par;
	int len;
}pp[maxlen],*las;
int c[maxlen][14],tot;
char st[14][maxn];
int degre[maxn];
int n;
int len[14];
int hh,tt,line[maxlen];
int l[14],r[14];
long long ans;
int inser(int x,int y)
{
	Node *now=&pp[++tot];c[tot][y]++;
	now->len=las->len+1;
	Node *u=las;
	for (;u && !u->son[x];u->son[x]=now,u=u->par);
	if (!u) now->par=&pp[0];
	else
	{
		Node *nxt=u->son[x];
		if (nxt->len==u->len+1) now->par=nxt;
		else
		{
			Node *ng=&pp[++tot];
			*ng=*nxt;
			ng->len=u->len+1;
			nxt->par=now->par=ng;
			for (;u && u->son[x]==nxt;u->son[x]=ng,u=u->par);
		}
	}
	las=now;
	return 0;
}
int check(int x)
{
	for (int i=0;i<=n;i++)
	 if (c[x][i]<l[i] || c[x][i]>r[i]) return 0;
	return 1;
}
int main()
{
	scanf("%s%d",st[0],&n);
	len[0]=strlen(st[0]);
	l[0]=1;r[0]=len[0];
	for (int i=1;i<=n;i++) scanf("%s%d%d",st[i],&l[i],&r[i]),len[i]=strlen(st[i]);
	las=&pp[0];
	for (int i=0;i<len[0];i++) inser(st[0][i]-'a'+1,0);
	for (int i=1;i<=n;i++)
	{
		inser(0,n+1);
		for (int j=0;j<len[i];j++) inser(st[i][j]-'a'+1,i);
	}
    for (int i=1;i<=tot;i++) degre[pp[i].par-pp]++;
    for (int i=1;i<=tot;i++)
     if (degre[i]==0) line[++tt]=i;
    for (hh=1;hh<=tot && hh<=tt;hh++)
    {
    	Node *x=&pp[line[hh]],*y=x->par;
    	ans+=(x->len-y->len)*check(x-pp);
    	for (int i=0;i<=n;i++) c[y-pp][i]+=c[x-pp][i];
    	degre[y-pp]--;
    	if (degre[y-pp]==0) line[++tt]=y-pp;
    }
    printf("%I64d\n",ans);
	return 0;
}


#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#include<iostream>
using namespace std;
char str[50005],tt[50005];
int Cnt,cur;
struct node{int ch[26],l,pre;}t[200005];int ndtot=1,last;
int sz[200005][10]={0};
int up[10],lo[10];
int lon[200005]={0},sho[200005]={0};
int cn[200005]={0},seq[200005];
int add(int c,int bo=1){
    int p=last;
    if(!t[p].ch[c]){
        int np=++ndtot;sz[np][cur]+=bo;
        cn[t[np].l=t[p].l+1]++;
        for(;p && !t[p].ch[c];p=t[p].pre)t[p].ch[c]=np;
        if(!p)t[np].pre=1;
        else{
            int q=t[p].ch[c];
            if(t[q].l==t[p].l+1)t[np].pre=q;
            else{
                int nq=++ndtot;t[nq]=t[q];
                cn[t[nq].l=t[p].l+1]++;
                t[np].pre=t[q].pre=nq;
                for(;p && t[p].ch[c]==q;p=t[p].pre)t[p].ch[c]=nq;
            }
        }
        return np;
    }else{
        int q=t[p].ch[c];
        if(t[q].l==t[p].l+1){
            sz[q][cur]+=bo;
            return q;
        }else{
            int nq=++ndtot;sz[nq][cur]+=bo;
            t[nq]=t[q];cn[t[nq].l=t[p].l+1]++;
            t[q].pre=nq;
            for (;p && t[p].ch[c]==q;p=t[p].pre)t[p].ch[c]=nq;
            return nq;
        }
    }
}
int n;int lef[50005],rig[50005];
int s[50005],cnt[50005],tmp1[50005],tmp2[50005],sa[50005],*rk,m=27,h[50005];
void doub(){
    int len,num,i,j,*x=tmp1,*y=tmp2;
    for (i=0;i<m;i++)cnt[i]=0;
    for (i=0;i<n;i++)cnt[s[i]]++;
    for (i=1;i<m;i++)cnt[i]+=cnt[i-1];
    for (i=n-1;i>=0;i--)sa[--cnt[s[i]]]=i;
    for (num=i=1,x[sa[0]]=0;i<n;i++)x[sa[i]]=s[sa[i]]==s[sa[i-1]]?num-1:num++;
    for (len=1;num<n;len<<=1){
        for (j=0;j<len;j++)y[j]=n-j-1;
        for (i=0;i<n;i++)if(sa[i]>=len)y[j++]=sa[i]-len;
        for (i=0;i<num;i++)cnt[i]=0;
        for (i=0;i<n;i++)cnt[x[i]]++;
        for (i=1;i<num;i++)cnt[i]+=cnt[i-1];
        for (i=n-1;i>=0;i--)sa[--cnt[x[y[i]]]]=y[i];
        swap(x,y);
        for (num=i=1,x[sa[0]]=0;i<n;i++)x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+len]==y[sa[i-1]+len]?num-1:num++;
    }
    rk=x;
}
void ch(){
    int i,l=0;
    for (i=0;i<n-1;h[rk[i++]]=l){
        for (l?l--:0;s[i+l]==s[sa[rk[i]-1]+l];l++);
    }
}
int main()
{
    scanf("%s",str);
    last=1;
    for(int i=0;str[i];i++)last=add(str[i]-'a',0);
    scanf("%d",&Cnt);
    for (cur=0;cur<Cnt;cur++){
        scanf("%s",tt);
        last=1;
        for (int i=0;tt[i];i++)last=add(tt[i]-'a');
        scanf("%d%d",&lo[cur],&up[cur]);
    }
    cn[0]=1;for (int i=1;cn[i];i++)cn[i]+=cn[i-1];
    for (int i=1;i<=ndtot;i++)seq[cn[t[i].l]--]=i;
    for (int j=ndtot;j>=2;j--){
        int i=seq[j],pr=t[i].pre;
        for (int m=0;m<Cnt;m++)sz[pr][m]+=sz[i][m];
    }
    for (int i=1;i<=ndtot;i++){
        for(int m=0;m<Cnt;m++){
            if(sz[i][m]<lo[m])lon[i]=1;
            if(sz[i][m]>up[m])sho[i]=1;
        }
    }
    int p=1,le=0;
    for (int i=0;str[i];i++){
        int c=str[i]-'a';
        while(p!=1 && !t[p].ch[c])p=t[p].pre,le=t[p].l;
        if(t[p].ch[c])p=t[p].ch[c],le++;
        while(p!=1 && lon[p])p=t[p].pre,le=t[p].l;
        lef[i]=i-le+1;
    }
    p=1,le=0;
    for (int i=0;str[i];i++){
        int c=str[i]-'a';
        while(p!=1 && !t[p].ch[c])p=t[p].pre,le=t[p].l;
        if(t[p].ch[c])p=t[p].ch[c],le++;
        while(p!=1 && !sho[p])p=t[p].pre,le=t[p].l;
        rig[i]=i-le;
    }
    n=strlen(str);
    for (int i=0;i<n;i++)s[n-i-1]=str[i]-'a'+1;s[n++]=0;
    doub();
    ch();
    for (int i=0;i<n-1;i++)rig[i]=min(rig[i],i-h[rk[n-i-2]]);
    long long ans=0;
    for (int i=0;i<n-1;i++){
        if(lef[i]<=rig[i])ans+=rig[i]-lef[i]+1;
    }
    cout<<ans<<endl;
    return 0;
}


#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#define N 1200000
#define int64 long long
#define For(i,x,y) for (i=x;i<=y;i++)
using namespace std;
struct sam {
	int l,fa,a[27],ty;
} a[N];
int i,j,k,n,m,t,last,s,len;
int L[20],R[20],b[N][11];
int64 an;
char p[N];
vector<int> c[N];
inline void into(int x,int l,int _) {
	int np,p,q,r,i;
	np=++t;
	a[np].ty=_,a[np].l=l;
	for (p=last;p&&!a[p].a[x];p=a[p].fa) a[p].a[x]=np;
	last=np;
	if (!p) a[np].fa=1;
	else {
		q=a[p].a[x];
		if (a[p].l+1==a[q].l) a[np].fa=q;
		else {
			r=++t;
			a[r]=a[q];
			a[r].l=a[p].l+1;
			a[q].fa=a[np].fa=r;
			for (;p&&a[p].a[x]==q;p=a[p].fa) a[p].a[x]=r;
		}
	}
}
void dfs(int x) {
	int i,j;
	for (i=0;i<c[x].size();i++) {
		int A=c[x][i];
		dfs(A);
		For(j,1,m) b[x][j]+=b[A][j];
	}
	b[x][a[x].ty]++;
	int s=0;
	For(i,1,m) s+=L[i]<=b[x][i]&&b[x][i]<=R[i];
	if (s==m&&a[x].l<=len) an+=a[x].l-a[a[x].fa].l;
}
int main() {
	t=last=1;
	scanf("%s",p+1);
	n=len=strlen(p+1);
	For(i,1,n) into(p[i]-'a',++s,0);
	scanf("%d",&m);
	For(i,1,m) {
		into(26,++s,0);
		scanf("%s%d%d",p+1,&L[i],&R[i]);
		n=strlen(p+1);
		For(j,1,n) into(p[j]-'a',++s,i);
	}
	For(i,2,t) c[a[i].fa].push_back(i);
	dfs(1);
	printf("%I64d\n",an);
	return 0;
}


#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#define N 1200000
#define int64 long long
#define For(i,x,y) for (i=x;i<=y;i++)
using namespace std;
struct sam {
	int l,fa,a[27],ty;
} a[N];
int i,j,k,n,m,t,last,s,len;
int L[20],R[20],b[N][11];
int64 an;
char p[N];
vector<int> c[N];
inline void into(int x,int l,int _) {
	int np,p,q,r;
	np=++t;
	a[np].ty=_,a[np].l=l;
	for (p=last;p&&!a[p].a[x];p=a[p].fa) a[p].a[x]=np;
	last=np;
	if (!p) a[np].fa=1;
	else {
		q=a[p].a[x];
		if (a[p].l+1==a[q].l) a[np].fa=q;
		else {
			r=++t;
			a[r]=a[q];
			a[r].ty=-1;
			a[r].l=a[p].l+1;
			a[q].fa=a[np].fa=r;
			for (;p&&a[p].a[x]==q;p=a[p].fa) a[p].a[x]=r;
		}
	}
}
void dfs(int x) {
	int i,j;
	for (i=0;i<c[x].size();i++) {
		int A=c[x][i];
		dfs(A);
		For(j,0,m) b[x][j]+=b[A][j];
	}
	if (a[x].ty>=0) b[x][a[x].ty]++;
	int s=0;
	For(i,0,m) s+=L[i]<=b[x][i]&&b[x][i]<=R[i];
	if (s==m&&b[x][0]) an+=a[x].l-a[a[x].fa].l;
}
int main() {
	//freopen("316G.in","r",stdin);
	t=last=1; a[t].ty=-1;
	scanf("%s",p+1);
	n=len=strlen(p+1);
	For(i,1,n) into(p[i]-'a',++s,0);
	scanf("%d",&m);
	For(i,1,m) {
		into(26,++s,-1);
		scanf("%s%d%d",p+1,&L[i],&R[i]);
		n=strlen(p+1);
		For(j,1,n) into(p[j]-'a',++s,i);
	}
	For(i,2,t) c[a[i].fa].push_back(i);
	dfs(1);
	printf("%I64d\n",an);
	return 0;
}