#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& x) {
  bool fu = 0;
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-') fu = 1, c = getchar();
  for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';
  if (fu) x = -x;
};
inline char getc() {
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  return c;
}
const int N = 1000010;
int n, i, j, k, l, g, p, now;
long long ans;
int L[12], R[12];
struct SAM {
  int tot, p, np, q, r, lp;
  int fa[N], l[N];
  int sz[N][12];
  int s[N], Q[N];
  map<int, int> ch[N];
  map<int, int>::iterator it;
  void addc(char c) {
    np = ++tot;
    p = lp;
    l[np] = l[p] + 1;
    lp = np;
    for (; p > 0 && ch[p][c] == 0; p = fa[p]) ch[p][c] = np;
    q = ch[p][c];
    if (p == 0)
      fa[np] = 1;
    else if (l[p] + 1 == l[q])
      fa[np] = q;
    else {
      r = ++tot;
      ch[r] = ch[q];
      l[r] = l[p] + 1;
      fa[r] = fa[q];
      fa[q] = fa[np] = r;
      for (; p > 0 && ch[p][c] == q; p = fa[p]) ch[p][c] = r;
    }
  }
  void work() {
    int i, j, p, q, t;
    for (i = 1; i <= tot; i++) s[l[i]]++;
    for (i = 1; i <= tot; i++) s[i] += s[i - 1];
    for (i = 1; i <= tot; i++) Q[s[l[i]]--] = i;
    for (i = tot; i >= 1; i--) {
      p = Q[i];
      for (it = ch[p].begin(); it != ch[p].end(); it++)
        if (it->first < 26) {
          for (t = 0; t <= now - 1; t++) sz[p][t] += sz[it->second][t];
        } else {
          sz[p][(it->first) - 26]++;
        }
      bool pd = 1;
      for (j = 0; j <= now - 1; j++)
        if (!(sz[p][j] >= L[j] && sz[p][j] <= R[j])) {
          pd = 0;
          break;
        }
      if (pd) ans += l[p] - l[fa[p]];
    }
    cout << ans << endl;
  }
} S;
char st[50010];
void addstr() {
  int i;
  scanf("%s", st + 1);
  l = strlen(st + 1);
  for (i = 1; i <= l; i++) S.addc(st[i] - 'a');
  S.addc(now + 26);
  if (now > 0) read(L[now]), read(R[now]);
  now++;
}
int main() {
  S.tot = S.lp = 1;
  addstr();
  L[0] = 1;
  R[0] = int(1e9);
  read(g);
  for (i = 1; i <= g; i++) addstr();
  S.work();
  scanf("\n");
  return 0;
}

### Prompt

Construct a Cpp code solution to the problem outlined:

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;
template <class T>
inline void read(T& x) {
  bool fu = 0;
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-') fu = 1, c = getchar();
  for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';
  if (fu) x = -x;
};
inline char getc() {
  char c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  return c;
}
const int N = 1000010;
int n, i, j, k, l, g, p, now;
long long ans;
int L[12], R[12];
struct SAM {
  int tot, p, np, q, r, lp;
  int fa[N], l[N];
  int sz[N][12];
  int s[N], Q[N];
  map<int, int> ch[N];
  map<int, int>::iterator it;
  void addc(char c) {
    np = ++tot;
    p = lp;
    l[np] = l[p] + 1;
    lp = np;
    for (; p > 0 && ch[p][c] == 0; p = fa[p]) ch[p][c] = np;
    q = ch[p][c];
    if (p == 0)
      fa[np] = 1;
    else if (l[p] + 1 == l[q])
      fa[np] = q;
    else {
      r = ++tot;
      ch[r] = ch[q];
      l[r] = l[p] + 1;
      fa[r] = fa[q];
      fa[q] = fa[np] = r;
      for (; p > 0 && ch[p][c] == q; p = fa[p]) ch[p][c] = r;
    }
  }
  void work() {
    int i, j, p, q, t;
    for (i = 1; i <= tot; i++) s[l[i]]++;
    for (i = 1; i <= tot; i++) s[i] += s[i - 1];
    for (i = 1; i <= tot; i++) Q[s[l[i]]--] = i;
    for (i = tot; i >= 1; i--) {
      p = Q[i];
      for (it = ch[p].begin(); it != ch[p].end(); it++)
        if (it->first < 26) {
          for (t = 0; t <= now - 1; t++) sz[p][t] += sz[it->second][t];
        } else {
          sz[p][(it->first) - 26]++;
        }
      bool pd = 1;
      for (j = 0; j <= now - 1; j++)
        if (!(sz[p][j] >= L[j] && sz[p][j] <= R[j])) {
          pd = 0;
          break;
        }
      if (pd) ans += l[p] - l[fa[p]];
    }
    cout << ans << endl;
  }
} S;
char st[50010];
void addstr() {
  int i;
  scanf("%s", st + 1);
  l = strlen(st + 1);
  for (i = 1; i <= l; i++) S.addc(st[i] - 'a');
  S.addc(now + 26);
  if (now > 0) read(L[now]), read(R[now]);
  now++;
}
int main() {
  S.tot = S.lp = 1;
  addstr();
  L[0] = 1;
  R[0] = int(1e9);
  read(g);
  for (i = 1; i <= g; i++) addstr();
  S.work();
  scanf("\n");
  return 0;
}

```