#include <bits/stdc++.h>
using namespace std;
namespace suffix_automaton_array {
std::vector<int> len, link, cnt[11];
std::vector<std::vector<int> > child;
const int alpha = 27, start_char = 'a';
int init() {
  len.clear();
  link.clear();
  child.clear();
  len.push_back(0);
  link.push_back(-1);
  for (int i = 0; i < int(11); i++) {
    cnt[i].push_back(0);
  }
  child.push_back(std::vector<int>(alpha, -1));
  return 0;
}
void add(int c, int &last, int f) {
  int cur = len.size();
  len.push_back(len[last] + 1);
  for (int i = 0; i < int(11); i++) {
    cnt[i].push_back(f == i);
  }
  link.push_back(-1);
  child.push_back(std::vector<int>(alpha, -1));
  int p = last;
  while (p != -1 && child[p][c] == -1) {
    child[p][c] = cur;
    p = link[p];
  }
  if (p == -1) {
    link[cur] = 0;
  } else {
    int q = child[p][c];
    if (len[p] + 1 == len[q]) {
      link[cur] = q;
    } else {
      int clone = len.size();
      len.push_back(len[p] + 1);
      for (int i = 0; i < int(11); i++) {
        cnt[i].push_back(0);
      }
      link.push_back(link[q]);
      child.push_back(child[q]);
      while (p != -1 && child[p][c] == q) {
        child[p][c] = clone;
        p = link[p];
      }
      link[cur] = link[q] = clone;
    }
  }
  last = cur;
}
void make(std::string s) {
  int last = init();
  for (int i = 0, c = 0; i < int(s.size()); i++) {
    c += int(s[i] == 'z' + 1);
    add(s[i] - start_char, last, c);
  }
}
}  // namespace suffix_automaton_array
using namespace suffix_automaton_array;
int main() {
  std::ios_base::sync_with_stdio(false);
  string p;
  cin >> p;
  int n;
  cin >> n;
  vector<int> l(n), r(n);
  for (int i = 0; i < int(n); i++) {
    string q;
    cin >> q >> l[i] >> r[i];
    p += string(1, 'z' + 1) + q;
  }
  make(p);
  vector<int> C(int((p).size()) + 1), ord(int((len).size()));
  for (auto l : (len)) {
    C[l]++;
  }
  for (int i = int((C).size()) - 2; i >= 0; i--) {
    C[i] += C[i + 1];
  }
  for (int i = int((len).size()) - 1; i >= 0; i--) {
    ord[--C[len[i]]] = i;
  }
  long long ans = 0;
  for (auto o : (ord)) {
    if (link[o] != -1) {
      bool flag = true;
      for (int i = 0; i < int(n); i++) {
        if (cnt[0][o] == 0) {
          flag = false;
          break;
        }
        if (!(l[i] <= cnt[i + 1][o] && cnt[i + 1][o] <= r[i])) {
          flag = false;
          break;
        }
      }
      if (flag) {
        ans += len[o] - len[link[o]];
      }
      for (int i = 0; i < int(n + 1); i++) {
        cnt[i][link[o]] += cnt[i][o];
      }
    }
  }
  cout << ans;
  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;
namespace suffix_automaton_array {
std::vector<int> len, link, cnt[11];
std::vector<std::vector<int> > child;
const int alpha = 27, start_char = 'a';
int init() {
  len.clear();
  link.clear();
  child.clear();
  len.push_back(0);
  link.push_back(-1);
  for (int i = 0; i < int(11); i++) {
    cnt[i].push_back(0);
  }
  child.push_back(std::vector<int>(alpha, -1));
  return 0;
}
void add(int c, int &last, int f) {
  int cur = len.size();
  len.push_back(len[last] + 1);
  for (int i = 0; i < int(11); i++) {
    cnt[i].push_back(f == i);
  }
  link.push_back(-1);
  child.push_back(std::vector<int>(alpha, -1));
  int p = last;
  while (p != -1 && child[p][c] == -1) {
    child[p][c] = cur;
    p = link[p];
  }
  if (p == -1) {
    link[cur] = 0;
  } else {
    int q = child[p][c];
    if (len[p] + 1 == len[q]) {
      link[cur] = q;
    } else {
      int clone = len.size();
      len.push_back(len[p] + 1);
      for (int i = 0; i < int(11); i++) {
        cnt[i].push_back(0);
      }
      link.push_back(link[q]);
      child.push_back(child[q]);
      while (p != -1 && child[p][c] == q) {
        child[p][c] = clone;
        p = link[p];
      }
      link[cur] = link[q] = clone;
    }
  }
  last = cur;
}
void make(std::string s) {
  int last = init();
  for (int i = 0, c = 0; i < int(s.size()); i++) {
    c += int(s[i] == 'z' + 1);
    add(s[i] - start_char, last, c);
  }
}
}  // namespace suffix_automaton_array
using namespace suffix_automaton_array;
int main() {
  std::ios_base::sync_with_stdio(false);
  string p;
  cin >> p;
  int n;
  cin >> n;
  vector<int> l(n), r(n);
  for (int i = 0; i < int(n); i++) {
    string q;
    cin >> q >> l[i] >> r[i];
    p += string(1, 'z' + 1) + q;
  }
  make(p);
  vector<int> C(int((p).size()) + 1), ord(int((len).size()));
  for (auto l : (len)) {
    C[l]++;
  }
  for (int i = int((C).size()) - 2; i >= 0; i--) {
    C[i] += C[i + 1];
  }
  for (int i = int((len).size()) - 1; i >= 0; i--) {
    ord[--C[len[i]]] = i;
  }
  long long ans = 0;
  for (auto o : (ord)) {
    if (link[o] != -1) {
      bool flag = true;
      for (int i = 0; i < int(n); i++) {
        if (cnt[0][o] == 0) {
          flag = false;
          break;
        }
        if (!(l[i] <= cnt[i + 1][o] && cnt[i + 1][o] <= r[i])) {
          flag = false;
          break;
        }
      }
      if (flag) {
        ans += len[o] - len[link[o]];
      }
      for (int i = 0; i < int(n + 1); i++) {
        cnt[i][link[o]] += cnt[i][o];
      }
    }
  }
  cout << ans;
  return 0;
}

```