#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000000000")
using namespace std;
const int INF = 1e17;
struct at {
  struct leaf {
    map<char, int> gone;
    map<int, int> re_link;
    int link, len, num_link;
    leaf() : num_link((int)0){};
  };
  leaf now[1000];
  int sz, last;
  at() : sz((int)1), last((int)0) {
    now[0].link = -1;
    now[0].len = 0;
  };
  at(string s) : sz((int)1), last((int)0) {
    now[0].link = -1;
    now[0].len = 0;
    for (int i = 0; i < s.size(); i++) add(s[i]);
  }
  void add(char adi) {
    int temp = last;
    last = sz++;
    now[last].len = now[temp].len + 1;
    for (; temp >= 0 && !now[temp].gone.count(adi); temp = now[temp].link) {
      now[temp].gone[adi] = last;
      if (!temp) now[last].link = 0, now[0].re_link[last]++;
    }
    if (temp < 0) return;
    int to = now[temp].gone[adi];
    if (now[to].len == now[temp].len + 1) {
      now[last].link = to;
      now[to].re_link[last]++;
      return;
    }
    int clone = sz++;
    now[clone].gone = now[to].gone;
    now[clone].len = now[temp].len + 1;
    now[clone].link = now[to].link;
    now[clone].re_link = now[to].re_link;
    now[clone].re_link[last];
    map<int, int>::iterator it = now[now[clone].link].re_link.find(to);
    if (it != now[now[clone].link].re_link.end())
      now[now[clone].link].re_link.erase(it),
          now[now[clone].link].re_link[clone]++;
    now[to].link = clone;
    now[last].link = clone;
    for (; temp >= 0 && now[temp].gone.count(adi) && now[temp].gone[adi] == to;
         temp = now[temp].link) {
      now[temp].gone[adi] = clone;
    }
  }
};
vector<int> used;
void dfs(int id, at &ati) {
  used[id] = 1;
  for (map<int, int>::iterator it = ati.now[id].re_link.begin();
       it != ati.now[id].re_link.end(); it++) {
    int to = it->first;
    if (!used[to]) dfs(to, ati);
    ati.now[id].num_link += ati.now[to].num_link + 1;
  }
}
void count_link(at &good) {
  used.assign(good.sz, 0);
  for (int i = 0; i < good.sz; i++) {
    if (!used[i]) {
      dfs(i, good);
    }
  }
}
int base = 3571;
vector<int> counter(4000);
map<int, int> been;
int take_hash(string s) {
  int res = 0;
  for (int i = 0; i < s.size(); i++) {
    res += s[i] * counter[i];
  }
  return res;
}
int main() {
  counter[0] = base;
  for (int i = 1; i < counter.size(); i++) counter[i] = counter[i - 1] * base;
  string s, t;
  cin >> s;
  int n;
  cin >> n;
  vector<at> ini(n);
  vector<pair<int, int> > bound(n);
  for (int i = 0; i < n; i++) {
    cin >> t >> bound[i].first >> bound[i].second;
    ini[i] = at(t);
    count_link(ini[i]);
  }
  vector<int> ptr(n, 0);
  int ans = 0;
  for (int i = 0; i < s.size(); i++) {
    ptr.assign(n, 0);
    t = "";
    for (int j = i; j < s.size(); j++) {
      t += s[j];
      int sum = 0, temp;
      for (int k = 0; k < n; k++) {
        if (ptr[k] == -1)
          temp = 0;
        else {
          if (!ini[k].now[ptr[k]].gone.count(s[j])) {
            ptr[k] = -1;
            temp = 0;
          } else {
            ptr[k] = ini[k].now[ptr[k]].gone[s[j]];
            temp = ini[k].now[ptr[k]].num_link + 1;
          }
        }
        if (temp < bound[k].first || temp > bound[k].second) sum++;
      }
      if (!sum) {
        int hash = take_hash(t);
        if (!been.count(hash)) ans++, been[hash]++;
      }
    }
  }
  cout << ans;
}

