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.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * 316G
 * O(n*|s|^2 + Σ(|p_i|)) time
 * θ(|s| + Σ(|p_i|)) space
 *
 * @author artyom
 */
public class _316G implements Runnable {
    private BufferedReader in;
    private StringTokenizer tok;

    private Object solve() throws IOException {
        String s = nextToken();
        int n = nextInt();
        String[] p = new String[n];
        SuffixTreeNode[] pRoots = new SuffixTreeNode[n];
        int[] l = new int[n], r = new int[n];
        for (int i = 0; i < n; i++) {
            p[i] = nextToken() + '{';
            pRoots[i] = buildSuffixTree(p[i]);
            preprocessP(pRoots[i]);
            l[i] = nextInt();
            r[i] = nextInt();
        }
        int cnt = 0, m = s.length();
        SuffixTreeNode sRoot = buildSuffixTree(s + '{');
        preprocessS(sRoot);
        sRoot.pPositions = pRoots;
        Deque<SuffixTreeNode> stack = new LinkedList<>();
        for (SuffixTreeNode child : sRoot.activeChildren) {
            stack.push(child);
        }
        out:
        while (!stack.isEmpty()) {
            SuffixTreeNode node = stack.pop();
            SuffixTreeNode[] cur = Arrays.copyOf(node.parent.pPositions, n);
            for (int len = node.parent.depth(), j = node.pos + len, lim = node.isLeaf() ? m : j + node.length(); j < lim; len++, j++) {
                int sat = 0;
                for (int i = 0; i < n; i++) {
                    if (cur[i] != null) {
                        if (len == cur[i].depth()) {
                            cur[i] = cur[i].children[s.charAt(j) - 'a'];
                        } else if (p[i].charAt(cur[i].pos + len) != s.charAt(j)) {
                            cur[i] = null;
                        }
                        if (cur[i] == null) {
                            if (l[i] == 0) {
                                sat++;
                            } else {
                                continue out;
                            }
                        } else {
                            if (cur[i].leaves < l[i]) {
                                continue out;
                            } else if (cur[i].leaves <= r[i]) {
                                sat++;
                            }
                        }
                    } else if (l[i] == 0) {
                        sat++;
                    }
                }
                if (sat == n) {
                    cnt++;
                }
            }
            if (!node.isLeaf()) {
                node.pPositions = cur;
                stack.addAll(node.activeChildren);
            }
        }
        return cnt;
    }

    private static void preprocessS(SuffixTreeNode node) {
        if (node.isLeaf()) {
            node.pos = node.position;
        } else {
            node.activeChildren = new LinkedList<>();
            for (SuffixTreeNode child : node.children) {
                if (child != null) {
                    preprocessS(child);
                    node.pos = child.pos;
                    node.activeChildren.add(child);
                }
            }
        }
    }

    private static void preprocessP(SuffixTreeNode node) {
        if (node.isLeaf()) {
            node.leaves = 1;
            node.pos = node.position;
        } else {
            for (SuffixTreeNode child : node.children) {
                if (child != null) {
                    preprocessP(child);
                    node.leaves += child.leaves;
                    node.pos = child.pos;
                }
            }
        }
    }

    //--------------------------------------------------------------
    public static void main(String[] args) {
        new _316G().run();
    }

    @Override
    public void run() {
        try {
            in = new BufferedReader(new InputStreamReader(System.in));
            tok = null;
            System.out.print(solve());
            in.close();
        } catch (IOException e) {
            System.exit(0);
        }
    }

    private String nextToken() throws IOException {
        while (tok == null || !tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    private int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    private static SuffixTreeNode buildSuffixTree(CharSequence s) {
        int n = s.length();
        byte[] a = new byte[n];
        for (int i = 0; i < n; i++) {
            a[i] = (byte) (s.charAt(i) - 'a');
        }
        SuffixTreeNode root = new SuffixTreeNode(0, 0, 0, null, null);
        SuffixTreeNode node = root;
        for (int i = 0, tail = 0; i < n; i++, tail++) {
            SuffixTreeNode last = null;
            while (tail >= 0) {
                SuffixTreeNode ch = node.children[a[i - tail]];
                while (ch != null && tail >= ch.length()) {
                    tail -= ch.length();
                    node = ch;
                    ch = ch.children[a[i - tail]];
                }
                if (ch == null) {
                    node.children[a[i]] = new SuffixTreeNode(i, n, node.depth(), i, node);
                    if (last != null) {
                        last.suffixLink = node;
                    }
                    last = null;
                } else {
                    byte t = a[(int) (ch.start + tail)];
                    if (t == a[i]) {
                        if (last != null) {
                            last.suffixLink = node;
                        }
                        break;
                    } else {
                        SuffixTreeNode splitNode = new SuffixTreeNode(ch.start, ch.start + tail, node.depth(), null, node);
                        splitNode.children[a[i]] = new SuffixTreeNode(i, n, ch.depth + tail, i, splitNode);
                        splitNode.children[t] = ch;
                        ch.start += tail;
                        ch.depth += tail;
                        ch.parent = splitNode;
                        node.children[a[i - tail]] = splitNode;
                        if (last != null) {
                            last.suffixLink = splitNode;
                        }
                        last = splitNode;
                    }
                }
                if (node == root) {
                    tail--;
                } else {
                    node = node.suffixLink;
                }
            }
        }
        return root;
    }

    private static final class SuffixTreeNode {
        int depth; // The number of characters from root to this node
        int start; // Inclusive
        int end; // Exclusive
        Integer position;
        SuffixTreeNode parent;
        final SuffixTreeNode[] children;
        SuffixTreeNode suffixLink;
        // Additional properties
        long leaves;
        int pos;
        SuffixTreeNode[] pPositions;
        Collection<SuffixTreeNode> activeChildren;

        private SuffixTreeNode(int start, int end, int depth, Integer position, SuffixTreeNode parent) {
            this.start = start;
            this.end = end;
            this.parent = parent;
            this.depth = depth;
            if (position == null) {
                this.position = null;
                this.children = new SuffixTreeNode[27];
            } else {
                this.position = position - depth;
                // Save some space and time
                this.children = null;
            }
        }

        boolean isLeaf() {
            return position != null;
        }

        /**
         * @return the number of characters on the edge to the parent.
         */
        final int length() {
            return end - start;
        }

        /**
         * @return the number of characters on the path from the root to this node.
         */
        final int depth() {
            return depth + end - start;
        }

        @Override
        public String toString() {
            return "(" + start + ',' + (end - 1) + ')';
        }
    }
}