#include <bits/stdc++.h>
using namespace std;
struct trie {
  int so[28], fi;
} t[100010];
struct Info {
  int we, nu, x, fh;
} f[100010 * 2];
int ro, num, li[100010][2], n, q, b[100010], nf, x, y, z, be[100010][2],
    en[100010][2], gen, vi[100010], s1[100010][410], s2[410], s3[410],
    s4[100010];
long long an[100010];
string s[100010];
vector<int> a[100010][2];
int build(int &nu, int x, int le) {
  if (!nu) nu = ++num;
  if (le != s[x].length()) return build(t[nu].so[s[x][le] - 'a'], x, le + 1);
  return nu;
}
void dfs(int x, int fa, int nu) {
  li[++num][nu] = x;
  be[x][nu] = num;
  for (auto y : a[x][nu])
    if (y != fa) dfs(y, x, nu);
  en[x][nu] = num;
}
void jb(int x, int y, int z) {
  a[x][z].emplace_back(y);
  a[y][z].emplace_back(x);
}
void bfs() {
  int l = 1, r = 1, li[100010];
  li[l] = 1;
  while (l <= r) {
    for (int i = 0; i < 26; i++) {
      int x = t[li[l]].so[i];
      if (x) {
        jb(li[l], x, 0);
        if (l == 1)
          t[x].fi = 1;
        else {
          int y = t[li[l]].fi;
          while (y != 1 && t[y].so[i] == 0) y = t[y].fi;
          if (t[y].so[i])
            t[x].fi = t[y].so[i];
          else
            t[x].fi = 1;
        }
        jb(t[x].fi, x, 1);
        li[++r] = x;
      }
    }
    l++;
  }
}
bool c1(const Info &x, const Info &y) { return x.nu < y.nu; }
void get(int x, int fa, int nu, int z) {
  s1[x][z] = s1[fa][z] + vi[x];
  for (auto y : a[x][nu])
    if (y != fa) get(y, x, nu, z);
}
void add(int x, int y) {
  s4[x] += y;
  s3[(x - 1) / gen] += y;
}
int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    b[i] = build(ro, i, 0);
  }
  bfs();
  for (int i = 1; i <= q; i++) {
    cin >> x >> y >> z;
    if (x - 1) f[++nf] = (Info){i, x - 1, z, -1};
    f[++nf] = (Info){i, y, z, 1};
  }
  sort(f + 1, f + nf + 1, c1);
  num = 0;
  dfs(1, 0, 0);
  num = 0;
  dfs(1, 0, 1);
  gen = sqrt(num);
  for (int i = 1; i <= num; i++) vi[i] = 0;
  for (int l = 1, r = 0; l <= num; l = r + 1) {
    r = min(r + gen, num);
    for (int i = l; i <= r; i++) vi[li[i][1]] = 1;
    get(1, 0, 0, (r - 1) / gen);
    for (int i = l; i <= r; i++) vi[li[i][1]] = 0;
  }
  for (int i = 1, j = 0; i <= n; i++) {
    int l1 = be[b[i]][1], r1 = en[b[i]][1];
    for (int l = 1, r = 0; l <= num; l = r + 1) {
      r = min(r + gen, num);
      if (l1 <= l && r <= r1)
        s2[(r - 1) / gen]++;
      else {
        for (int k = max(l, l1); k <= min(r, r1); k++) {
          add(be[li[k][1]][0], 1);
          if (en[li[k][1]][0] < num) add(en[li[k][1]][0] + 1, -1);
        }
      }
    }
    while (j + 1 <= nf && f[j + 1].nu <= i) {
      j++;
      for (int k = 0; k <= (num - 1) / gen; k++)
        an[f[j].we] += 1ll * f[j].fh * s2[k] * s1[b[f[j].x]][k];
      r1 = be[b[f[j].x]][0];
      for (int l = 1, r = 0; l <= num; l = r + 1) {
        r = min(r + gen, num);
        if (r <= r1)
          an[f[j].we] += 1ll * f[j].fh * s3[(r - 1) / gen];
        else {
          for (int k = l; k <= min(r, r1); k++)
            an[f[j].we] += 1ll * f[j].fh * s4[k];
        }
      }
    }
  }
  for (int i = 1; i <= q; i++) cout << an[i] << '\n';
  return 0;
}