### 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>
#pragma comment(linker, "/STACK:100000000000000")
using namespace std;
const int INF = 1e17;
struct at {
  struct leaf {
    map<char, int> gone;
    map<int, int> re_link;
    int link, len, num_link;
    leaf() : num_link((int)0){};
  };
  leaf now[1000];
  int sz, last;
  at() : sz((int)1), last((int)0) {
    now[0].link = -1;
    now[0].len = 0;
  };
  at(string s) : sz((int)1), last((int)0) {
    now[0].link = -1;
    now[0].len = 0;
    for (int i = 0; i < s.size(); i++) add(s[i]);
  }
  void add(char adi) {
    int temp = last;
    last = sz++;
    now[last].len = now[temp].len + 1;
    for (; temp >= 0 && !now[temp].gone.count(adi); temp = now[temp].link) {
      now[temp].gone[adi] = last;
      if (!temp) now[last].link = 0, now[0].re_link[last]++;
    }
    if (temp < 0) return;
    int to = now[temp].gone[adi];
    if (now[to].len == now[temp].len + 1) {
      now[last].link = to;
      now[to].re_link[last]++;
      return;
    }
    int clone = sz++;
    now[clone].gone = now[to].gone;
    now[clone].len = now[temp].len + 1;
    now[clone].link = now[to].link;
    now[clone].re_link = now[to].re_link;
    now[clone].re_link[last];
    map<int, int>::iterator it = now[now[clone].link].re_link.find(to);
    if (it != now[now[clone].link].re_link.end())
      now[now[clone].link].re_link.erase(it),
          now[now[clone].link].re_link[clone]++;
    now[to].link = clone;
    now[last].link = clone;
    for (; temp >= 0 && now[temp].gone.count(adi) && now[temp].gone[adi] == to;
         temp = now[temp].link) {
      now[temp].gone[adi] = clone;
    }
  }
};
vector<int> used;
void dfs(int id, at &ati) {
  used[id] = 1;
  for (map<int, int>::iterator it = ati.now[id].re_link.begin();
       it != ati.now[id].re_link.end(); it++) {
    int to = it->first;
    if (!used[to]) dfs(to, ati);
    ati.now[id].num_link += ati.now[to].num_link + 1;
  }
}
void count_link(at &good) {
  used.assign(good.sz, 0);
  for (int i = 0; i < good.sz; i++) {
    if (!used[i]) {
      dfs(i, good);
    }
  }
}
int base = 3571;
vector<int> counter(4000);
map<int, int> been;
int take_hash(string s) {
  int res = 0;
  for (int i = 0; i < s.size(); i++) {
    res += s[i] * counter[i];
  }
  return res;
}
int main() {
  counter[0] = base;
  for (int i = 1; i < counter.size(); i++) counter[i] = counter[i - 1] * base;
  string s, t;
  cin >> s;
  int n;
  cin >> n;
  vector<at> ini(n);
  vector<pair<int, int> > bound(n);
  for (int i = 0; i < n; i++) {
    cin >> t >> bound[i].first >> bound[i].second;
    ini[i] = at(t);
    count_link(ini[i]);
  }
  vector<int> ptr(n, 0);
  int ans = 0;
  for (int i = 0; i < s.size(); i++) {
    ptr.assign(n, 0);
    t = "";
    for (int j = i; j < s.size(); j++) {
      t += s[j];
      int sum = 0, temp;
      for (int k = 0; k < n; k++) {
        if (ptr[k] == -1)
          temp = 0;
        else {
          if (!ini[k].now[ptr[k]].gone.count(s[j])) {
            ptr[k] = -1;
            temp = 0;
          } else {
            ptr[k] = ini[k].now[ptr[k]].gone[s[j]];
            temp = ini[k].now[ptr[k]].num_link + 1;
          }
        }
        if (temp < bound[k].first || temp > bound[k].second) sum++;
      }
      if (!sum) {
        int hash = take_hash(t);
        if (!been.count(hash)) ans++, been[hash]++;
      }
    }
  }
  cout << ans;
}

```