#include <bits/stdc++.h>
using namespace std;
int read() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || '9' < c) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return f * x;
}
int n, acnt, Root, fa[200000 + 5], nxt[200000 + 5][26], fail[200000 + 5],
    ed[200000 + 5];
void Init() {
  acnt = Root = 1;
  return;
}
vector<int> G[200000 + 5], N[200000 + 5];
void Insert(char *s, int len, int id) {
  int u = Root;
  for (int i = 1; i <= len; i++) {
    int c = s[i] - 'a';
    if (!nxt[u][c]) {
      nxt[u][c] = ++acnt, fa[nxt[u][c]] = u;
    }
    u = nxt[u][c];
    N[u].push_back(id);
  }
  ed[id] = u;
  return;
}
void Make_Fail() {
  queue<int> Q;
  for (int c = 0; c < 26; c++)
    if (nxt[Root][c])
      fail[nxt[Root][c]] = Root, Q.push(nxt[Root][c]);
    else
      nxt[Root][c] = Root;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int c = 0; c < 26; c++) {
      if (nxt[u][c])
        fail[nxt[u][c]] = nxt[fail[u]][c], Q.push(nxt[u][c]);
      else
        nxt[u][c] = nxt[fail[u]][c];
    }
  }
  return;
}
int tree[200000 + 5];
int lowbit(int x) { return x & (-x); }
int Sum(int i) {
  int ret = 0;
  while (i) ret += tree[i], i -= lowbit(i);
  return ret;
}
void Add(int i, int x) {
  while (i <= acnt) tree[i] += x, i += lowbit(i);
  return;
}
int dcnt, dfn[200000 + 5], siz[200000 + 5];
void DFS(int u) {
  dfn[u] = ++dcnt, siz[u] = 1;
  for (int i = 0; i < (int)G[u].size(); i++) {
    int v = G[u][i];
    DFS(v), siz[u] += siz[v];
  }
  return;
}
int ans[500000 + 5];
char S[200000 + 5];
struct node {
  int x, id, s;
};
vector<node> Q[200000 + 5];
int main() {
  n = read();
  int q = read();
  Init();
  for (int i = 1; i <= n; i++) scanf("%s", S + 1), Insert(S, strlen(S + 1), i);
  Make_Fail();
  for (int i = 2; i <= acnt; i++) G[fail[i]].push_back(i);
  DFS(Root);
  for (int i = 1; i <= q; i++) {
    int l = read(), r = read(), x = read();
    Q[l - 1].push_back((node){x, i, -1});
    Q[r].push_back((node){x, i, 1});
  }
  for (int i = 1; i <= n; i++) {
    int u = ed[i];
    while (u != Root) Add(dfn[u], 1), u = fa[u];
    for (int j = 0; j < (int)Q[i].size(); j++) {
      int x = Q[i][j].x, id = Q[i][j].id, sign = Q[i][j].s;
      ans[id] +=
          sign * (Sum(dfn[ed[x]] + siz[ed[x]] - 1) - Sum(dfn[ed[x]] - 1));
    }
  }
  for (int i = 1; i <= q; i++) printf("%d\n", ans[i]);
  return 0;
}
