#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:200000000")
using namespace std;
template <typename T>
inline T Abs(T x) {
  return (x >= 0) ? x : -x;
}
template <typename T>
inline T sqr(T x) {
  return x * x;
}
template <typename T>
inline string toStr(T x) {
  stringstream st;
  st << x;
  string s;
  st >> s;
  return s;
}
template <typename T>
inline int bit(T mask, int b) {
  return (b >= 0 && (mask & (T(1) << b)) != 0) ? 1 : 0;
}
inline int nextInt() {
  int x;
  if (scanf("%d", &x) != 1) throw;
  return x;
}
inline long long nextInt64() {
  long long x;
  if (scanf("%I64d", &x) != 1) throw;
  return x;
}
inline double nextDouble() {
  double x;
  if (scanf("%lf", &x) != 1) throw;
  return x;
}
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double EPS = 1E-9;
const long double PI = 3.1415926535897932384626433832795;
const int MAXN = 20100;
const long long MOD = INF + 9;
struct vertex {
  int v, l, r, prev, link;
  map<char, int> nxt;
  vertex(int l = -1, int r = -1, int prev = -1)
      : l(l), r(r), prev(prev), link(-1) {}
  bool has(char c) { return nxt.count(c) != 0; }
  int &next(char c) { return nxt[c]; }
  int length() { return r - l; }
};
struct state {
  int v, pos;
  explicit state(int v = 0, int pos = 0) : v(v), pos(pos) {}
  bool correct() { return v >= 0 && pos >= 0; }
};
struct sufftree {
  int size, d[MAXN];
  vertex tree[MAXN];
  state cur;
  string s;
  void build(string s) {
    this->s = s + "#";
    size = 1;
    reset();
    memset(d, 255, sizeof d);
    tree[0] = vertex(0, 0, 0);
    tree[0].link = 0;
    for (int i = 0; i < (int)(this->s.size()); i++) cur = extend(cur, i);
  }
  void reset() { cur = state(0, 0); }
  int calc(int v) {
    int &ans = d[v];
    if (ans != -1) return ans;
    ans = tree[v].nxt.empty() ? 1 : 0;
    for (map<char, int>::iterator i = tree[v].nxt.begin();
         i != tree[v].nxt.end(); i++)
      ans += calc(i->second);
    return ans;
  }
  int move(char c) {
    if (!cur.correct()) return 0;
    cur = go(cur, c);
    if (!cur.correct()) return 0;
    return calc(cur.v);
  }
  int addEdge(int prev, int l, int r) {
    int v = size++;
    tree[v] = vertex(l, r, prev);
    tree[prev].next(s[l]) = v;
    return v;
  }
  int split(state cur) {
    int v = cur.v, prev = tree[v].prev;
    int down = cur.pos, up = tree[v].length() - down;
    if (up == 0) return prev;
    if (down == 0) return v;
    int mid = tree[v].l + up;
    int ans = addEdge(prev, tree[v].l, mid);
    tree[v].l = mid;
    tree[v].prev = ans;
    tree[ans].next(s[mid]) = v;
    return ans;
  }
  state go(int v, int l, int r) {
    if (l == r) return state(v, 0);
    while (true) {
      int u = tree[v].next(s[l]);
      int len = tree[u].length();
      if (len >= r - l) return state(u, len - r + l);
      l += len;
      v = u;
    }
    throw;
  }
  int link(int v) {
    int &ans = tree[v].link;
    if (ans != -1) return ans;
    int lp = link(tree[v].prev);
    int ll = tree[v].l + ((tree[v].prev == 0) ? 1 : 0);
    return ans = split(go(lp, ll, tree[v].r));
  }
  state go(state cur, char c) {
    int v = cur.v, pos = cur.pos;
    if (pos == 0 && tree[v].has(c)) {
      int u = tree[v].next(c);
      return state(u, tree[u].length() - 1);
    }
    if (pos != 0 && s[tree[v].r - pos] == c) return state(v, pos - 1);
    return state(-1, -1);
  }
  state extend(state v, int i) {
    char c = s[i];
    while (true) {
      state nv = go(v, c);
      if (nv.correct()) return nv;
      int mid = split(v);
      addEdge(mid, i, (int)s.size());
      v = state(link(mid), 0);
      if (mid == 0) return v;
    }
    throw;
  }
};
int n, l[13], r[13];
sufftree tree[13];
int main() {
  string s;
  cin >> s;
  n = nextInt();
  for (int i = 0; i < (int)(n); i++) {
    string t;
    cin >> t;
    tree[i].build(t);
    l[i] = nextInt();
    r[i] = nextInt();
  }
  set<string> st;
  for (int lf = 0; lf < (int)(s.size()); lf++) {
    for (int i = 0; i < (int)(n); i++) tree[i].reset();
    long long h = 0, pw = 1;
    for (int rg = lf; rg < (int)s.size(); rg++) {
      bool ok = true;
      for (int i = 0; i < (int)(n); i++) {
        int cur = tree[i].move(s[rg]);
        if (!(l[i] <= cur && cur <= r[i])) ok = false;
      }
      h = (h + pw * s[rg]) % MOD;
      pw = (pw * 313) % MOD;
      if (ok) st.insert(s.substr(lf, rg - lf + 1));
    }
  }
  cout << st.size() << endl;
  return 0;
}

### Prompt

