#include <bits/stdc++.h>
using namespace std;
int wa[700007], wb[700007], wv[700007], wy[700007];
int sa[700007], rk[700007], heg[700007];
int rmq[21][700007], mp[700007], vis[700007];
int a[700007];
char r[700007], s[700007];
int n;
bool cmp(int *r, int i, int j, int len) {
  return r[i] == r[j] && r[i + len] == r[j + len];
}
void suffix(int *r, int n, int m) {
  int *x = wa, *y = wb, *t;
  int i, j, p;
  for (int i = 0; i < m; ++i) wy[i] = 0;
  for (int i = 0; i < n; ++i) wy[x[i] = r[i]]++;
  for (int i = 1; i < m; ++i) wy[i] += wy[i - 1];
  for (int i = n - 1; i >= 0; --i) sa[--wy[x[i]]] = i;
  for (j = 1, p = 0; p < n; j <<= 1, m = p) {
    for (p = 0, i = n - j; i < n; ++i) y[p++] = i;
    for (i = 0; i < n; ++i)
      if (sa[i] >= j) y[p++] = sa[i] - j;
    for (i = 0; i < m; ++i) wy[i] = 0;
    for (i = 0; i < n; ++i) wy[wv[i] = x[y[i]]]++;
    for (i = 1; i < m; ++i) wy[i] += wy[i - 1];
    for (i = n - 1; i >= 0; --i) sa[--wy[wv[i]]] = y[i];
    t = x, x = y, y = t, x[sa[0]] = 0;
    for (i = 1, p = 1; i < n; ++i)
      x[sa[i]] = cmp(y, sa[i], sa[i - 1], j) ? p - 1 : p++;
  }
}
void setHeight(int *r, int n) {
  int i, j, k;
  for (i = 0; i <= n; ++i) rk[sa[i]] = i;
  for (i = 0, k = 0; i < n; heg[rk[i++]] = k)
    for (k = k ? k - 1 : 0, j = sa[rk[i] - 1]; r[i + k] == r[j + k]; ++k)
      ;
}
void setRMQ(int n) {
  int i, j, a, b;
  mp[0] = -1;
  for (i = 1; i <= n; ++i)
    if (i & (i - 1))
      mp[i] = mp[i - 1];
    else
      mp[i] = mp[i - 1] + 1;
  for (i = 0; i <= n; ++i) rmq[0][i] = heg[i];
  for (j = 1; j <= mp[n]; ++j)
    for (i = 0; i + (1 << j) - 1 < n; ++i) {
      a = rmq[j - 1][i];
      b = rmq[j - 1][i + (1 << (j - 1))];
      rmq[j][i] = min(a, b);
    }
}
int lcp(int a, int b) {
  a = rk[a], b = rk[b];
  if (a > b) swap(a, b);
  if (a == b) return 700007;
  ++a;
  int j = mp[b - a + 1];
  int x = rmq[j][a];
  b -= (1 << j) - 1;
  int y = rmq[j][b];
  return min(x, y);
}
int be[700007];
int sum[11][700007], mylen;
void init(int n, int len) {
  memset(sum, 0, sizeof(sum));
  for (int i = 1; i <= len; ++i) {
    if (be[sa[i]] >= 0) sum[be[sa[i]]][i]++;
    for (int j = 0; j <= n; ++j) sum[j][i] += sum[j][i - 1];
  }
}
int LL[11], RR[11];
int up[11], down[11];
int findLen(int st, int x, int lp, int len) {
  int L = -1, R = -1;
  int l, r, mid;
  if (heg[st + 1] < lp)
    R = st;
  else {
    l = st, r = len;
    while (l < r) {
      mid = (l + r + 1) >> 1;
      if (lcp(sa[st], sa[mid]) >= lp)
        l = mid;
      else
        r = mid - 1;
    }
    R = l;
  }
  if (heg[st] < lp)
    L = st;
  else {
    l = 1, r = st;
    while (l < r) {
      mid = (l + r) >> 1;
      if (lcp(sa[mid], sa[st]) >= lp)
        r = mid;
      else
        l = mid + 1;
    }
    L = l;
  }
  return sum[x][R] - sum[x][L - 1];
}
int lft[700007], rht[700007];
void init(int len) {
  suffix(a, len + 1, 30);
  setHeight(a, len);
  for (int i = 1; i <= len; ++i) {
    lft[sa[i]] = heg[i];
    rht[sa[i]] = len - sa[i];
  }
}
int solve(int x, int len) {
  int l, r, mid;
  int mi = lft[x], ma = rht[x];
  for (int i = 1; i <= n; ++i) {
    l = lft[x], r = rht[x];
    while (l < r) {
      mid = (l + r + 1) >> 1;
      if (findLen(rk[x], i, mid, len) >= down[i])
        l = mid;
      else
        r = mid - 1;
    }
    if (ma > r) ma = r;
    l = lft[x] + 1, r = rht[x] + 1;
    while (l < r) {
      mid = (l + r) >> 1;
      if (findLen(rk[x], i, mid, len) <= up[i])
        r = mid;
      else
        l = mid + 1;
    }
    if (findLen(rk[x], i, l, len) <= up[i]) --l;
    if (mi < l) mi = l;
  }
  return ma - min(ma, mi);
}
int main() {
  int m, len;
  m = 27, len = 0;
  scanf("%s", s);
  for (int i = 0; s[i]; ++i) {
    be[len] = 0;
    a[len++] = s[i] - 'a' + 1;
  }
  a[len] = 0;
  init(len);
  mylen = len;
  be[len] = -1;
  a[len++] = m++;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s);
    scanf("%d%d", down + i, up + i);
    for (int j = 0; s[j]; ++j) {
      be[len] = i;
      a[len++] = s[j] - 'a' + 1;
    }
    be[len] = -1;
    a[len++] = m++;
  }
  a[len] = 0;
  suffix(a, len + 1, m);
  setHeight(a, len);
  setRMQ(len);
  init(n, len);
  long long ans = 0;
  for (int i = 0; i < mylen; ++i)
    if (rht[i] > lft[i]) ans += solve(i, len);
  cout << ans << endl;
}

