#include <bits/stdc++.h>
using namespace std;
int const MAXN = 60100;
int const MAX_RULE = 15;
unsigned int const MAX_HASH = 500003;
unsigned long long const HASH_CONST = 1000000000000000009ULL;
char st[MAXN], rule_str[MAX_RULE][MAXN], buf[MAXN];
int rules, rule_a[MAX_RULE], rule_b[MAX_RULE], rule_len[MAX_RULE];
struct my_list {
  unsigned long long hash_value;
  int count;
  my_list *next;
};
struct my_ht {
  my_list *s[MAX_HASH];
  void clear() {
    for (int i = 0; i < MAX_HASH; i++)
      if (s[i]) {
        my_list *t = s[i], *tt = 0;
        while (t) {
          tt = t;
          t = t->next;
          delete tt;
        }
        s[i] = 0;
      }
  }
  void add(unsigned long long hash_value) {
    unsigned int d = hash_value % MAX_HASH;
    my_list *t = s[d];
    while (t) {
      if (t->hash_value == hash_value) {
        t->count++;
        return;
      }
      t = t->next;
    }
    t = new my_list;
    t->hash_value = hash_value;
    t->count = 1;
    t->next = s[d];
    s[d] = t;
  }
  my_list *find(unsigned long long hash_value) {
    unsigned int d = hash_value % MAX_HASH;
    my_list *t = s[d];
    while (t) {
      if (t->hash_value == hash_value) return t;
      t = t->next;
    }
    return 0;
  }
} cur_ht[MAX_RULE], st_ht;
int main() {
  gets(st);
  int n = (int)strlen(st);
  gets(buf);
  sscanf(buf, "%d", &rules);
  for (int i = 0; i < rules; i++) {
    gets(buf);
    sscanf(buf, "%s%d%d", rule_str[i], &rule_a[i], &rule_b[i]);
    rule_len[i] = (int)strlen(rule_str[i]);
  }
  for (int i = 0; i < rules; i++) {
    cur_ht[i].clear();
    for (int j = 0; j < rule_len[i]; j++) {
      unsigned long long cur_hash = 0;
      for (int k = j; k < rule_len[i]; k++) {
        cur_hash = (cur_hash * HASH_CONST) +
                   ((unsigned long long)(rule_str[i][k] - 'a' + 1));
        cur_ht[i].add(cur_hash);
      }
    }
  }
  int ans = 0;
  for (int j = 0; j < n; j++) {
    unsigned long long cur_hash = 0;
    for (int k = j; k < n; k++) {
      cur_hash =
          (cur_hash * HASH_CONST) + ((unsigned long long)(st[k] - 'a' + 1));
      if (st_ht.find(cur_hash) == 0) {
        st_ht.add(cur_hash);
        int Ok = 1;
        for (int q = 0; q < rules; q++) {
          int cur_count_value = 0;
          my_list *t = cur_ht[q].find(cur_hash);
          if (t != 0) cur_count_value = t->count;
          if (cur_count_value >= rule_a[q] && cur_count_value <= rule_b[q])
            continue;
          else {
            Ok = 0;
            break;
          }
        }
        ans += Ok;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Please create a solution in CPP to 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;
int const MAXN = 60100;
int const MAX_RULE = 15;
unsigned int const MAX_HASH = 500003;
unsigned long long const HASH_CONST = 1000000000000000009ULL;
char st[MAXN], rule_str[MAX_RULE][MAXN], buf[MAXN];
int rules, rule_a[MAX_RULE], rule_b[MAX_RULE], rule_len[MAX_RULE];
struct my_list {
  unsigned long long hash_value;
  int count;
  my_list *next;
};
struct my_ht {
  my_list *s[MAX_HASH];
  void clear() {
    for (int i = 0; i < MAX_HASH; i++)
      if (s[i]) {
        my_list *t = s[i], *tt = 0;
        while (t) {
          tt = t;
          t = t->next;
          delete tt;
        }
        s[i] = 0;
      }
  }
  void add(unsigned long long hash_value) {
    unsigned int d = hash_value % MAX_HASH;
    my_list *t = s[d];
    while (t) {
      if (t->hash_value == hash_value) {
        t->count++;
        return;
      }
      t = t->next;
    }
    t = new my_list;
    t->hash_value = hash_value;
    t->count = 1;
    t->next = s[d];
    s[d] = t;
  }
  my_list *find(unsigned long long hash_value) {
    unsigned int d = hash_value % MAX_HASH;
    my_list *t = s[d];
    while (t) {
      if (t->hash_value == hash_value) return t;
      t = t->next;
    }
    return 0;
  }
} cur_ht[MAX_RULE], st_ht;
int main() {
  gets(st);
  int n = (int)strlen(st);
  gets(buf);
  sscanf(buf, "%d", &rules);
  for (int i = 0; i < rules; i++) {
    gets(buf);
    sscanf(buf, "%s%d%d", rule_str[i], &rule_a[i], &rule_b[i]);
    rule_len[i] = (int)strlen(rule_str[i]);
  }
  for (int i = 0; i < rules; i++) {
    cur_ht[i].clear();
    for (int j = 0; j < rule_len[i]; j++) {
      unsigned long long cur_hash = 0;
      for (int k = j; k < rule_len[i]; k++) {
        cur_hash = (cur_hash * HASH_CONST) +
                   ((unsigned long long)(rule_str[i][k] - 'a' + 1));
        cur_ht[i].add(cur_hash);
      }
    }
  }
  int ans = 0;
  for (int j = 0; j < n; j++) {
    unsigned long long cur_hash = 0;
    for (int k = j; k < n; k++) {
      cur_hash =
          (cur_hash * HASH_CONST) + ((unsigned long long)(st[k] - 'a' + 1));
      if (st_ht.find(cur_hash) == 0) {
        st_ht.add(cur_hash);
        int Ok = 1;
        for (int q = 0; q < rules; q++) {
          int cur_count_value = 0;
          my_list *t = cur_ht[q].find(cur_hash);
          if (t != 0) cur_count_value = t->count;
          if (cur_count_value >= rule_a[q] && cur_count_value <= rule_b[q])
            continue;
          else {
            Ok = 0;
            break;
          }
        }
        ans += Ok;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

```