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;
char ss[611111];
int s[611111];
pair<pair<int, int>, int> p[611111];
int lc[21][611111];
int gg[611111];
int c[21][611111];
int su[611111];
int it, n, q;
int nn = 0;
int len[611111], st[611111];
int ans[611111], rev[611111];
int lcp(int x, int y) {
  int r = 0;
  for (int i = it; i >= 0; i--)
    if (x + (1ll << (i)) <= nn && y + (1ll << (i)) <= nn &&
        c[i][x] == c[i][y]) {
      x += (1ll << (i));
      y += (1ll << (i));
      r += (1ll << (i));
    }
  return r;
}
int flc(int x, int y) {
  if (x > y) return 1e9;
  int w = gg[y - x + 1];
  return min(lc[w][x], lc[w][y - (1ll << (w)) + 1]);
}
void add(int x) {
  x++;
  while (x <= n) {
    su[x]++;
    x = x + x - (x & (x - 1));
  }
}
int fin(int x) {
  int r = 0;
  x++;
  while (x > 0) {
    r += su[x];
    x &= x - 1;
  }
  return r;
}
int main() {
  scanf("%d%d\n", &n, &q);
  for (int i = 0; i < n; i++) {
    gets(ss);
    int k = strlen(ss);
    len[i] = k;
    st[i] = nn;
    for (int j = 0; j < k; j++) s[nn++] = ss[j] - 'a';
    s[nn++] = 27 + i;
  }
  for (int i = 0; i < nn; i++) p[i] = make_pair(make_pair(s[i], 0), i);
  sort(p, p + nn);
  int e = 0;
  for (int i = 0; i < nn; i++) {
    if (i > 0 && p[i].first != p[i - 1].first) e++;
    c[0][p[i].second] = e;
  }
  it = 0;
  while ((1ll << (it)) < nn) {
    it++;
    for (int i = 0; i < nn; i++)
      if (i + (1ll << (it - 1)) < nn)
        p[i] = make_pair(
            make_pair(c[it - 1][i], c[it - 1][i + (1ll << (it - 1))]), i);
      else
        p[i] = make_pair(make_pair(c[it - 1][i], -1), i);
    sort(p, p + nn);
    e = 0;
    for (int i = 0; i < nn; i++) {
      if (i > 0 && p[i].first != p[i - 1].first) e++;
      c[it][p[i].second] = e;
    }
  }
  gg[1] = 0;
  for (int i = 2; i <= nn; i++) gg[i] = gg[i / 2] + 1;
  for (int i = 0; i < nn - 1; i++) lc[0][i] = lcp(p[i].second, p[i + 1].second);
  for (int i = 1; i <= gg[nn - 1]; i++)
    for (int j = 0; j < nn - 1; j++)
      if (j + (1ll << (i)) <= nn - 1)
        lc[i][j] = min(lc[i - 1][j], lc[i - 1][j + (1ll << (i - 1))]);
  for (int i = 0; i < nn; i++) rev[p[i].second] = i;
  vector<pair<pair<int, int>, pair<int, int> > > ev;
  for (int i = 0; i < q; i++) {
    int l, r, k;
    scanf("%d%d%d", &l, &r, &k);
    l--;
    r--;
    k--;
    int ce = rev[st[k]];
    int q1 = ce, q2 = ce;
    int le = 0;
    int ri = ce;
    while (le < ri) {
      int mid = (le + ri) / 2;
      if (flc(mid, ce - 1) < len[k])
        le = mid + 1;
      else
        ri = mid;
    }
    q1 = le;
    le = ce;
    ri = nn - 1;
    while (le < ri) {
      int mid = (le + ri + 1) / 2;
      if (flc(ce, mid - 1) < len[k])
        ri = mid - 1;
      else
        le = mid;
    }
    q2 = le;
    ans[i] = 0;
    ev.push_back(make_pair(make_pair(q2, 2), make_pair(r, i)));
    ev.push_back(make_pair(make_pair(q2, 1), make_pair(l - 1, i)));
    ev.push_back(make_pair(make_pair(q1 - 1, 1), make_pair(r, i)));
    ev.push_back(make_pair(make_pair(q1 - 1, 2), make_pair(l - 1, i)));
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < len[i]; j++)
      ev.push_back(make_pair(make_pair(rev[st[i] + j], 0), make_pair(i, -1)));
  sort(ev.begin(), ev.end());
  for (int i = 0; i < ev.size(); i++) {
    if (ev[i].first.second == 0) {
      add(ev[i].second.first);
    } else {
      int coef = 1;
      if (ev[i].first.second == 1) coef = -1;
      ans[ev[i].second.second] += coef * fin(ev[i].second.first);
    }
  }
  for (int i = 0; i < q; i++) printf("%d\n", ans[i]);
  return 0;
}