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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T in() {
  char ch;
  T n = 0;
  bool ng = false;
  while (1) {
    ch = getchar();
    if (ch == '-') {
      ng = true;
      ch = getchar();
      break;
    }
    if (ch >= '0' && ch <= '9') break;
  }
  while (1) {
    n = n * 10 + (ch - '0');
    ch = getchar();
    if (ch < '0' || ch > '9') break;
  }
  return (ng ? -n : n);
}
template <typename T>
inline T POW(T B, T P) {
  if (P == 0) return 1;
  if (P & 1)
    return B * POW(B, P - 1);
  else
    return (POW(B, P / 2) * POW(B, P / 2));
}
template <typename T>
inline T Gcd(T a, T b) {
  if (a < 0) return Gcd(-a, b);
  if (b < 0) return Gcd(a, -b);
  return (b == 0) ? a : Gcd(b, a % b);
}
template <typename T>
inline T Lcm(T a, T b) {
  if (a < 0) return Lcm(-a, b);
  if (b < 0) return Lcm(a, -b);
  return a * (b / Gcd(a, b));
}
long long Bigmod(long long base, long long power, long long MOD) {
  long long ret = 1;
  while (power) {
    if (power & 1) ret = (ret * base) % MOD;
    base = (base * base) % MOD;
    power >>= 1;
  }
  return ret;
}
bool isVowel(char ch) {
  ch = toupper(ch);
  if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
    return true;
  return false;
}
long long ModInverse(long long number, long long MOD) {
  return Bigmod(number, MOD - 2, MOD);
}
bool isConst(char ch) {
  if (isalpha(ch) && !isVowel(ch)) return true;
  return false;
}
int toInt(string s) {
  int sm;
  stringstream second(s);
  second >> sm;
  return sm;
}
const int N = 500007;
int cmp(int *r, int a, int b, int l) {
  return (r[a] == r[b]) && (r[a + l] == r[b + l]);
}
int wa[N], wb[N], wws[N], wv[N];
int rnk[N], height[N];
void DA(int *r, int *sa, int n, int m) {
  int i, j, p, *x = wa, *y = wb, *t;
  for (i = 0; i < m; i++) wws[i] = 0;
  for (i = 0; i < n; i++) wws[x[i] = r[i]]++;
  for (i = 1; i < m; i++) wws[i] += wws[i - 1];
  for (i = n - 1; i >= 0; i--) sa[--wws[x[i]]] = i;
  for (j = 1, p = 1; p < n; j *= 2, m = p) {
    for (p = 0, i = n - j; i < n; i++) y[p++] = i;
    for (i = 0; i < n; i++)
      if (sa[i] >= j) y[p++] = sa[i] - j;
    for (i = 0; i < n; i++) wv[i] = x[y[i]];
    for (i = 0; i < m; i++) wws[i] = 0;
    for (i = 0; i < n; i++) wws[wv[i]]++;
    for (i = 1; i < m; i++) wws[i] += wws[i - 1];
    for (i = n - 1; i >= 0; i--) sa[--wws[wv[i]]] = y[i];
    for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
      x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
  }
}
void calheight(int *r, int *sa, int n) {
  int i, j, k = 0;
  for (i = 1; i <= n; i++) rnk[sa[i]] = i;
  for (i = 0; i < n; height[rnk[i++]] = k)
    for (k ? k-- : 0, j = sa[rnk[i] - 1]; r[i + k] == r[j + k]; k++)
      ;
}
int sa[N], data[N], n, p, q;
char str[N];
void Deal() {
  DA(data, sa, n + 1, 128);
  calheight(data, sa, n);
}
int dp[20][500007];
void Sparse() {
  for (int i = 0; i < n; i++) dp[0][i] = height[i + 1];
  for (int i = 1; (1 << i) <= n; i++) {
    for (int j = 0; j + (1 << i) - 1 < n; j++) {
      if (dp[i - 1][j] < dp[i - 1][j + (1 << (i - 1))])
        dp[i][j] = dp[i - 1][j];
      else
        dp[i][j] = dp[i - 1][j + (1 << (i - 1))];
    }
  }
}
int Query(int xx, int yy) {
  xx--, yy--;
  int diff = 31 - __builtin_clz(yy - xx + 1);
  return (dp[diff][xx] < dp[diff][yy - (1 << diff) + 1]
              ? dp[diff][xx]
              : dp[diff][yy - (1 << diff) + 1]);
}
struct Node {
  int l, r, sum;
  Node() {
    l = r = 0;
    sum = 0;
  }
};
int avail, Root[500007];
Node Tree[20 * 500007];
int v;
int Upd(int id, int l, int r, int pos) {
  int u = ++avail;
  Tree[u] = Tree[id];
  if (l == r) {
    Tree[u].sum += v;
    return u;
  }
  int mid = (l + r) / 2;
  if (pos <= mid)
    Tree[u].l = Upd(Tree[u].l, l, mid, pos);
  else
    Tree[u].r = Upd(Tree[u].r, mid + 1, r, pos);
  Tree[u].sum = Tree[Tree[u].l].sum + Tree[Tree[u].r].sum;
  return u;
}
int Qry(int idl, int idr, int l, int r, int st, int ed) {
  if (st <= l && ed >= r) return Tree[idl].sum - Tree[idr].sum;
  int mid = (l + r) / 2;
  if (ed <= mid)
    return Qry(Tree[idl].l, Tree[idr].l, l, mid, st, ed);
  else if (st > mid)
    return Qry(Tree[idl].r, Tree[idr].r, mid + 1, r, st, ed);
  else {
    return Qry(Tree[idl].l, Tree[idr].l, l, mid, st, ed) +
           Qry(Tree[idl].r, Tree[idr].r, mid + 1, r, st, ed);
  }
}
char P[500007];
int Ln[500007];
int Pos[500007];
pair<int, int> Nxt[500007];
int Id[500007];
int St[500007];
int main() {
  int p, q;
  p = in<int>(), q = in<int>();
  n = 0;
  for (int i = 1; i < p + 1; i++) {
    scanf("%s", P);
    int ln = strlen(P);
    Ln[i] = ln;
    Pos[n] = i;
    St[i] = n;
    for (int j = 0; j < ln; j++) {
      data[n] = (int)(P[j]);
      Id[n] = i;
      n++;
    }
    data[n++] = 65;
  }
  data[n] = 0;
  Deal();
  Sparse();
  Root[0] = 0;
  Tree[0] = Node();
  for (int i = n; i >= 1; i--) {
    int ps = sa[i];
    v = 0;
    if (Id[ps]) {
      v = 1;
    }
    Root[i] = Upd(Root[i + 1], 1, n, Id[ps]);
    if (Pos[ps] == 0) continue;
    ps = Pos[ps];
    int nw = i;
    int lo = i + 1, hi = n;
    while (lo <= hi) {
      int mid = (lo + hi) / 2;
      if (Query(i + 1, mid) >= Ln[ps]) {
        nw = mid;
        lo = mid + 1;
      } else
        hi = mid - 1;
    }
    int rg = i + 1;
    lo = 1, hi = i;
    while (lo <= hi) {
      int mid = (lo + hi) / 2;
      if (Query(mid, i) >= Ln[ps]) {
        rg = mid;
        hi = mid - 1;
      } else
        lo = mid + 1;
    }
    rg--;
    Nxt[i] = make_pair(rg, nw + 1);
  }
  while (q--) {
    int l, r, ps;
    l = in<int>(), r = in<int>(), ps = in<int>();
    int rk = rnk[St[ps]];
    int lx = Nxt[rk].first;
    int nx = Nxt[rk].second;
    printf("%d\n", Qry(Root[lx], Root[nx], 1, n, l, r));
  }
  return 0;
}