#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int nm = 0;
  bool fh = true;
  char cw = getchar();
  for (; !isdigit(cw); cw = getchar()) fh ^= (cw == '-');
  for (; isdigit(cw); cw = getchar()) nm = nm * 10 + (cw - '0');
  return fh ? nm : -nm;
}
int n, Q, ch[500200][26], que[500200], fail[500200], tot, ed[500200];
vector<int> pos[500200];
char str[500200];
long long ans[500200];
inline void build() {
  int hd = 1, tl = 0;
  for (int i = 0; i < 26; i++)
    if (ch[0][i]) que[++tl] = ch[0][i];
  while (hd <= tl) {
    int x = que[hd++];
    for (int i = 0; i < 26; i++)
      if (ch[x][i]) {
        int p = ch[x][i];
        fail[p] = ch[fail[x]][i], que[++tl] = p;
      } else
        ch[x][i] = ch[fail[x]][i];
  }
}
int to[500200], nt[500200], fs[500200], tmp;
int L[500200], R[500200], cnt;
inline void link(int x, int y) { to[++tmp] = y, nt[tmp] = fs[x], fs[x] = tmp; }
inline void dfs(int x) {
  L[x] = ++cnt;
  for (int i = fs[x]; i; i = nt[i]) dfs(to[i]);
  R[x] = cnt;
}
namespace BIT {
long long T[500200];
inline void add(int k) {
  for (; k <= cnt; k += (k & -k)) ++T[k];
}
inline long long qry(int l, int r) {
  long long res = 0ll;
  for (int k = r; k; k -= (k & -k)) res += T[k];
  for (int k = l - 1; k; k -= (k & -k)) res -= T[k];
  return res;
}
}  // namespace BIT
vector<pair<int, int> > vec[500200];
int main() {
  n = read(), Q = read();
  for (int i = 1; i <= n; i++) {
    scanf("%s", str + 1);
    int l = strlen(str + 1), p = 0;
    for (int j = 1; j <= l; j++) {
      int t = str[j] - 'a';
      if (!ch[p][t]) ch[p][t] = ++tot;
      p = ch[p][t], pos[i].push_back(p);
    }
    ed[i] = p;
  }
  build();
  for (int i = 1; i <= tot; i++) link(fail[i], i);
  dfs(0);
  for (int i = 1, l, r, k; i <= Q; i++) {
    l = read(), r = read(), k = read();
    vec[l - 1].push_back(make_pair(k, -i)), vec[r].push_back(make_pair(k, i));
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0, TP = pos[i].size(); j < TP; j++) BIT::add(L[pos[i][j]]);
    for (int j = 0, TP = vec[i].size(), X, Y; j < TP; j++)
      X = vec[i][j].first, Y = vec[i][j].second,
      (Y < 0) ? ans[-Y] -= BIT::qry(L[ed[X]], R[ed[X]])
              : ans[Y] += BIT::qry(L[ed[X]], R[ed[X]]);
  }
  for (int i = 1; i <= Q; i++) printf("%lld\n", ans[i]);
  return 0;
}
