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.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;


public class cf316g {
  static int n;
  static int[] min, max;
  public static void main(String[] args) {
    FastIO in = new FastIO(), out = in;
    String s = in.next().trim();
    n = in.nextInt();
    SuffixAutomaton sa = new SuffixAutomaton();
    sa.append(s,0);
    sa.append((char)0,-1);
    min = new int[n+1];
    max = new int[n+1];
    min[0] = 1; max[0] = 987654321;
    for(int i=1; i<=n; i++) {
      sa.append((char)i,-1);
      sa.append(in.next().trim(),i);
      min[i] = in.nextInt();
      max[i] = in.nextInt();
    }
    sa.aggregate();
    out.println(sa.calc());
    out.close();
  }
  static class SuffixAutomaton {
    int size, last;
    ArrayList<int[]> count;
    List<Integer> len, link, first;
    List<Map<Character,Integer>> next;
    SuffixAutomaton() {
      len = new ArrayList<Integer>();
      link = new ArrayList<Integer>();
      first = new ArrayList<Integer>();
      count = new ArrayList<int[]>();
      next = new ArrayList<Map<Character,Integer>>();
      grow(0,-1);
    }
    long calc() {
      long ret = 0;
      for(int i=1; i<size; i++) {
        boolean ok = true;
        for(int j=0; j<=n; j++)
          if(min[j] > count.get(i)[j] ||
             max[j] < count.get(i)[j])
            ok = false;
        if(ok)
          ret += len.get(i) - len.get(link.get(i));
      }
      return ret;
    }
    void aggregate() {
      int[] deg = new int[size];
      for(int i=0; i<size; i++)
        if(link.get(i) != -1)
          deg[link.get(i)]++;
      ArrayDeque<Integer> q = new ArrayDeque<Integer>();
      for(int i=0; i<size; i++)
        if(deg[i] == 0)
          q.add(i);
      while(!q.isEmpty()) {
        int cur = q.poll();
        if(link.get(cur) != -1) {
          for(int i=0; i<=n; i++) 
            count.get(link.get(cur))[i] += count.get(cur)[i];
          deg[link.get(cur)]--;
          if(deg[link.get(cur)] == 0)
            q.add(link.get(cur));
        }
      }
    }
    void append(String s, int color) {
      for(char c : s.toCharArray())
        append(c, color);
    }
    void append(char c, int color) {
      int cur = grow(len.get(last)+1, 0), p;
      if(color != -1)
        count.get(cur)[color] = 1;
      for(p=last; p!=-1 && !next.get(p).containsKey(c); p=link.get(p))
        next.get(p).put(c, cur);
      if(p!=-1) {
        int q = next.get(p).get(c);
        if(len.get(p)+1==len.get(q))
          link.set(cur, q);
        else {
          int clone = grow(len.get(p)+1,link.get(q));
          next.get(clone).putAll(next.get(q));
          for(; p != -1 && next.get(p).get(c)==q; p = link.get(p))
            next.get(p).put(c, clone);
          link.set(q,clone); link.set(cur,clone);
        }
      }
      last = cur;
    }
    int grow(int len_, int link_) {
      len.add(len_);
      link.add(link_);
      next.add(new TreeMap<Character,Integer>());
      count.add(new int[n+1]);
      return size++;
    }
  }
  static class FastIO extends PrintWriter {
    BufferedReader br;
    StringTokenizer st;
    
    public FastIO() {
      this(System.in,System.out);
    }
    public FastIO(InputStream in, OutputStream out) {
      super(new BufferedWriter(new OutputStreamWriter(out)));
      br = new BufferedReader(new InputStreamReader(in));
      scanLine();
    }
    public void scanLine() {
      try {
        st = new StringTokenizer(br.readLine().trim());
      } catch(Exception e) {
        throw new RuntimeException(e.getMessage());
      }
    }
    public int numTokens() {
      if(!st.hasMoreTokens()) {
        scanLine();
        return numTokens();
      }
      return st.countTokens();
    }
    public String next() {
      if(!st.hasMoreTokens()) {
        scanLine();
        return next();
      }
      return st.nextToken();
    }
    public double nextDouble() {
      return Double.parseDouble(next());
    }
    public long nextLong() {
      return Long.parseLong(next());
    }
    public int nextInt() {
      return Integer.parseInt(next());
    }
  }
}
