Problem: What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercase English letters. What is double strange that a phone number can be associated with several bears!

In that country there is a rock band called CF consisting of n bears (including Mike) numbered from 1 to n. 

<image>

Phone number of i-th member of CF is si. May 17th is a holiday named Phone Calls day. In the last Phone Calls day, everyone called all the numbers that are substrings of his/her number (one may call some number several times). In particular, everyone called himself (that was really strange country).

Denote as call(i, j) the number of times that i-th member of CF called the j-th member of CF. 

The geek Mike has q questions that he wants to ask you. In each question he gives you numbers l, r and k and you should tell him the number 

<image>

Input

The first line of input contains integers n and q (1 ≤ n ≤ 2 × 105 and 1 ≤ q ≤ 5 × 105).

The next n lines contain the phone numbers, i-th line contains a string si consisting of lowercase English letters (<image>).

The next q lines contain the information about the questions, each of them contains integers l, r and k (1 ≤ l ≤ r ≤ n and 1 ≤ k ≤ n).

Output

Print the answer for each question in a separate line.

Examples

Input

5 5
a
ab
abab
ababab
b
1 5 1
3 5 1
1 5 2
1 5 3
1 4 5


Output

7
5
6
3
6

Solution (in JAVA):

Solution:
import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class MainG
{

    static int mod = (int) (1e9+7);
    static InputReader in;
    static PrintWriter out;
    
    static int[] suffixArray(String S)
    {
        int n = S.length();
        Integer[] order = new Integer[n];
        for (int i = 0; i < n; i++)
                order[i] = n - 1 - i;
        Arrays.sort(order, (a, b) -> Character.compare(S.charAt(a), S.charAt(b)));
        int[] sa = new int[n];
        int[] classes = new int[n];
        for (int i = 0; i < n; i++)
        {
            sa[i] = order[i];
            classes[i] = S.charAt(i);
        }
        for (int len = 1; len < n; len <<= 1)
        {
            int[] c = classes.clone();
            for (int i = 0; i < n; i++)
                classes[sa[i]] = i > 0 && c[sa[i - 1]] == c[sa[i]] && sa[i - 1] + len < n
                                    && c[sa[i - 1] + (len >> 1)] == c[sa[i] + (len >> 1)] ? classes[sa[i - 1]] : i;
            int[] cnt = new int[n];
            for (int i = 0; i < n; i++)
                cnt[i] = i;
            int[] s = sa.clone();
            for (int i = 0; i < n; i++)
            {
                int s1 = s[i] - len;
                if (s1 >= 0)
                    sa[cnt[classes[s1]]++] = s1;
            }
        }
        return sa;
    }

    static int[] lcp(int[] sa, String s)
    {
        int n = sa.length;
        int[] rank = new int[n];
        for (int i = 0; i < n; i++)
            rank[sa[i]] = i;
        int[] lcp = new int[n - 1];
        
        for (int i = 0, h = 0; i < n; i++)
        {
            if (rank[i] < n - 1)
            {
                for (int j = sa[rank[i] + 1]; Math.max(i, j) + h < s.length()
                                && s.charAt(i + h) == s.charAt(j + h); ++h);
                lcp[rank[i]] = h;
                if (h > 0)
                    --h;
            }
        }
        return lcp;
    }   
    
    
    static int[] logTable;
    static int[][] rmq;

    static void sparseTable(int[] a)
    {
        int n = a.length;

        logTable = new int[n + 1];
        for (int i = 2; i <= n; i++)
            logTable[i] = logTable[i >> 1] + 1;

        rmq = new int[logTable[n] + 1][n];

        for (int i = 0; i < n; ++i)
            rmq[0][i] = i;
      
        for (int k = 1; (1 << k) < n; ++k) {
            for (int i = 0; i + (1 << k) <= n; i++) {
                int x = rmq[k - 1][i];
                int y = rmq[k - 1][i + (1 << k - 1)];
                rmq[k][i] = a[x] <= a[y] ? x : y;
            }
        }
    }

    static int get(int i, int j, int[] a)
    {
        int k = logTable[j - i];
        int x = rmq[k][i];
        int y = rmq[k][j - (1 << k) + 1];
        return a[x] <= a[y] ? x : y;
    }

    static void update(int i, int val, int[] bit) {
        for (i++; i < bit.length; i += (i & -i)) {
            bit[i] += val;
        }
    }

    static int query(int i, int[] bit) {
        int ans = 0;

        for (i++; i > 0; i -= (i & -i)) {
            ans += bit[i];
        }

        return ans;
    }

    static int getAns(int l, int r, int[] bit) {
        if (l > r) {
            return 0;
        }
        return query(r, bit) - query(l - 1, bit);
    }

    static void solve() 
    {
        
        in = new InputReader(System.in);
        out = new PrintWriter(System.out); 
        
        int n = in.nextInt();
        int q = in.nextInt();
        String[] str = new String[n];
        StringBuilder all = new StringBuilder();
        int[] start = new int[n];
        
        for(int i = 0; i < n; i++) {
            str[i] = in.readString();
            start[i] = all.length();
            all.append(str[i]);
            if(i != n - 1)
                all.append("#");
        }
        
        String s = all.toString();
        int[] sa = suffixArray(s);
        int[] lcp = lcp(sa, s);
        int[] l = new int[q];
        int[] r = new int[q];
        int[] k = new int[q];
        
        int m = s.length();
        int[] idx = new int[m];
        int[] pos = new int[m];
        ArrayList<Pair>[] list = new ArrayList[m];
        
        for(int i = 0; i < m; i++) {
            idx[sa[i]] = i;
            list[i] = new ArrayList<>();
        }
        
        for(int i = 0, p = 0; i < n; i++) {
            for(int j = 0; j < str[i].length(); j++)
                pos[p++] = i;
            if(i != n - 1) pos[p++] = -1;
        }
        sparseTable(lcp);

        for(int i = 0; i < q; i++) {
            l[i] = in.nextInt() - 1;
            r[i] = in.nextInt() - 1;
            k[i] = in.nextInt() - 1;
            int ll = 0;
            int rr = idx[start[k[i]]] - 1;
            while(rr - ll >= 0) {
                int mid = (rr + ll) >> 1;
                if(lcp[get(mid, idx[start[k[i]]] - 1, lcp)] >= str[k[i]].length()) {
                    rr = mid - 1;
                }
                else ll = mid + 1;
            }
            if(rr >= 0)
                list[rr].add(new Pair(i, -1));
            ll = idx[start[k[i]]];
            rr = lcp.length - 1;
            while(rr - ll >= 0) {
                int mid = (rr + ll) >> 1;
                if(lcp[get(idx[start[k[i]]], mid, lcp)] >= str[k[i]].length()) {
                    ll = mid + 1;
                }
                else rr = mid - 1;
            }
            list[ll].add(new Pair(i, 1));
//            debug(idx[start[k[i]]], a[i], b[i]);
        }
        int[] ans = new int[q];
        int[] bit = new int[n + 3];
        
        for(int i = 0; i < m; i++) {
            if(pos[sa[i]] != -1)
                update(pos[sa[i]], 1, bit);
            for(Pair p : list[i]) {
                ans[p.x] += p.y * getAns(l[p.x], r[p.x], bit);
            }
        }
        
        for(int i : ans)
            out.println(i);
        
        out.close(); 
    }
    
    static long add(long a, long b) {
        return (a + b) % mod;
    }
    
    static long mul(long a, long b) {
        return (a * b) % mod;
    }
    
    static long sub(long a, long b) {
        return (a - b + mod) % mod;
    } 
    
    public static void main(String[] args)
    {
        new Thread(null ,new Runnable(){
            public void run()
            {
                try{
                    solve();
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
        },"1",1<<26).start();
    }
    
    static class Pair implements Comparable<Pair>
    {

        int x,y;
        int i;
        
        Pair (int x, int y)
        {
                this.x = x;
                this.y = y;
        }

        public int compareTo(Pair o)
        {
            return -Integer.compare(this.x, o.x);
        }

        public boolean equals(Object o)
        {
            if (o instanceof Pair)
            {
                Pair p = (Pair)o;
                return p.x == x && p.y==y;
            }
            return false;
        }

        @Override
        public String toString()
        {
            return x + " "+ y + " "+i;
        }

        /*public int hashCode()
        {
            return new Long(x).hashCode() * 31 + new Long(y).hashCode();
        }*/

    } 

    
    static String rev(String s){
        StringBuilder sb=new StringBuilder(s);
        sb.reverse();
        return sb.toString();
    }
    
    static long gcd(long x,long y)
    {
        if(y==0)
                return x;
        else
                return gcd(y,x%y);
    }

    static int gcd(int x,int y)
    {
        if(y==0)
                return x;
        else 
                return gcd(y,x%y);
    }

    static long pow(long n,long p,long m)
    {
         long  result = 1;
          if(p==0){
            return 1;
          }
          
        while(p!=0)
        {
            if(p%2==1)
                result *= n;
            if(result >= m)
               result %= m;
            p >>=1;
            n*=n;
            if(n >= m)
                n%=m;
        }
        
        return result;
    }

    static long pow(long n,long p)
    {
        long  result = 1;
          if(p==0)
            return 1;

        while(p!=0)
        {
            if(p%2==1)
                result *= n;	    
            p >>=1;
            n*=n;	    
        }
        return result;
    }

    static void debug(Object... o)
    {
            System.out.println(Arrays.deepToString(o));
    }

    static class InputReader
    {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream)
        {
                this.stream = stream;
        }

        public int snext()
        {
                if (snumChars == -1)
                        throw new InputMismatchException();
                if (curChar >= snumChars)
                {
                        curChar = 0;
                        try
                        {
                                snumChars = stream.read(buf);
                        } catch (IOException e)
                        {
                                throw new InputMismatchException();
                        }
                        if (snumChars <= 0)
                                return -1;
                }
                return buf[curChar++];
        }

        public int nextInt()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                int sgn = 1;
                if (c == '-')
                {
                        sgn = -1;
                        c = snext();
                }
                int res = 0;
                do
                {
                        if (c < '0' || c > '9')
                                throw new InputMismatchException();
                        res *= 10;
                        res += c - '0';
                        c = snext();
                } while (!isSpaceChar(c));
                return res * sgn;
        }

        public long nextLong()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                int sgn = 1;
                if (c == '-')
                {
                        sgn = -1;
                        c = snext();
                }
                long res = 0;
                do
                {
                        if (c < '0' || c > '9')
                                throw new InputMismatchException();
                        res *= 10;
                        res += c - '0';
                        c = snext();
                } while (!isSpaceChar(c));
                return res * sgn;
        }

        public int[] nextIntArray(int n)
        {
                int a[] = new int[n];
                for (int i = 0; i < n; i++)
                {
                        a[i] = nextInt();
                }
                return a;
        }

        public long[] nextLongArray(int n)
        {
                long a[] = new long[n];
                for (int i = 0; i < n; i++)
                {
                        a[i] = nextLong();
                }
                return a;
        }

        public String readString()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                StringBuilder res = new StringBuilder();
                do
                {
                        res.appendCodePoint(c);
                        c = snext();
                } while (!isSpaceChar(c));
                return res.toString();
        }

        public String nextLine()
        {
                int c = snext();
                while (isSpaceChar(c))
                        c = snext();
                StringBuilder res = new StringBuilder();
                do
                {
                        res.appendCodePoint(c);
                        c = snext();
                } while (!isEndOfLine(c));
                return res.toString();
        }

        public boolean isSpaceChar(int c)
        {
                if (filter != null)
                        return filter.isSpaceChar(c);
                return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c)
        {
                return c == '\n' || c == '\r' || c == -1;
        }

        public interface SpaceCharFilter
        {
                public boolean isSpaceChar(int ch);
        }

    }
}