#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, q, id[N], num;
int tr[N][26], fail[N], ed[N], fa[N], len[N], tot = 1, tr_siz[N];
long long ans[N], sum[N];
char s[N];
vector<pair<int, int> > Lq[N], Rq[N], lq[N], rq[N];
vector<int> fail_tr[N];
void dfs1(int x) {
  int siz = fail_tr[x].size();
  for (int i = 0; i < siz; i++)
    dfs1(fail_tr[x][i]), sum[x] += sum[fail_tr[x][i]];
  return;
}
void dfs2(int x) {
  tr_siz[x] = 1;
  id[x] = ++num;
  int siz = fail_tr[x].size();
  for (int i = 0; i < siz; i++)
    dfs2(fail_tr[x][i]), tr_siz[x] += tr_siz[fail_tr[x][i]];
  return;
}
long long trs[N];
inline void add(int x, int v) {
  while (x <= tot) {
    trs[x] += v;
    x += (x & -x);
  }
  return;
}
inline long long query(int x) {
  long long res = 0;
  while (x > 0) {
    res += trs[x];
    x -= (x & -x);
  }
  return res;
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    len[i] = strlen(s + 1);
    m += len[i];
    int now = 1;
    for (int j = 1; j <= len[i]; j++) {
      int p = s[j] - 'a';
      if (!tr[now][p]) tr[now][p] = ++tot, fa[tot] = now;
      now = tr[now][p];
    }
    ed[i] = now;
  }
  queue<int> Q;
  for (int i = 0; i < 26; i++)
    if (tr[1][i])
      Q.push(tr[1][i]), fail[tr[1][i]] = 1;
    else
      tr[1][i] = 1;
  while (!Q.empty()) {
    int x = Q.front();
    Q.pop();
    for (int i = 0; i < 26; i++)
      if (tr[x][i])
        Q.push(tr[x][i]), fail[tr[x][i]] = tr[fail[x]][i];
      else
        tr[x][i] = tr[fail[x]][i];
  }
  for (int i = 2; i <= tot; i++) fail_tr[fail[i]].push_back(i);
  int T = 500;
  for (int i = 1; i <= q; i++) {
    int l, r, k;
    scanf("%d%d%d", &l, &r, &k);
    if (len[k] > T)
      Lq[k].push_back(make_pair(l, i)), Rq[k].push_back(make_pair(r, i));
    else
      lq[l].push_back(make_pair(k, i)), rq[r].push_back(make_pair(k, i));
  }
  for (int i = 1; i <= n; i++)
    if (len[i] > T) {
      int now = ed[i];
      while (now != 1) sum[now] = 1, now = fa[now];
      dfs1(1);
      sort(Lq[i].begin(), Lq[i].end());
      sort(Rq[i].begin(), Rq[i].end());
      int sizl = Lq[i].size(), sizr = Rq[i].size();
      long long op = 0;
      for (int j = 1, l1 = 0, r1 = 0; j <= n; j++) {
        while (l1 < sizl && Lq[i][l1].first == j)
          ans[Lq[i][l1].second] -= op, l1++;
        op += sum[ed[j]];
        while (r1 < sizr && Rq[i][r1].first == j)
          ans[Rq[i][r1].second] += op, r1++;
      }
      for (int j = 1; j <= tot; j++) sum[j] = 0;
    }
  dfs2(1);
  for (int i = 1; i <= n; i++) {
    int sizl = lq[i].size(), sizr = rq[i].size();
    for (int j = 0; j < sizl; j++) {
      int now = ed[lq[i][j].first];
      while (now != 1) ans[lq[i][j].second] -= query(id[now]), now = fa[now];
    }
    add(id[ed[i]], 1);
    add(id[ed[i]] + tr_siz[ed[i]], -1);
    for (int j = 0; j < sizr; j++) {
      int now = ed[rq[i][j].first];
      while (now != 1) ans[rq[i][j].second] += query(id[now]), now = fa[now];
    }
  }
  for (int i = 1; i <= q; i++) printf("%lld\n", ans[i]);
  return 0;
}
