Smart Beaver recently got interested in a new word game. The point is as follows: count the number of distinct good substrings of some string s. To determine if a string is good or not the game uses rules. Overall there are n rules. Each rule is described by a group of three (p, l, r), where p is a string and l and r (l ≤ r) are integers. We’ll say that string t complies with rule (p, l, r), if the number of occurrences of string t in string p lies between l and r, inclusive. For example, string "ab", complies with rules ("ab", 1, 2) and ("aab", 0, 1), but does not comply with rules ("cd", 1, 2) and ("abab", 0, 1).

A substring s[l... r] (1 ≤ l ≤ r ≤ |s|) of string s = s1s2... s|s| (|s| is a length of s) is string slsl + 1... sr.

Consider a number of occurrences  of string t in string p as a number of pairs of integers l, r (1 ≤ l ≤ r ≤ |p|) such that p[l... r] = t.

We’ll say that string t is good if it complies with all n rules. Smart Beaver asks you to help him to write a program that can calculate the number of distinct good substrings of string s. Two substrings s[x... y] and s[z... w] are cosidered to be distinct iff s[x... y] ≠ s[z... w].

Input

The first line contains string s. The second line contains integer n. Next n lines contain the rules, one per line. Each of these lines contains a string and two integers pi, li, ri, separated by single spaces (0 ≤ li ≤ ri ≤ |pi|). It is guaranteed that all the given strings are non-empty and only contain lowercase English letters.

The input limits for scoring 30 points are (subproblem G1): 

  * 0 ≤ n ≤ 10. 
  * The length of string s and the maximum length of string p is  ≤ 200. 



The input limits for scoring 70 points are (subproblems G1+G2): 

  * 0 ≤ n ≤ 10. 
  * The length of string s and the maximum length of string p is  ≤ 2000. 



The input limits for scoring 100 points are (subproblems G1+G2+G3): 

  * 0 ≤ n ≤ 10. 
  * The length of string s and the maximum length of string p is  ≤ 50000. 

Output

Print a single integer — the number of good substrings of string s.

Examples

Input

aaab
2
aa 0 0
aab 1 1


Output

3


Input

ltntlnen
3
n 0 0
ttlneenl 1 4
lelllt 1 1


Output

2


Input

a
0


Output

1

Note

There are three good substrings in the first sample test: «aab», «ab» and «b».

