#include <bits/stdc++.h>
using namespace std;
const int N = 111111;
const int C = 11;
const int K = 20;
int lg[N];
void PRE() {
  for (int i = 0, p = 0; i < N; i++) {
    if ((1 << p + 1) <= i) p++;
    lg[i] = p;
  }
}
struct RMQ {
  int v[K][N];
  void build(int *a, int n) {
    for (int i = 0; i < n; i++) v[0][i] = a[i];
    for (int k = 1; (1 << k) <= n; k++) {
      for (int i = 0; i + (1 << k - 1) <= n; i++) {
        v[k][i] = min(v[k - 1][i], v[k - 1][i + (1 << k - 1)]);
      }
    }
  }
  int query(int l, int r) {
    int k = lg[r - l + 1];
    return min(v[k][l], v[k][r - (1 << k) + 1]);
  }
};
int t1[N], t2[N], c[N];
struct SA {
  char s[N];
  int sa[N], n, ht[N], rk[N];
  void get_n() { n = strlen(s); }
  void cal(int m = 256) {
    int *x = t1, *y = t2;
    for (int i = 0; i < m; i++) c[i] = 0;
    for (int i = 0; i < n; i++) c[x[i] = s[i]]++;
    for (int i = 1; i < m; i++) c[i] += c[i - 1];
    for (int i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
    for (int k = 1; k <= n; k <<= 1) {
      int p = 0;
      for (int i = n - k; i < n; i++) y[p++] = i;
      for (int i = 0; i < n; i++)
        if (sa[i] >= k) y[p++] = sa[i] - k;
      for (int i = 0; i < m; i++) c[i] = 0;
      for (int i = 0; i < n; i++) c[x[y[i]]]++;
      for (int i = 1; i < m; i++) c[i] += c[i - 1];
      for (int i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
      swap(x, y);
      p = 1;
      x[sa[0]] = 0;
      for (int i = 1; i < n; i++) {
        x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]
                       ? p - 1
                       : p++;
      }
      if (p >= n) break;
      m = p;
    }
    for (int i = 0; i < n; i++) rk[sa[i]] = i;
    int k = 0;
    for (int i = 0; i < n; i++) {
      if (k) k--;
      int j = sa[rk[i] - 1];
      while (s[i + k] == s[j + k]) k++;
      ht[rk[i]] = k;
    }
    rmq.build(ht, n);
  }
  RMQ rmq;
  int lcp(int l, int r) {
    l = rk[l];
    r = rk[r];
    return lcp_sa(l, r);
  }
  int lcp_sa(int l, int r) {
    if (l == r) return n - sa[l] - 1;
    if (l > r) swap(l, r);
    return rmq.query(l + 1, r);
  }
} sa[C];
char str[N >> 1], buf[N >> 1];
int n, L[C], R[C], len, sum[C][N];
int get_sum(int id, int l, int r) {
  return sum[id][r] - (l ? sum[id][l - 1] : 0);
}
int find_l(int id, int p, int d) {
  int l = 0, r = p, mk = r, m;
  while (l <= r) {
    m = l + r >> 1;
    if (sa[id].lcp_sa(p, m) >= d)
      r = m - 1, mk = m;
    else
      l = m + 1;
  }
  return mk;
}
int find_r(int id, int p, int d) {
  int l = p, r = sa[id].n - 1, mk = l, m;
  while (l <= r) {
    m = l + r >> 1;
    if (sa[id].lcp_sa(p, m) >= d)
      l = m + 1, mk = m;
    else
      r = m - 1;
  }
  return mk;
}
int get_range(int id, int s, int t) {
  int d = t - s + 1, l, r, p = sa[id].rk[s];
  l = find_l(id, p, d);
  r = find_r(id, p, d);
  return get_sum(id, l, r);
}
bool check_l(int s, int t) {
  for (int i = 0; i < n; i++)
    if (get_range(i, s, t) > R[i]) return 0;
  return 1;
}
bool check_r(int s, int t) {
  for (int i = 0; i < n; i++)
    if (get_range(i, s, t) < L[i]) return 0;
  return 1;
}
int find_l(int p) {
  int l = p, r = len, mk = len, m;
  while (l <= r) {
    m = l + r >> 1;
    if (check_l(p, m))
      r = m - 1, mk = m;
    else
      l = m + 1;
  }
  return mk;
}
int find_r(int p) {
  int l = p, r = len - 1, mk = -1, m;
  while (l <= r) {
    m = l + r >> 1;
    if (check_r(p, m))
      l = m + 1, mk = m;
    else
      r = m - 1;
  }
  return mk + 1;
}
int lb[N >> 1], ub[N >> 1];
long long work() {
  long long ans = 0;
  if (n == 0) {
    ans = (long long)len * (len + 1) >> 1;
    strcpy(sa[n].s, str);
    sa[n].get_n();
    sa[n].cal();
    for (int i = 1; i < sa[n].n; i++) ans -= sa[n].ht[i];
    return ans;
  }
  for (int i = 0; i < n; i++) {
    sa[i].get_n();
    sa[i].cal();
  }
  for (int i = 0; i < n; i++) memset(sum[i], 0, sizeof(sum[i]));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < sa[i].n; j++)
      if (sa[i].sa[j] > len) sum[i][j]++;
    for (int j = 0; j < sa[i].n; j++) sum[i][j] += sum[i][j - 1];
  }
  for (int i = 0; i < len; i++) {
    lb[i] = find_l(i);
    ub[i] = find_r(i);
    ans += max(ub[i] - lb[i], 0);
  }
  strcpy(sa[n].s, str);
  sa[n].get_n();
  sa[n].cal();
  for (int i = 1, h, d; i < sa[n].n; i++) {
    int i1 = sa[n].sa[i - 1], i2 = sa[n].sa[i];
    h = sa[n].ht[i];
    d = lb[i2] - i2;
    ans -= max(min(max(h - d, 0), min(ub[i1] - lb[i1], ub[i2] - lb[i2])), 0);
  }
  return ans;
}
int main() {
  PRE();
  while (cin >> str >> n) {
    len = strlen(str);
    str[len] = '$';
    str[len + 1] = 0;
    for (int i = 0; i < n; i++) {
      cin >> buf >> L[i] >> R[i];
      strcpy(sa[i].s, str);
      strcat(sa[i].s, buf);
    }
    cout << work() << endl;
  }
  return 0;
}

