#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int ch[N][26], fail[N], q[N], n, Q, R[N], len[N], K, cnt, dfn[N], en[N], e[N],
    l, r, k, rt, dfsc, bel[N];
char s[N];
vector<int> G[N];
long long a[N], b[N], c[N], ans[N], w[N];
struct node {
  int a, b, id;
};
vector<node> f[N], g[N];
void insert(int p, char *s) {
  int o = rt;
  for (char *i = s; *i; ++i) {
    int x = *i - 97;
    if (!ch[o][x]) ch[o][x] = ++cnt;
    o = ch[o][x];
  }
  e[p] = o;
}
void construct() {
  int *s = q, *t = q;
  for (int i = (0); i <= (25); ++i)
    ch[rt][i] ? fail[(*s++) = ch[rt][i]] = rt : ch[rt][i] = rt;
  while (s != t) {
    int u = (*t++);
    for (int i = (0); i <= (25); ++i)
      ch[u][i] ? fail[(*s++) = ch[u][i]] = ch[fail[u]][i]
               : ch[u][i] = ch[fail[u]][i];
  }
  for (int i = (2); i <= (cnt); ++i) G[fail[i]].push_back(i);
}
void DFS(int u) {
  for (int &v : G[u]) DFS(v), a[u] += a[v];
}
void dfs(int u) {
  dfn[u] = en[u] = ++dfsc;
  for (int &v : G[u]) dfs(v), en[u] = en[v];
}
long long sum(int i) { return b[bel[i] - 1] + c[i]; }
void add(int i, int x) {
  int t = bel[i], R = t * K;
  for (int j = (t); j <= (bel[cnt]); ++j) b[j] += x;
  for (int j = (i); j <= (R); ++j) c[j] += x;
}
int main() {
  scanf("%d%d", &n, &Q);
  int lst = 1;
  cnt = rt = 1;
  for (int i = (1); i <= (n); ++i) {
    scanf("%s", s + lst);
    len[i] = strlen(s + lst);
    insert(i, s + lst);
    lst += len[i];
    R[i] = lst - 1;
  }
  int o = (int)sqrt(lst + 0.5);
  construct();
  K = (int)sqrt(cnt + 0.5);
  for (int i = (1); i <= (cnt + 1); ++i) bel[i] = (i + K - 1) / K;
  for (int i = (1); i <= (Q); ++i) {
    scanf("%d%d%d", &l, &r, &k);
    if (len[k] > o)
      f[k].push_back((node){l - 1, r, i});
    else
      g[l - 1].push_back((node){k, -1, i}), g[r].push_back((node){k, 1, i});
  }
  for (int i = (1); i <= (n); ++i)
    if (len[i] > o) {
      for (int j = (1); j <= (cnt); ++j) a[j] = 0;
      int o = rt;
      for (int j = (R[i - 1] + 1); j <= (R[i]); ++j) {
        o = ch[o][s[j] - 'a'];
        a[o]++;
      }
      DFS(rt);
      for (int j = (1); j <= (n); ++j) w[j] = w[j - 1] + a[e[j]];
      for (auto &x : f[i]) ans[x.id] = w[x.b] - w[x.a];
    }
  dfs(rt);
  for (int i = (1); i <= (n); ++i) {
    add(dfn[e[i]], 1);
    add(en[e[i]] + 1, -1);
    for (auto &x : g[i]) {
      int c = x.a, o = rt;
      for (int j = (R[c - 1] + 1); j <= (R[c]); ++j) {
        o = ch[o][s[j] - 'a'];
        ans[x.id] += x.b * sum(dfn[o]);
      }
    }
  }
  for (int i = (1); i <= (Q); ++i) printf("%lld\n", ans[i]);
  return 0;
}