In the second test only substrings «e» and «t» are good.
#include <bits/stdc++.h>
using namespace std;
int lg[1300005];
void get_lg(int n) {
  int i;
  for (i = 1; i <= n; ++i) lg[i] = lg[i - 1] + (1 << lg[i - 1] + 1 == i);
}
struct suffix_array {
  char str[1300005];
  int sa[1300005], rank[1300005], trank[1300005], tsa[1300005], sum[1300005],
      height[1300005], LCP[22][600005];
  int l;
  void build() {
    int i, j, k, p = 10000;
    for (i = 1; i <= p; ++i) sum[i] = 0;
    for (i = 1; i <= l; ++i) trank[i] = str[i];
    for (i = 1; i <= l; ++i) ++sum[trank[i]];
    for (i = 1; i <= p; ++i) sum[i] += sum[i - 1];
    for (i = l; i >= 1; --i) sa[sum[trank[i]]--] = i;
    rank[sa[1]] = 1;
    p = 1;
    for (i = 2; i <= l; ++i) {
      if (trank[sa[i]] != trank[sa[i - 1]]) ++p;
      rank[sa[i]] = p;
    }
    for (k = 1; k <= l; k *= 2) {
      for (i = 1; i <= p; ++i) sum[i] = 0;
      for (i = 1; i <= l; ++i) ++sum[rank[i + k]];
      for (i = 1; i <= p; ++i) sum[i] += sum[i - 1];
      for (i = l; i >= 1; --i) tsa[sum[rank[i + k]]--] = i;
      for (i = 1; i <= p; ++i) sum[i] = 0;
      for (i = 1; i <= l; ++i) ++sum[rank[i]];
      for (i = 1; i <= p; ++i) sum[i] += sum[i - 1];
      for (i = l; i >= 1; --i) sa[sum[rank[tsa[i]]]--] = tsa[i];
      trank[sa[1]] = 1;
      p = 1;
      for (i = 2; i <= l; ++i) {
        if (rank[sa[i]] != rank[sa[i - 1]] ||
            rank[sa[i] + k] != rank[sa[i - 1] + k])
          ++p;
        trank[sa[i]] = p;
      }
      for (i = 1; i <= l; ++i) rank[i] = trank[i];
    }
    for (i = 1, k = 0; i <= l; ++i) {
      if (rank[i] == 1) continue;
      for (; str[i + k] == str[sa[rank[i] - 1] + k];) ++k;
      height[rank[i]] = k;
      if (k > 0) --k;
    }
    for (i = 1; i <= l; ++i) LCP[0][i] = height[i];
    for (k = 1; k <= lg[l]; ++k)
      for (i = 1; i + (1 << k) <= l + 1; ++i)
        LCP[k][i] = min(LCP[k - 1][i], LCP[k - 1][i + (1 << k - 1)]);
  }
  inline int lcp(int x, int y) {
    if (x == y) return l - x;
    if (x > y) swap(x, y);
    int k = lg[y - x];
    return min(LCP[k][x + 1], LCP[k][y - (1 << k) + 1]);
  }
} a;
int L, n, lim, last, i, j, k, ans, len[15], l[15], r[15], liml[15], limr[15];
int sum[600005][11], tmp[11];
bool check1(int x, int y) {
  int l, r, mid, aim;
  for (l = 1; l <= n; ++l) tmp[l] = 0;
  l = 1;
  r = x;
  aim = x;
  while (l <= r) {
    mid = l + r >> 1;
    if (a.lcp(mid, x) >= y)
      aim = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  for (l = 1; l <= n; ++l) tmp[l] += sum[x][l] - sum[aim - 1][l];
  l = x + 1;
  r = L;
  aim = x;
  while (l <= r) {
    mid = l + r >> 1;
    if (a.lcp(mid, x) >= y)
      aim = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  for (l = 1; l <= n; ++l) tmp[l] += sum[aim][l] - sum[x][l];
  for (l = 1; l <= n; ++l)
    if (tmp[l] > limr[l]) return false;
  return true;
}
bool check2(int x, int y) {
  int l, r, mid, aim;
  for (l = 1; l <= n; ++l) tmp[l] = 0;
  l = 1;
  r = x;
  aim = x;
  while (l <= r) {
    mid = l + r >> 1;
    if (a.lcp(mid, x) >= y)
      aim = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  for (l = 1; l <= n; ++l) tmp[l] += sum[x][l] - sum[aim - 1][l];
  l = x + 1;
  r = L;
  aim = x;
  while (l <= r) {
    mid = l + r >> 1;
    if (a.lcp(mid, x) >= y)
      aim = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  for (l = 1; l <= n; ++l) tmp[l] += sum[aim][l] - sum[x][l];
  for (l = 1; l <= n; ++l)
    if (tmp[l] < liml[l]) return false;
  return true;
}
void work(int x) {
  int l = 1, r = len[0] - a.sa[x] + 1, mid, aim1 = r + 1, aim2;
  while (l <= r) {
    mid = l + r >> 1;
    if (check1(x, mid))
      aim1 = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  l = aim1;
  r = len[0] - a.sa[x] + 1;
  aim2 = aim1 - 1;
  while (l <= r) {
    mid = l + r >> 1;
    if (check2(x, mid))
      aim2 = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  if (lim + 1 > aim1) aim1 = lim + 1;
  if (aim1 > aim2) return;
  ans += aim2 - aim1 + 1;
}
int main() {
  scanf("%s", a.str + 1);
  len[0] = strlen(a.str + 1);
  L = len[0] + 1;
  a.str[L++] = 1;
  l[0] = 1;
  r[0] = len[0];
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) {
    scanf("%s", a.str + L);
    len[i] = strlen(a.str + L);
    l[i] = L;
    r[i] = L + len[i] - 1;
    L += len[i];
    a.str[L++] = i + 1;
    scanf("%d%d", &liml[i], &limr[i]);
  }
  a.l = L;
  get_lg(L);
  a.build();
  for (i = 1; i <= L; ++i)
    for (j = 0; j <= n; ++j)
      sum[i][j] = sum[i - 1][j] + (a.sa[i] >= l[j] && a.sa[i] <= r[j]);
  last = 0;
  for (i = 1; i <= L; ++i)
    if (a.sa[i] <= len[0]) {
      if (last) lim = a.lcp(last, i);
      work(i);
      last = i;
    }
  printf("%d\n", ans);
}
