#include <bits/stdc++.h>
using namespace std;
int bg[100001], ed[100001], A[100011], Tm[100001], n, Q, i, j, k, l, len, S, x,
    y;
bool bz[100001];
long long ans[100001], Ans[100001];
char st[100001], St[100001];
struct type {
  int x, y, z;
} q[100001];
namespace tree {
int a[100011][2], b[50000001], ls[100011], tr[100011][26], fa[100011],
    d[100011], Ed[100011], i, j, k, l, len = 0, h, t, Len;
vector<int> c[100011];
long long f[100011];
void clear() {
  memset(tr, 0, sizeof(tr[0]) * (len + 1));
  memset(fa, 0, 4 * (len + 1));
  memset(f, 0, 8 * (len + 1));
  len = 0;
}
void New(int t, int x) {
  if (!tr[t][x]) tr[t][x] = ++len;
}
void NEW(int x, int y) {
  ++Len;
  a[Len][0] = y;
  a[Len][1] = ls[x];
  ls[x] = Len;
}
void dfs(int t) {
  int i, j, k, l, tot2 = c[t].size(), s;
  for (i = Ed[t - 1] + 1; i <= Ed[t]; i++) s = b[i], ans[s] -= f[q[s].z];
  for (i = 0; i <= tot2 - 1; i++) ++f[c[t][i]];
  for (i = ls[t]; i; i = a[i][1]) dfs(a[i][0]);
  for (i = Ed[t - 1] + 1; i <= Ed[t]; i++) s = b[i], ans[s] += f[q[s].z];
}
void build(int x, int y, int tp) {
  clear(), len = 1;
  for (i = x; i <= y; i++) {
    l = 1;
    for (j = bg[i]; j <= ed[i]; j++) {
      New(l, st[j] - 'a'), l = tr[l][st[j] - 'a'];
      if (tp) c[l].push_back(i);
    }
    if (!tp) ++f[l];
    A[i] = l;
  }
  if (tp) {
    for (i = 1; i <= Q; i++) {
      for (j = 1; j <= n; j += S) {
        k = (j + S - 1 < n ? j + S - 1 : n);
        if (!(q[i].x <= j && k <= q[i].y) || !bz[i])
          for (l = (q[i].x > j ? q[i].x : j); l <= (q[i].y < k ? q[i].y : k);
               l++)
            ++Ed[A[l]];
      }
    }
    for (i = 1; i <= len; i++) Ed[i] += Ed[i - 1];
    for (i = len + 1; i >= 1; i--) Ed[i] = Ed[i - 1];
    for (i = 1; i <= Q; i++) {
      for (j = 1; j <= n; j += S) {
        k = (j + S - 1 < n ? j + S - 1 : n);
        if (!(q[i].x <= j && k <= q[i].y) || !bz[i]) {
          for (l = (q[i].x > j ? q[i].x : j); l <= (q[i].y < k ? q[i].y : k);
               l++)
            b[++Ed[A[l]]] = i;
        }
      }
    }
  }
  h = 0, t = 1;
  d[1] = 1;
  while (h < t) {
    ++h;
    for (i = 0; i <= 25; i++)
      if (tr[d[h]][i]) {
        l = tr[d[h]][i];
        d[++t] = l;
        j = fa[d[h]];
        while (j && !tr[j][i]) j = fa[j];
        fa[l] = (!j) ? 1 : tr[j][i];
      }
  }
  if (!tp) {
    for (i = 1; i <= t; i++) f[d[i]] += f[fa[d[i]]];
  } else {
    for (i = 2; i <= len; i++) NEW(fa[i], i);
    dfs(1);
  }
}
void js(int t, int id) {
  long long s = 0;
  if (Tm[t] == x) {
    ans[id] += Ans[t];
    return;
  }
  l = 1;
  for (i = bg[t]; i <= ed[t]; i++) {
    while (l && !tr[l][st[i] - 'a']) l = fa[l];
    if (!l)
      l = 1;
    else
      l = tr[l][st[i] - 'a'];
    s += f[l];
  }
  Tm[t] = x, Ans[t] = s, ans[id] += s;
}
};  // namespace tree
int main() {
  scanf("%d%d", &n, &Q), S = floor(sqrt(n));
  for (i = 1; i <= n; i++) {
    scanf("%s", St + 1), l = strlen(St + 1);
    bg[i] = len + 1;
    for (j = 1; j <= l; j++) st[++len] = St[j];
    ed[i] = len;
  }
  for (i = 1; i <= Q; i++) scanf("%d%d%d", &q[i].x, &q[i].y, &q[i].z);
  for (x = 1; x <= n; x += S) {
    y = (x + S - 1 < n ? x + S - 1 : n);
    tree::build(x, y, 0);
    for (i = 1; i <= Q; i++)
      if (q[i].x <= x && y <= q[i].y) tree::js(q[i].z, i), bz[i] = 1;
  }
  tree::build(1, n, 1);
  for (i = 1; i <= Q; i++) printf("%lld\n", ans[i]);
  fclose(stdin);
  fclose(stdout);
  return 0;
}
