#include <bits/stdc++.h>
using namespace std;
int m, q, p[400005];
char s[200005];
int pp[400005], son0[15000000], son1[15000000], sum[15000000], n1;
int update(int l, int r, int k, int x) {
  if (!k) k = ++n1;
  ++sum[k];
  if (l == r) return k;
  int mid = (l + r) >> 1;
  if (x <= mid) son0[k] = update(l, mid, son0[k], x);
  if (x > mid) son1[k] = update(mid + 1, r, son1[k], x);
  return k;
}
int merge(int x, int y) {
  if (!x || !y) return x + y;
  int z = ++n1;
  sum[z] = sum[x] + sum[y];
  son0[z] = merge(son0[x], son0[y]);
  son1[z] = merge(son1[x], son1[y]);
  return z;
}
int find(int l, int r, int k, int x, int y) {
  if (l >= x && r <= y) return sum[k];
  int mid = (l + r) >> 1, ans = 0;
  if (x <= mid) ans += find(l, mid, son0[k], x, y);
  if (y > mid) ans += find(mid + 1, r, son1[k], x, y);
  return ans;
}
int n = 1, le, nex[400005], wen[400005], hea[400005], fa[400005], len[400005];
map<int, int> son[400005];
void add(int x, int y) {
  ++le;
  nex[le] = hea[x];
  wen[le] = y;
  hea[x] = le;
}
int insert(int x, int last) {
  int p1 = last;
  if (son[p1][x] && len[son[p1][x]] == len[p1] + 1) return son[p1][x];
  int p2 = ++n, boo = 0;
  len[p2] = len[p1] + 1;
  for (; p1 && !son[p1][x]; p1 = fa[p1]) son[p1][x] = p2;
  if (!p1)
    fa[p2] = 1;
  else {
    int q1 = son[p1][x];
    if (len[q1] == len[p1] + 1)
      fa[p2] = q1;
    else {
      if (p1 == last) boo = 1;
      int q2 = ++n;
      fa[q2] = fa[q1];
      son[q2] = son[q1];
      fa[q1] = fa[p2] = q2;
      len[q2] = len[p1] + 1;
      for (; p1 && son[p1][x] == q1; p1 = fa[p1]) son[p1][x] = q2;
    }
  }
  return boo ? n : p2;
}
void build() {
  for (int i = 2; i <= n; i++) add(fa[i], i);
}
void dfs(int x) {
  for (int i = hea[x]; i; i = nex[i]) {
    dfs(wen[i]);
    pp[x] = merge(pp[x], pp[wen[i]]);
  }
}
int main() {
  scanf("%d%d", &m, &q);
  for (int i = 1; i <= m; i++) {
    p[i] = 1;
    scanf("%s", s + 1);
    int len = strlen(s + 1);
    for (int j = 1; j <= len; j++) {
      p[i] = insert(s[j] - 97, p[i]);
      pp[p[i]] = update(1, m, pp[p[i]], i);
    }
  }
  build();
  dfs(1);
  for (int i = 1; i <= q; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    printf("%d\n", find(1, m, pp[p[z]], x, y));
  }
  return 0;
}
