#include <bits/stdc++.h>
using namespace std;
struct CNode {
  char c;
  int isOk;
  CNode* child;
  CNode* sib;
};
void addNode(CNode* node, const string& s, int pos, int isOk, bool canAdd) {
  if (pos == s.size()) {
    if (node->isOk) {
      node->isOk = isOk;
    }
    return;
  }
  if (node->child == 0) {
    if (!canAdd) {
      return;
    }
    node->child = new CNode;
    CNode* child = node->child;
    child->c = s[pos];
    child->child = 0;
    child->sib = 0;
    child->isOk = isOk;
    assert(pos + 1 == s.size());
    addNode(child, s, pos + 1, isOk, canAdd);
    return;
  }
  CNode* child = node->child;
  while (true) {
    if (child->c == s[pos]) {
      addNode(child, s, pos + 1, isOk, canAdd);
      return;
    }
    if (child->sib == 0) {
      break;
    }
    child = child->sib;
  }
  if (!canAdd) {
    return;
  }
  child->sib = new CNode;
  child = child->sib;
  child->c = s[pos];
  child->child = 0;
  child->sib = 0;
  child->isOk = isOk;
  assert(pos + 1 == s.size());
  addNode(child, s, pos + 1, isOk, canAdd);
}
CNode* find(CNode* node, const string& s, int pos) {
  if (pos == s.size()) {
    return node;
  }
  CNode* child = node->child;
  while (child) {
    if (child->c == s[pos]) {
      return find(child, s, pos + 1);
    }
    child = child->sib;
  }
  return 0;
}
void walk(CNode* node, int isOk) {
  if (!node) {
    return;
  }
  if (node->isOk < isOk) {
    node->isOk = 0;
  }
  CNode* child = node->child;
  while (child) {
    walk(child, isOk);
    child = child->sib;
  }
}
int main() {
  string s;
  while (cin >> s) {
    int n;
    cin >> n;
    CNode root;
    root.child = 0;
    root.sib = 0;
    bool canNull = true;
    for (int k = 0; k < n; k++) {
      string ps;
      cin >> ps;
      int l, r;
      cin >> l >> r;
      map<string, int> scount;
      for (int i = 0; i < ps.size(); i++) {
        for (int j = 1; j + i <= ps.size(); j++) {
          string subs = ps.substr(i, j);
          if (scount.find(subs) != scount.end()) {
            scount[subs]++;
          } else {
            scount[subs] = 1;
          }
        }
      }
      map<string, int>::iterator it;
      for (it = scount.begin(); it != scount.end(); it++) {
        const bool isOk = (it->second >= l) && (it->second <= r);
        addNode(&root, it->first, 0, isOk ? k + 1 : 0, canNull);
      }
      canNull &= (l == 0);
      if (l > 0) {
        walk(&root, k + 1);
      }
    }
    set<string> sSet;
    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
      for (int j = 1; j + i <= s.size(); j++) {
        string subs = s.substr(i, j);
        sSet.insert(subs);
      }
    }
    set<string>::iterator it;
    for (it = sSet.begin(); it != sSet.end(); it++) {
      CNode* node = find(&root, *it, 0);
      if (node != 0) {
      }
      if (node == 0 && canNull) {
        ans++;
      } else if (node != 0 && node->isOk) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

### Prompt

Your challenge is to write a CPP solution 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;
struct CNode {
  char c;
  int isOk;
  CNode* child;
  CNode* sib;
};
void addNode(CNode* node, const string& s, int pos, int isOk, bool canAdd) {
  if (pos == s.size()) {
    if (node->isOk) {
      node->isOk = isOk;
    }
    return;
  }
  if (node->child == 0) {
    if (!canAdd) {
      return;
    }
    node->child = new CNode;
    CNode* child = node->child;
    child->c = s[pos];
    child->child = 0;
    child->sib = 0;
    child->isOk = isOk;
    assert(pos + 1 == s.size());
    addNode(child, s, pos + 1, isOk, canAdd);
    return;
  }
  CNode* child = node->child;
  while (true) {
    if (child->c == s[pos]) {
      addNode(child, s, pos + 1, isOk, canAdd);
      return;
    }
    if (child->sib == 0) {
      break;
    }
    child = child->sib;
  }
  if (!canAdd) {
    return;
  }
  child->sib = new CNode;
  child = child->sib;
  child->c = s[pos];
  child->child = 0;
  child->sib = 0;
  child->isOk = isOk;
  assert(pos + 1 == s.size());
  addNode(child, s, pos + 1, isOk, canAdd);
}
CNode* find(CNode* node, const string& s, int pos) {
  if (pos == s.size()) {
    return node;
  }
  CNode* child = node->child;
  while (child) {
    if (child->c == s[pos]) {
      return find(child, s, pos + 1);
    }
    child = child->sib;
  }
  return 0;
}
void walk(CNode* node, int isOk) {
  if (!node) {
    return;
  }
  if (node->isOk < isOk) {
    node->isOk = 0;
  }
  CNode* child = node->child;
  while (child) {
    walk(child, isOk);
    child = child->sib;
  }
}
int main() {
  string s;
  while (cin >> s) {
    int n;
    cin >> n;
    CNode root;
    root.child = 0;
    root.sib = 0;
    bool canNull = true;
    for (int k = 0; k < n; k++) {
      string ps;
      cin >> ps;
      int l, r;
      cin >> l >> r;
      map<string, int> scount;
      for (int i = 0; i < ps.size(); i++) {
        for (int j = 1; j + i <= ps.size(); j++) {
          string subs = ps.substr(i, j);
          if (scount.find(subs) != scount.end()) {
            scount[subs]++;
          } else {
            scount[subs] = 1;
          }
        }
      }
      map<string, int>::iterator it;
      for (it = scount.begin(); it != scount.end(); it++) {
        const bool isOk = (it->second >= l) && (it->second <= r);
        addNode(&root, it->first, 0, isOk ? k + 1 : 0, canNull);
      }
      canNull &= (l == 0);
      if (l > 0) {
        walk(&root, k + 1);
      }
    }
    set<string> sSet;
    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
      for (int j = 1; j + i <= s.size(); j++) {
        string subs = s.substr(i, j);
        sSet.insert(subs);
      }
    }
    set<string>::iterator it;
    for (it = sSet.begin(); it != sSet.end(); it++) {
      CNode* node = find(&root, *it, 0);
      if (node != 0) {
      }
      if (node == 0 && canNull) {
        ans++;
      } else if (node != 0 && node->isOk) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

```