Please provide a CPP coded solution to the problem described below:

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:200000000")
using namespace std;
template <typename T>
inline T Abs(T x) {
  return (x >= 0) ? x : -x;
}
template <typename T>
inline T sqr(T x) {
  return x * x;
}
template <typename T>
inline string toStr(T x) {
  stringstream st;
  st << x;
  string s;
  st >> s;
  return s;
}
template <typename T>
inline int bit(T mask, int b) {
  return (b >= 0 && (mask & (T(1) << b)) != 0) ? 1 : 0;
}
inline int nextInt() {
  int x;
  if (scanf("%d", &x) != 1) throw;
  return x;
}
inline long long nextInt64() {
  long long x;
  if (scanf("%I64d", &x) != 1) throw;
  return x;
}
inline double nextDouble() {
  double x;
  if (scanf("%lf", &x) != 1) throw;
  return x;
}
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double EPS = 1E-9;
const long double PI = 3.1415926535897932384626433832795;
const int MAXN = 20100;
const long long MOD = INF + 9;
struct vertex {
  int v, l, r, prev, link;
  map<char, int> nxt;
  vertex(int l = -1, int r = -1, int prev = -1)
      : l(l), r(r), prev(prev), link(-1) {}
  bool has(char c) { return nxt.count(c) != 0; }
  int &next(char c) { return nxt[c]; }
  int length() { return r - l; }
};
struct state {
  int v, pos;
  explicit state(int v = 0, int pos = 0) : v(v), pos(pos) {}
  bool correct() { return v >= 0 && pos >= 0; }
};
struct sufftree {
  int size, d[MAXN];
  vertex tree[MAXN];
  state cur;
  string s;
  void build(string s) {
    this->s = s + "#";
    size = 1;
    reset();
    memset(d, 255, sizeof d);
    tree[0] = vertex(0, 0, 0);
    tree[0].link = 0;
    for (int i = 0; i < (int)(this->s.size()); i++) cur = extend(cur, i);
  }
  void reset() { cur = state(0, 0); }
  int calc(int v) {
    int &ans = d[v];
    if (ans != -1) return ans;
    ans = tree[v].nxt.empty() ? 1 : 0;
    for (map<char, int>::iterator i = tree[v].nxt.begin();
         i != tree[v].nxt.end(); i++)
      ans += calc(i->second);
    return ans;
  }
  int move(char c) {
    if (!cur.correct()) return 0;
    cur = go(cur, c);
    if (!cur.correct()) return 0;
    return calc(cur.v);
  }
  int addEdge(int prev, int l, int r) {
    int v = size++;
    tree[v] = vertex(l, r, prev);
    tree[prev].next(s[l]) = v;
    return v;
  }
  int split(state cur) {
    int v = cur.v, prev = tree[v].prev;
    int down = cur.pos, up = tree[v].length() - down;
    if (up == 0) return prev;
    if (down == 0) return v;
    int mid = tree[v].l + up;
    int ans = addEdge(prev, tree[v].l, mid);
    tree[v].l = mid;
    tree[v].prev = ans;
    tree[ans].next(s[mid]) = v;
    return ans;
  }
  state go(int v, int l, int r) {
    if (l == r) return state(v, 0);
    while (true) {
      int u = tree[v].next(s[l]);
      int len = tree[u].length();
      if (len >= r - l) return state(u, len - r + l);
      l += len;
      v = u;
    }
    throw;
  }
  int link(int v) {
    int &ans = tree[v].link;
    if (ans != -1) return ans;
    int lp = link(tree[v].prev);
    int ll = tree[v].l + ((tree[v].prev == 0) ? 1 : 0);
    return ans = split(go(lp, ll, tree[v].r));
  }
  state go(state cur, char c) {
    int v = cur.v, pos = cur.pos;
    if (pos == 0 && tree[v].has(c)) {
      int u = tree[v].next(c);
      return state(u, tree[u].length() - 1);
    }
    if (pos != 0 && s[tree[v].r - pos] == c) return state(v, pos - 1);
    return state(-1, -1);
  }
  state extend(state v, int i) {
    char c = s[i];
    while (true) {
      state nv = go(v, c);
      if (nv.correct()) return nv;
      int mid = split(v);
      addEdge(mid, i, (int)s.size());
      v = state(link(mid), 0);
      if (mid == 0) return v;
    }
    throw;
  }
};
int n, l[13], r[13];
sufftree tree[13];
int main() {
  string s;
  cin >> s;
  n = nextInt();
  for (int i = 0; i < (int)(n); i++) {
    string t;
    cin >> t;
    tree[i].build(t);
    l[i] = nextInt();
    r[i] = nextInt();
  }
  set<string> st;
  for (int lf = 0; lf < (int)(s.size()); lf++) {
    for (int i = 0; i < (int)(n); i++) tree[i].reset();
    long long h = 0, pw = 1;
    for (int rg = lf; rg < (int)s.size(); rg++) {
      bool ok = true;
      for (int i = 0; i < (int)(n); i++) {
        int cur = tree[i].move(s[rg]);
        if (!(l[i] <= cur && cur <= r[i])) ok = false;
      }
      h = (h + pw * s[rg]) % MOD;
      pw = (pw * 313) % MOD;
      if (ok) st.insert(s.substr(lf, rg - lf + 1));
    }
  }
  cout << st.size() << endl;
  return 0;
}

```