#include <bits/stdc++.h>
using namespace std;
int m, last;
int nxt[400400][26], link[400400], dp[400400], pos[400400];
void extend(int c) {
  if (nxt[last][c] && dp[nxt[last][c]] == dp[last] + 1) {
    last = nxt[last][c];
    return;
  }
  int x = last, y = last = ++m;
  dp[y] = dp[x] + 1;
  while (x && !nxt[x][c]) nxt[x][c] = y, x = link[x];
  if (!x)
    link[y] = 1;
  else {
    int z = nxt[x][c];
    if (dp[z] == dp[x] + 1)
      link[y] = z;
    else {
      int u = ++m;
      memcpy(nxt[u], nxt[z], sizeof nxt[u]);
      link[u] = link[z], link[z] = link[y] = u;
      dp[u] = dp[x] + 1;
      while (x && nxt[x][c] == z) nxt[x][c] = u, x = link[x];
    }
  }
}
int n, q, ans[1001001];
string s[400400];
vector<int> v[400400];
int T, dt[400400], ft[400400];
vector<int> w[400400];
void dfs(int x) {
  dt[x] = ++T;
  for (int i = 0; i < v[x].size(); i++) dfs(v[x][i]);
  ft[x] = T;
}
struct query {
  int l, r, p, id;
  query(int l = 0, int r = 0, int p = 0, int id = 0)
      : l(l), r(r), p(p), id(id) {}
  bool operator<(const query &b) const { return p < b.p; }
} Q[1001001];
int sum[400400];
void add(int x) {
  while (x <= m) sum[x]++, x += x & -x;
}
int calc(int x) {
  int rlt = 0;
  while (x) rlt += sum[x], x -= x & -x;
  return rlt;
}
int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++) cin >> s[i];
  last = ++m;
  for (int i = 1; i <= n; i++) {
    last = 1;
    for (int j = 0; j < s[i].size(); j++) extend(s[i][j] - 'a');
  }
  for (int x = 1; x <= m; x++)
    if (link[x]) v[link[x]].push_back(x);
  dfs(1);
  for (int i = 1; i <= n; i++) {
    int x = 1;
    for (int j = 0; j < s[i].size(); j++)
      x = nxt[x][s[i][j] - 'a'], w[i].push_back(dt[x]);
    pos[i] = x;
  }
  for (int i = 1; i <= q; i++) {
    int l, r, k;
    scanf("%d %d %d", &l, &r, &k);
    k = pos[k];
    Q[i * 2 - 1] = query(dt[k], ft[k], r, i);
    Q[i * 2] = query(dt[k], ft[k], l - 1, -i);
  }
  sort(Q + 1, Q + 2 * q + 1);
  for (int i = 1, j = 1; i <= 2 * q; i++) {
    while (j <= Q[i].p) {
      for (int k = 0; k < w[j].size(); k++) add(w[j][k]);
      j++;
    }
    int tmp = calc(Q[i].r) - calc(Q[i].l - 1);
    if (Q[i].id > 0)
      ans[Q[i].id] += tmp;
    else
      ans[-Q[i].id] -= tmp;
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}
