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.
#include <bits/stdc++.h>
using namespace std;
int n, fr[100], to[100];
struct Suffixautomaton {
  struct Node {
    int deep, fail, id;
    int next[40];
    void clear() {
      deep = fail = 0;
      memset(next, 0, sizeof(next));
    }
  };
  int size, endp, ans;
  Node node[2 * 600000];
  void init() {
    for (int i = 0; i < 40; i++) node[0].next[i] = 1;
    node[0].deep = -1;
    node[1].clear();
    size = 2;
    endp = 1;
    ans = 0;
  }
  void insert(int c, int id) {
    int p, p1, p2;
    p = size++;
    node[p].clear();
    node[p].deep = node[endp].deep + 1;
    node[p].id = id;
    while (node[endp].next[c] == 0) {
      node[endp].next[c] = p;
      endp = node[endp].fail;
    }
    p1 = node[endp].next[c];
    if (node[p1].deep == node[endp].deep + 1)
      node[p].fail = p1;
    else {
      p2 = size++;
      node[p2] = node[p1];
      node[p].fail = node[p1].fail = p2;
      node[p2].deep = node[endp].deep + 1;
      while (node[endp].next[c] == p1) {
        node[endp].next[c] = p2;
        endp = node[endp].fail;
      }
    }
    endp = p;
  }
  int cnt[2 * 600000], st[2 * 600000];
  inline int getid(char &c) {
    if (islower(c)) return c - 'a';
    return 26 + c - 'A';
  }
  int val[600000], path[600000][11];
  int build(char *s, int len) {
    int i, j, k, x;
    for (i = 0; i < len; i++) insert(getid(s[i]), i);
    for (i = 0; i <= len; i++) cnt[i] = 0;
    for (i = 1; i < size; i++) cnt[node[i].deep]++;
    for (i = 1; i <= len; i++) cnt[i] += cnt[i - 1];
    for (i = 1; i < size; i++) st[cnt[node[i].deep]--] = i;
    for (i = size - 1; i > 0; i--) {
      x = st[i];
      for (j = 0; j <= n; j++) path[x][j] = 0;
      for (k = 0; k < 26; k++) {
        if (node[x].next[k] == 0) continue;
        for (j = 0; j <= n; j++) path[x][j] += path[node[x].next[k]][j];
      }
      for (k = 0; k <= n; k++)
        if (node[x].next[26 + k]) path[x][k]++;
    }
    for (i = 1; i < size; i++) val[i] = 0;
    val[1] = 1;
    for (i = 1; i < size; i++) {
      x = st[i];
      for (k = 0; k < 26; k++) {
        if (node[x].next[k] == 0) continue;
        val[node[x].next[k]] += val[x];
      }
    }
    int ret = 0;
    for (i = 2; i < size; i++) {
      if (path[i][0] == 0) continue;
      for (j = 1; j <= n; j++) {
        if (path[i][j] < fr[j - 1] || path[i][j] > to[j - 1]) break;
      }
      if (j <= n) continue;
      ret += val[i];
    }
    return ret;
  }
} sam;
char s[600000], t[600000];
int main() {
  scanf("%s", s);
  strcat(s, "A");
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s%d%d", t, fr + i, to + i);
    strcat(s, t);
    char p[10];
    p[0] = i + 'B';
    p[1] = 0;
    strcat(s, p);
  }
  sam.init();
  int len = strlen(s);
  printf("%d\n", sam.build(s, len));
  return 0;
}