### Prompt

In Cpp, your task is to solve the following problem:

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.

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 111111;
const int C = 11;
const int K = 20;
int lg[N];
void PRE() {
  for (int i = 0, p = 0; i < N; i++) {
    if ((1 << p + 1) <= i) p++;
    lg[i] = p;
  }
}
struct RMQ {
  int v[K][N];
  void build(int *a, int n) {
    for (int i = 0; i < n; i++) v[0][i] = a[i];
    for (int k = 1; (1 << k) <= n; k++) {
      for (int i = 0; i + (1 << k - 1) <= n; i++) {
        v[k][i] = min(v[k - 1][i], v[k - 1][i + (1 << k - 1)]);
      }
    }
  }
  int query(int l, int r) {
    int k = lg[r - l + 1];
    return min(v[k][l], v[k][r - (1 << k) + 1]);
  }
};
int t1[N], t2[N], c[N];
struct SA {
  char s[N];
  int sa[N], n, ht[N], rk[N];
  void get_n() { n = strlen(s); }
  void cal(int m = 256) {
    int *x = t1, *y = t2;
    for (int i = 0; i < m; i++) c[i] = 0;
    for (int i = 0; i < n; i++) c[x[i] = s[i]]++;
    for (int i = 1; i < m; i++) c[i] += c[i - 1];
    for (int i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
    for (int k = 1; k <= n; k <<= 1) {
      int p = 0;
      for (int i = n - k; i < n; i++) y[p++] = i;
      for (int i = 0; i < n; i++)
        if (sa[i] >= k) y[p++] = sa[i] - k;
      for (int i = 0; i < m; i++) c[i] = 0;
      for (int i = 0; i < n; i++) c[x[y[i]]]++;
      for (int i = 1; i < m; i++) c[i] += c[i - 1];
      for (int i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
      swap(x, y);
      p = 1;
      x[sa[0]] = 0;
      for (int i = 1; i < n; i++) {
        x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]
                       ? p - 1
                       : p++;
      }
      if (p >= n) break;
      m = p;
    }
    for (int i = 0; i < n; i++) rk[sa[i]] = i;
    int k = 0;
    for (int i = 0; i < n; i++) {
      if (k) k--;
      int j = sa[rk[i] - 1];
      while (s[i + k] == s[j + k]) k++;
      ht[rk[i]] = k;
    }
    rmq.build(ht, n);
  }
  RMQ rmq;
  int lcp(int l, int r) {
    l = rk[l];
    r = rk[r];
    return lcp_sa(l, r);
  }
  int lcp_sa(int l, int r) {
    if (l == r) return n - sa[l] - 1;
    if (l > r) swap(l, r);
    return rmq.query(l + 1, r);
  }
} sa[C];
char str[N >> 1], buf[N >> 1];
int n, L[C], R[C], len, sum[C][N];
int get_sum(int id, int l, int r) {
  return sum[id][r] - (l ? sum[id][l - 1] : 0);
}
int find_l(int id, int p, int d) {
  int l = 0, r = p, mk = r, m;
  while (l <= r) {
    m = l + r >> 1;
    if (sa[id].lcp_sa(p, m) >= d)
      r = m - 1, mk = m;
    else
      l = m + 1;
  }
  return mk;
}
int find_r(int id, int p, int d) {
  int l = p, r = sa[id].n - 1, mk = l, m;
  while (l <= r) {
    m = l + r >> 1;
    if (sa[id].lcp_sa(p, m) >= d)
      l = m + 1, mk = m;
    else
      r = m - 1;
  }
  return mk;
}
int get_range(int id, int s, int t) {
  int d = t - s + 1, l, r, p = sa[id].rk[s];
  l = find_l(id, p, d);
  r = find_r(id, p, d);
  return get_sum(id, l, r);
}
bool check_l(int s, int t) {
  for (int i = 0; i < n; i++)
    if (get_range(i, s, t) > R[i]) return 0;
  return 1;
}
bool check_r(int s, int t) {
  for (int i = 0; i < n; i++)
    if (get_range(i, s, t) < L[i]) return 0;
  return 1;
}
int find_l(int p) {
  int l = p, r = len, mk = len, m;
  while (l <= r) {
    m = l + r >> 1;
    if (check_l(p, m))
      r = m - 1, mk = m;
    else
      l = m + 1;
  }
  return mk;
}
int find_r(int p) {
  int l = p, r = len - 1, mk = -1, m;
  while (l <= r) {
    m = l + r >> 1;
    if (check_r(p, m))
      l = m + 1, mk = m;
    else
      r = m - 1;
  }
  return mk + 1;
}
int lb[N >> 1], ub[N >> 1];
long long work() {
  long long ans = 0;
  if (n == 0) {
    ans = (long long)len * (len + 1) >> 1;
    strcpy(sa[n].s, str);
    sa[n].get_n();
    sa[n].cal();
    for (int i = 1; i < sa[n].n; i++) ans -= sa[n].ht[i];
    return ans;
  }
  for (int i = 0; i < n; i++) {
    sa[i].get_n();
    sa[i].cal();
  }
  for (int i = 0; i < n; i++) memset(sum[i], 0, sizeof(sum[i]));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < sa[i].n; j++)
      if (sa[i].sa[j] > len) sum[i][j]++;
    for (int j = 0; j < sa[i].n; j++) sum[i][j] += sum[i][j - 1];
  }
  for (int i = 0; i < len; i++) {
    lb[i] = find_l(i);
    ub[i] = find_r(i);
    ans += max(ub[i] - lb[i], 0);
  }
  strcpy(sa[n].s, str);
  sa[n].get_n();
  sa[n].cal();
  for (int i = 1, h, d; i < sa[n].n; i++) {
    int i1 = sa[n].sa[i - 1], i2 = sa[n].sa[i];
    h = sa[n].ht[i];
    d = lb[i2] - i2;
    ans -= max(min(max(h - d, 0), min(ub[i1] - lb[i1], ub[i2] - lb[i2])), 0);
  }
  return ans;
}
int main() {
  PRE();
  while (cin >> str >> n) {
    len = strlen(str);
    str[len] = '$';
    str[len + 1] = 0;
    for (int i = 0; i < n; i++) {
      cin >> buf >> L[i] >> R[i];
      strcpy(sa[i].s, str);
      strcat(sa[i].s, buf);
    }
    cout << work() << endl;
  }
  return 0;
}

```