#include <bits/stdc++.h>
using namespace std;
int cntnode = 1;
int fa[1000005];
char str[1000005];
int nxt[200005][26];
int mp[1000005], t[1000005];
void init(int pp) {
  int np, i;
  int l;
  np = 1;
  l = strlen(str);
  for (i = 0; i < l; i++) {
    if (!nxt[np][str[i] - 'a']) {
      cntnode++;
      nxt[np][str[i] - 'a'] = cntnode;
      fa[cntnode] = np;
    }
    np = nxt[np][str[i] - 'a'];
  }
  mp[pp] = np;
}
int z[1000005];
struct node {
  int fr, tt;
} s[1000005];
int siz[1000005];
int head[1000005], cnte = 0;
int cntdfn = 0, dfn[1000005], tt[1000005];
int ans[1000005];
int par[1000005];
void adde(int a, int b) {
  s[cnte].fr = head[a];
  head[a] = cnte;
  s[cnte].tt = b;
  cnte++;
}
void build() {
  int i;
  int head, tail;
  head = 0, tail = 1, z[tail] = 1;
  par[1] = 0;
  for (i = 0; i < 26; i++) {
    nxt[0][i] = 1;
  }
  while (head < tail) {
    head++;
    for (i = 0; i < 26; i++)
      if (nxt[z[head]][i])
        par[nxt[z[head]][i]] = nxt[par[z[head]]][i], tail++,
        z[tail] = nxt[z[head]][i];
      else
        nxt[z[head]][i] = nxt[par[z[head]]][i];
  }
}
int lowbit(int val) { return val & (-val); }
void add(int pos, int val) {
  int i;
  for (i = pos; i <= cntnode; i += lowbit(i)) t[i] += val;
}
int query(int pos) {
  int i, sum = 0;
  for (i = pos; i; i -= lowbit(i)) sum += t[i];
  return sum;
}
void dfs(int pos) {
  int i;
  cntdfn++;
  dfn[pos] = cntdfn;
  for (i = head[pos]; i != -1; i = s[i].fr) dfs(s[i].tt);
  tt[pos] = cntdfn;
}
struct qq {
  int id, lab, tim, p;
} q[1000005];
bool cmptim(qq a, qq b) { return a.tim < b.tim; }
int main() {
  int n, cq, i, cntq = 0;
  int a, b, c;
  scanf("%d%d", &n, &cq);
  for (i = 1; i <= n; i++) {
    scanf("%s", str);
    init(i);
  }
  build();
  for (i = 1; i <= cntnode; i++) head[i] = -1;
  for (i = 1; i <= cntnode; i++) adde(par[i], i);
  dfs(1);
  cntq = 0;
  for (i = 1; i <= cq; i++) {
    scanf("%d%d%d", &a, &b, &c);
    cntq++;
    q[cntq].id = i;
    q[cntq].lab = -1;
    q[cntq].tim = a - 1;
    q[cntq].p = mp[c];
    cntq++;
    q[cntq].id = i;
    q[cntq].lab = 1;
    q[cntq].tim = b;
    q[cntq].p = mp[c];
  }
  sort(q + 1, q + 1 + cntq, cmptim);
  for (i = 0; i <= cntnode; i++) t[i] = 0;
  int nt = 0, np;
  for (i = 1; i <= cntq; i++) {
    while (nt < q[i].tim) {
      nt++;
      np = mp[nt];
      while (np) {
        add(dfn[np], 1);
        np = fa[np];
      }
    }
    ans[q[i].id] += q[i].lab * (query(tt[q[i].p]) - query(dfn[q[i].p] - 1));
  }
  for (i = 1; i <= cq; i++) printf("%d\n", ans[i]);
  return 0;
}
