#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 1000;
const int maxs = 320;
int root, cnt, n, qq, sq, m, Time;
int ql[maxn], qr[maxn], qk[maxn];
int q[maxn], ch[maxn][26], fail[maxn];
int ed[maxn], lft[maxn], rght[maxn], L[maxn], R[maxn], bel[maxn];
int tot = 0, V[maxn], N[maxn], F[maxn];
int presum[maxn], dp[maxn];
long long ans[maxn];
vector<int> pos[maxn], ask[maxs];
char tmp[maxn];
vector<char> str[maxn];
void add(int a, int b) { ++tot, V[tot] = b, N[tot] = F[a], F[a] = tot; }
void ins(char* s, int ind) {
  int at = root;
  int len = strlen(s);
  for (int i = 0; i < len; i++) {
    int d = s[i] - 'a';
    if (!ch[at][d]) ch[at][d] = ++cnt;
    at = ch[at][d];
    pos[ind].push_back(at);
  }
  ed[ind] = at;
}
void build() {
  int hd = 0, tl = 1;
  q[1] = root;
  fail[1] = 0;
  while (hd != tl) {
    int x = q[++hd];
    for (int i = 0; i < 26; i++)
      if (ch[x][i]) {
        int y = ch[x][i], ba = fail[x];
        q[++tl] = y;
        while (ba != 0 && !ch[ba][i]) ba = fail[ba];
        fail[y] = ba == 0 ? root : ch[ba][i];
      }
  }
}
void dfs(int x) {
  lft[x] = ++Time;
  for (int p = F[x]; p > 0; p = N[p]) dfs(V[p]);
  rght[x] = Time;
}
int solve(int x, int y) {
  int p1 =
      lower_bound(pos[y].begin(), pos[y].end(), lft[ed[x]]) - pos[y].begin();
  int p2 =
      upper_bound(pos[y].begin(), pos[y].end(), rght[ed[x]]) - pos[y].begin();
  return p2 - p1;
}
int main() {
  root = cnt = 1;
  scanf("%d%d", &n, &qq);
  for (int i = 1; i <= n; i++) pos[i].clear();
  for (int i = 1; i <= n; i++) {
    scanf("%s", tmp);
    int len = strlen(tmp);
    str[i].clear();
    for (int j = 0; j < len; j++) str[i].push_back(tmp[j]);
    ins(tmp, i);
  }
  build();
  sq = (int)sqrt(n);
  if (sq * sq < n) sq++;
  m = 0;
  for (int l = 1; l <= n; l++) {
    L[++m] = l;
    R[m] = min(n, l + sq - 1);
    for (int i = L[m]; i <= R[m]; i++) bel[i] = m;
    l = R[m];
  }
  tot = 0;
  memset(N, 0, sizeof N);
  memset(F, 0, sizeof F);
  for (int i = 1; i <= cnt; i++) add(fail[i], i);
  Time = 0;
  dfs(root);
  for (int i = 1; i <= n; i++) {
    for (int& e : pos[i]) e = lft[e];
    sort(pos[i].begin(), pos[i].end());
  }
  for (int i = 1; i <= m; i++) ask[i].clear();
  for (int i = 1; i <= qq; i++) {
    scanf("%d%d%d", &ql[i], &qr[i], &qk[i]);
    int l = ql[i], r = qr[i], k = qk[i];
    ans[i] = 0;
    if (bel[l] == bel[r]) {
      for (int j = l; j <= r; j++) ans[i] += solve(j, k);
      continue;
    }
    for (int j = l; j <= R[bel[l]]; j++) ans[i] += solve(j, k);
    for (int j = L[bel[r]]; j <= r; j++) ans[i] += solve(j, k);
    for (int j = bel[l] + 1; j < bel[r]; j++) ask[j].push_back(i);
  }
  for (int i = 1; i <= m; i++) {
    memset(presum, 0, sizeof presum);
    for (int j = L[i]; j <= R[i]; j++) {
      presum[lft[ed[j]]]++;
      presum[rght[ed[j]] + 1]--;
    }
    for (int j = 1; j <= cnt; j++) presum[j] += presum[j - 1];
    for (int j = 1; j <= n; j++) {
      dp[j] = 0;
      for (int e : pos[j]) dp[j] += presum[e];
    }
    for (int j : ask[i]) ans[j] += dp[qk[j]];
  }
  for (int i = 1; i <= qq; i++) printf("%I64d\n", ans[i]);
  return 0;
}
