#include <bits/stdc++.h>
using namespace std;
int n, q;
const int N = 400010;
int root[N], pos[N];
char s[N];
string c[N];
namespace XDS {
int cnt;
int lson[N * 25], rson[N * 25], tr[N * 25];
void Insert(int &k, int l, int r, int pos) {
  if (!k) k = ++cnt;
  ++tr[k];
  if (l == r) return;
  int mid = (l + r) >> 1;
  if (pos <= mid)
    Insert(lson[k], l, mid, pos);
  else
    Insert(rson[k], mid + 1, r, pos);
}
int merge(int x, int y, int l, int r) {
  if (!x || !y) return x + y;
  int k = ++cnt;
  if (l == r) {
    tr[k] = tr[x] + tr[y];
    return k;
  }
  int mid = (l + r) >> 1;
  lson[k] = merge(lson[x], lson[y], l, mid);
  rson[k] = merge(rson[x], rson[y], mid + 1, r);
  tr[k] = tr[lson[k]] + tr[rson[k]];
  return k;
}
int ask(int k, int l, int r, int x, int y) {
  if (!k) return 0;
  if (x <= l && r <= y) return tr[k];
  int mid = (l + r) >> 1, res = 0;
  if (x <= mid) res += ask(lson[k], l, mid, x, y);
  if (mid + 1 <= y) res += ask(rson[k], mid + 1, r, x, y);
  return res;
}
}  // namespace XDS
namespace SAM {
int cnt = 1, last = 1;
int tr[N][26], len[N], fa[N];
void Insert(int x, int id) {
  int p = last, np = ++cnt, q, nq;
  XDS::Insert(root[np], 1, n, id);
  last = np;
  len[np] = len[p] + 1;
  for (; p && !tr[p][x]; p = fa[p]) tr[p][x] = np;
  if (!p)
    fa[np] = 1;
  else if (len[q = tr[p][x]] == len[p] + 1)
    fa[np] = q;
  else {
    nq = ++cnt;
    len[nq] = len[p] + 1;
    fa[nq] = fa[q];
    memcpy(tr[nq], tr[q], sizeof(tr[q]));
    fa[q] = fa[np] = nq;
    for (; p && tr[p][x] == q; p = fa[p]) tr[p][x] = nq;
  }
}
void match(string s, int id) {
  int now = 1, len = s.length();
  for (int i = 0; i < len; ++i) {
    int c = s[i] - 'a';
    while (!tr[now][c]) now = fa[now];
    if (tr[now][c])
      now = tr[now][c];
    else
      now = 1;
  }
  pos[id] = now;
}
}  // namespace SAM
namespace SHU {
vector<int> g[N];
void dfs(int x) {
  for (int i = 0, siz = g[x].size(); i < siz; ++i) {
    dfs(g[x][i]);
    root[x] = XDS::merge(root[x], root[g[x][i]], 1, n);
  }
}
}  // namespace SHU
int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s);
    SAM::last = 1;
    c[i] = s;
    for (int j = 0, len = strlen(s); j < len; ++j) SAM::Insert(s[j] - 'a', i);
  }
  for (int i = 1; i <= n; ++i) SAM::match(c[i], i);
  for (int i = 2; i <= SAM::cnt; ++i) SHU::g[SAM::fa[i]].push_back(i);
  SHU::dfs(1);
  while (q--) {
    int l, r, k;
    scanf("%d%d%d", &l, &r, &k);
    printf("%d\n", XDS::ask(root[pos[k]], 1, n, l, r));
  }
  return 0;
}
