#include <bits/stdc++.h>
using namespace std;
int ri() {
  int x;
  scanf("%d", &x);
  return x;
}
const int N = 200000, Q = 500000;
char a[N + N];
int tick, ans[Q], fenwick[2 * N];
struct Node {
  Node *f, *c[26];
  int len, l, r;
  vector<Node *> e;
} pool[2 * N], *tail, *pit = pool + 1, *w[N];
vector<tuple<int, Node *, int> > q[N];
void extend(char c) {
  Node *p = tail, *x = pit++;
  x->len = p->len + 1;
  for (; p && !p->c[c]; p = p->f) p->c[c] = x;
  if (!p)
    x->f = pool;
  else if (p->len + 1 == p->c[c]->len)
    x->f = p->c[c];
  else {
    Node *q = p->c[c], *r = pit++;
    *r = *q;
    r->len = p->len + 1;
    x->f = q->f = r;
    for (; p && p->c[c] == q; p = p->f) p->c[c] = r;
  }
  tail = x;
}
void dfs(Node *x) {
  x->l = tick++;
  for (auto y : x->e) dfs(y);
  x->r = tick;
}
void add(int n, int x) {
  for (; x < n; x |= x + 1) fenwick[x]++;
}
int getSum(int x) {
  int s = 0;
  for (; x; x &= x - 1) s += fenwick[x - 1];
  return s;
}
int main() {
  int n = ri(), m = ri(), l = 0;
  for (int i = (0); i < (n); i++) {
    int j = 0;
    scanf("%s", a + l);
    tail = pool;
    for (j = 0; a[l + j]; j++) extend(a[l + j] - 'a');
    l += j + 1;
  }
  l = 0;
  for (int i = (0); i < (n); i++) {
    Node *x = pool;
    int j = 0;
    for (j = 0; a[l + j]; j++) x = x->c[a[l + j] - 'a'];
    l += j + 1;
    w[i] = x;
  }
  for (Node *x = pool; x < pit; x++)
    if (x->f) x->f->e.push_back(x);
  dfs(pool);
  for (int i = (0); i < (m); i++) {
    int x = ri() - 1, y = ri() - 1, z = ri() - 1;
    if (x) q[x - 1].emplace_back(-1, w[z], i);
    q[y].emplace_back(1, w[z], i);
  }
  l = 0;
  for (int i = (0); i < (n); i++) {
    Node *x = pool;
    int j = 0;
    for (j = 0; a[l + j]; j++) {
      x = x->c[a[l + j] - 'a'];
      add(tick, x->l);
    }
    l += j + 1;
    for (auto qq : q[i]) {
      x = get<1>(qq);
      ans[get<2>(qq)] += get<0>(qq) * (getSum(x->r) - getSum(x->l));
    }
  }
  for (int i = (0); i < (m); i++) printf("%d\n", ans[i]);
}
