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>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
pair<int, int> df[11];
struct suff_auto {
  struct vert {
    map<char, int> nex;
    int cn[10];
    int len, link;
    int u;
    vert() {
      len = link = u = 0;
      for (int i = 0; i < 10; i++) cn[i] = 0;
    }
  };
  vector<vert> ms;
  vector<bool> us;
  int cnt, last;
  suff_auto() {
    ms.push_back(vert());
    cnt = 1, last = 0;
    ms[0].link = -1;
  }
  void upd(char e) {
    int newcnt = cnt + 1;
    ms.push_back(vert());
    ms[cnt].len = ms[last].len + 1;
    while (last != -1 && ms[last].nex[e] == 0) {
      ms[last].nex[e] = cnt;
      last = ms[last].link;
    }
    if (last == -1)
      ms[cnt].link = 0;
    else if (ms[ms[last].nex[e]].len == ms[last].len + 1)
      ms[cnt].link = ms[last].nex[e];
    else {
      newcnt = cnt + 2;
      ms.push_back(vert());
      int q = ms[last].nex[e];
      ms[cnt + 1].len = ms[last].len + 1;
      ms[cnt + 1].nex = ms[q].nex;
      ms[cnt + 1].link = ms[q].link;
      ms[q].link = ms[cnt].link = cnt + 1;
      while (last != -1 && ms[last].nex[e] == q) {
        ms[last].nex[e] = cnt + 1;
        last = ms[last].link;
      }
    }
    last = cnt;
    cnt = newcnt;
  }
  void dfs(int v, int g, long long &s) {
    us[v] = 1;
    for (auto it : ms[v].nex) {
      if (!us[it.second]) dfs(it.second, g, s);
      if ((it.first >= '0' && it.first <= '9') || it.first == '#') {
        if (it.first == '#')
          ms[v].u++;
        else
          ms[v].cn[(int)it.first - '0']++;
      } else {
        for (int i = 0; i <= 9; i++) ms[v].cn[i] += ms[it.second].cn[i];
        ms[v].u += ms[it.second].u;
      }
    }
    if (v != 0) {
      int l = ms[ms[v].link].len + 1, r = ms[v].len;
      bool tr = (ms[v].u != 0);
      for (int i = 0; i < g; i++) {
        if (ms[v].cn[i] < df[i].first || ms[v].cn[i] > df[i].second) tr = false;
      }
      if (tr) s += (r - l + 1);
    }
  }
};
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.setf(ios::fixed);
  cout.precision(0);
  suff_auto de;
  string s;
  cin >> s;
  for (int i = 0; i < s.size(); i++) de.upd(s[i]);
  de.upd('#');
  int y;
  cin >> y;
  for (int i = 0; i < y; i++) {
    cin >> s;
    for (int j = 0; j < s.size(); j++) de.upd(s[j]);
    de.upd(char('0' + i));
    int a, b;
    cin >> a >> b;
    df[i] = {a, b};
  }
  de.us.resize(de.ms.size());
  long long ans = 0;
  de.dfs(0, y, ans);
  cout << ans;
}