### Prompt

Please create a solution in CPP 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;
int wa[700007], wb[700007], wv[700007], wy[700007];
int sa[700007], rk[700007], heg[700007];
int rmq[21][700007], mp[700007], vis[700007];
int a[700007];
char r[700007], s[700007];
int n;
bool cmp(int *r, int i, int j, int len) {
  return r[i] == r[j] && r[i + len] == r[j + len];
}
void suffix(int *r, int n, int m) {
  int *x = wa, *y = wb, *t;
  int i, j, p;
  for (int i = 0; i < m; ++i) wy[i] = 0;
  for (int i = 0; i < n; ++i) wy[x[i] = r[i]]++;
  for (int i = 1; i < m; ++i) wy[i] += wy[i - 1];
  for (int i = n - 1; i >= 0; --i) sa[--wy[x[i]]] = i;
  for (j = 1, p = 0; p < n; j <<= 1, m = p) {
    for (p = 0, i = n - j; i < n; ++i) y[p++] = i;
    for (i = 0; i < n; ++i)
      if (sa[i] >= j) y[p++] = sa[i] - j;
    for (i = 0; i < m; ++i) wy[i] = 0;
    for (i = 0; i < n; ++i) wy[wv[i] = x[y[i]]]++;
    for (i = 1; i < m; ++i) wy[i] += wy[i - 1];
    for (i = n - 1; i >= 0; --i) sa[--wy[wv[i]]] = y[i];
    t = x, x = y, y = t, x[sa[0]] = 0;
    for (i = 1, p = 1; i < n; ++i)
      x[sa[i]] = cmp(y, sa[i], sa[i - 1], j) ? p - 1 : p++;
  }
}
void setHeight(int *r, int n) {
  int i, j, k;
  for (i = 0; i <= n; ++i) rk[sa[i]] = i;
  for (i = 0, k = 0; i < n; heg[rk[i++]] = k)
    for (k = k ? k - 1 : 0, j = sa[rk[i] - 1]; r[i + k] == r[j + k]; ++k)
      ;
}
void setRMQ(int n) {
  int i, j, a, b;
  mp[0] = -1;
  for (i = 1; i <= n; ++i)
    if (i & (i - 1))
      mp[i] = mp[i - 1];
    else
      mp[i] = mp[i - 1] + 1;
  for (i = 0; i <= n; ++i) rmq[0][i] = heg[i];
  for (j = 1; j <= mp[n]; ++j)
    for (i = 0; i + (1 << j) - 1 < n; ++i) {
      a = rmq[j - 1][i];
      b = rmq[j - 1][i + (1 << (j - 1))];
      rmq[j][i] = min(a, b);
    }
}
int lcp(int a, int b) {
  a = rk[a], b = rk[b];
  if (a > b) swap(a, b);
  if (a == b) return 700007;
  ++a;
  int j = mp[b - a + 1];
  int x = rmq[j][a];
  b -= (1 << j) - 1;
  int y = rmq[j][b];
  return min(x, y);
}
int be[700007];
int sum[11][700007], mylen;
void init(int n, int len) {
  memset(sum, 0, sizeof(sum));
  for (int i = 1; i <= len; ++i) {
    if (be[sa[i]] >= 0) sum[be[sa[i]]][i]++;
    for (int j = 0; j <= n; ++j) sum[j][i] += sum[j][i - 1];
  }
}
int LL[11], RR[11];
int up[11], down[11];
int findLen(int st, int x, int lp, int len) {
  int L = -1, R = -1;
  int l, r, mid;
  if (heg[st + 1] < lp)
    R = st;
  else {
    l = st, r = len;
    while (l < r) {
      mid = (l + r + 1) >> 1;
      if (lcp(sa[st], sa[mid]) >= lp)
        l = mid;
      else
        r = mid - 1;
    }
    R = l;
  }
  if (heg[st] < lp)
    L = st;
  else {
    l = 1, r = st;
    while (l < r) {
      mid = (l + r) >> 1;
      if (lcp(sa[mid], sa[st]) >= lp)
        r = mid;
      else
        l = mid + 1;
    }
    L = l;
  }
  return sum[x][R] - sum[x][L - 1];
}
int lft[700007], rht[700007];
void init(int len) {
  suffix(a, len + 1, 30);
  setHeight(a, len);
  for (int i = 1; i <= len; ++i) {
    lft[sa[i]] = heg[i];
    rht[sa[i]] = len - sa[i];
  }
}
int solve(int x, int len) {
  int l, r, mid;
  int mi = lft[x], ma = rht[x];
  for (int i = 1; i <= n; ++i) {
    l = lft[x], r = rht[x];
    while (l < r) {
      mid = (l + r + 1) >> 1;
      if (findLen(rk[x], i, mid, len) >= down[i])
        l = mid;
      else
        r = mid - 1;
    }
    if (ma > r) ma = r;
    l = lft[x] + 1, r = rht[x] + 1;
    while (l < r) {
      mid = (l + r) >> 1;
      if (findLen(rk[x], i, mid, len) <= up[i])
        r = mid;
      else
        l = mid + 1;
    }
    if (findLen(rk[x], i, l, len) <= up[i]) --l;
    if (mi < l) mi = l;
  }
  return ma - min(ma, mi);
}
int main() {
  int m, len;
  m = 27, len = 0;
  scanf("%s", s);
  for (int i = 0; s[i]; ++i) {
    be[len] = 0;
    a[len++] = s[i] - 'a' + 1;
  }
  a[len] = 0;
  init(len);
  mylen = len;
  be[len] = -1;
  a[len++] = m++;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s);
    scanf("%d%d", down + i, up + i);
    for (int j = 0; s[j]; ++j) {
      be[len] = i;
      a[len++] = s[j] - 'a' + 1;
    }
    be[len] = -1;
    a[len++] = m++;
  }
  a[len] = 0;
  suffix(a, len + 1, m);
  setHeight(a, len);
  setRMQ(len);
  init(n, len);
  long long ans = 0;
  for (int i = 0; i < mylen; ++i)
    if (rht[i] > lft[i]) ans += solve(i, len);
  cout << ans << endl;
}

```