#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 = 1010100;
struct vertex {
  int v, l, r, prev, link, sid;
  map<char, int> nxt;
  vertex(int sid = -1, int l = -1, int r = -1, int prev = -1)
      : sid(sid), l(l), r(r), prev(prev), link(-1) {
    if (l > r) cerr << l << " " << r << endl;
  }
  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; }
};
int size, d[MAXN][13];
int n, l[13], r[13];
vertex tree[MAXN];
string s[13];
state cur;
inline char last(const vertex &v) {
  if (v.r == 0) return 0;
  return s[v.sid][v.r - 1];
}
void calc(int v) {
  if (tree[v].nxt.empty()) {
    d[v][last(tree[v]) - 'A'] = 1;
    return;
  }
  for (map<char, int>::iterator i = tree[v].nxt.begin(); i != tree[v].nxt.end();
       i++) {
    int u = i->second;
    calc(u);
    for (int j = 0; j < (int)(n + 1); j++) d[v][j] += d[u][j];
  }
}
int addEdge(int sid, int prev, int l, int r) {
  int v = size++;
  tree[v] = vertex(sid, l, r, prev);
  tree[prev].next(s[sid][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 sid = tree[v].sid;
  int mid = tree[v].l + up;
  int ans = addEdge(sid, prev, tree[v].l, mid);
  tree[v].l = mid;
  tree[v].prev = ans;
  tree[ans].next(s[sid][mid]) = v;
  return ans;
}
state go(int v, int sid, int l, int r) {
  if (l == r) return state(v, 0);
  while (true) {
    int u = tree[v].next(s[sid][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, tree[v].sid, 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].sid][tree[v].r - pos] == c)
    return state(v, pos - 1);
  return state(-1, -1);
}
state extend(int sid, state v, int i) {
  char c = s[sid][i];
  while (true) {
    state nv = go(v, c);
    if (nv.correct()) return nv;
    int mid = split(v);
    addEdge(sid, mid, i, (int)s[sid].size());
    v = state(link(mid), 0);
    if (mid == 0) return v;
  }
  throw;
}
long long DFS(int v) {
  bool ok = true;
  for (int i = 0; i < (int)(n + 1); i++)
    if (d[v][i] < l[i] || d[v][i] > r[i]) {
      ok = false;
      break;
    }
  long long ans = ok ? (tree[v].r - tree[v].l) : 0;
  if (last(tree[v]) == 'A') ans = max(0LL, ans - 1);
  for (map<char, int>::iterator i = tree[v].nxt.begin(); i != tree[v].nxt.end();
       i++)
    ans += DFS(i->second);
  return ans;
}
int main() {
  tree[0] = vertex(0, 0, 0, 0);
  tree[0].link = 0;
  size = 1;
  cin >> s[0];
  l[0] = 1;
  r[0] = INF;
  n = nextInt();
  for (int i = 0; i < (int)(n + 1); i++) {
    if (i != 0) {
      cin >> s[i];
      l[i] = nextInt();
      r[i] = nextInt();
    }
    s[i] += char('A' + i);
    state cur(0, 0);
    for (int j = 0; j < (int)(s[i].size()); j++) cur = extend(i, cur, j);
  }
  calc(0);
  cout << DFS(0) << endl;
  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>
#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 = 1010100;
struct vertex {
  int v, l, r, prev, link, sid;
  map<char, int> nxt;
  vertex(int sid = -1, int l = -1, int r = -1, int prev = -1)
      : sid(sid), l(l), r(r), prev(prev), link(-1) {
    if (l > r) cerr << l << " " << r << endl;
  }
  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; }
};
int size, d[MAXN][13];
int n, l[13], r[13];
vertex tree[MAXN];
string s[13];
state cur;
inline char last(const vertex &v) {
  if (v.r == 0) return 0;
  return s[v.sid][v.r - 1];
}
void calc(int v) {
  if (tree[v].nxt.empty()) {
    d[v][last(tree[v]) - 'A'] = 1;
    return;
  }
  for (map<char, int>::iterator i = tree[v].nxt.begin(); i != tree[v].nxt.end();
       i++) {
    int u = i->second;
    calc(u);
    for (int j = 0; j < (int)(n + 1); j++) d[v][j] += d[u][j];
  }
}
int addEdge(int sid, int prev, int l, int r) {
  int v = size++;
  tree[v] = vertex(sid, l, r, prev);
  tree[prev].next(s[sid][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 sid = tree[v].sid;
  int mid = tree[v].l + up;
  int ans = addEdge(sid, prev, tree[v].l, mid);
  tree[v].l = mid;
  tree[v].prev = ans;
  tree[ans].next(s[sid][mid]) = v;
  return ans;
}
state go(int v, int sid, int l, int r) {
  if (l == r) return state(v, 0);
  while (true) {
    int u = tree[v].next(s[sid][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, tree[v].sid, 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].sid][tree[v].r - pos] == c)
    return state(v, pos - 1);
  return state(-1, -1);
}
state extend(int sid, state v, int i) {
  char c = s[sid][i];
  while (true) {
    state nv = go(v, c);
    if (nv.correct()) return nv;
    int mid = split(v);
    addEdge(sid, mid, i, (int)s[sid].size());
    v = state(link(mid), 0);
    if (mid == 0) return v;
  }
  throw;
}
long long DFS(int v) {
  bool ok = true;
  for (int i = 0; i < (int)(n + 1); i++)
    if (d[v][i] < l[i] || d[v][i] > r[i]) {
      ok = false;
      break;
    }
  long long ans = ok ? (tree[v].r - tree[v].l) : 0;
  if (last(tree[v]) == 'A') ans = max(0LL, ans - 1);
  for (map<char, int>::iterator i = tree[v].nxt.begin(); i != tree[v].nxt.end();
       i++)
    ans += DFS(i->second);
  return ans;
}
int main() {
  tree[0] = vertex(0, 0, 0, 0);
  tree[0].link = 0;
  size = 1;
  cin >> s[0];
  l[0] = 1;
  r[0] = INF;
  n = nextInt();
  for (int i = 0; i < (int)(n + 1); i++) {
    if (i != 0) {
      cin >> s[i];
      l[i] = nextInt();
      r[i] = nextInt();
    }
    s[i] += char('A' + i);
    state cur(0, 0);
    for (int j = 0; j < (int)(s[i].size()); j++) cur = extend(i, cur, j);
  }
  calc(0);
  cout << DFS(0) << endl;
  return 0;
}

```