#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int N, Q, B, L, son[MAXN][26], fail[MAXN], Tlen;
char S[MAXN];
vector<int> V[MAXN];
int id[MAXN], idcnt, val[320][100005];
long long sum[320], ans[MAXN];
void ins(char* S, int L, int pos) {
  int p = 0;
  for (int i = 1; i <= L; ++i) {
    int& v = son[p][S[i] - 'a'];
    if (!v) v = ++Tlen;
    p = v, V[pos].push_back(p);
  }
}
queue<int> que;
struct node {
  int v, next;
} E[MAXN];
int head[MAXN], Elen;
void add(int u, int v) {
  ++Elen, E[Elen].v = v, E[Elen].next = head[u], head[u] = Elen;
}
void build() {
  for (int i = 0; i < 26; ++i)
    if (son[0][i]) que.push(son[0][i]);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = 0; i < 26; ++i) {
      if (son[u][i])
        fail[son[u][i]] = son[fail[u]][i], que.push(son[u][i]);
      else
        son[u][i] = son[fail[u]][i];
    }
  }
  for (int i = 1; i <= Tlen; ++i) add(fail[i], i);
}
int dfn[MAXN], siz[MAXN], dfncnt;
void dfs(int u) {
  dfn[u] = ++dfncnt;
  siz[u] = 1;
  for (int i = head[u]; i; i = E[i].next) {
    dfs(E[i].v), siz[u] += siz[E[i].v];
    for (int j = 1; j <= idcnt; ++j) val[j][u] += val[j][E[i].v];
  }
}
struct query {
  int r, k, id, op;
  query(int r = 0, int k = 0, int id = 0, int op = 0)
      : r(r), k(k), id(id), op(op) {}
} q[MAXN];
int ql;
bool cmp(query q1, query q2) { return q1.r < q2.r; }
int tr[MAXN];
inline int lowbit(int x) { return x & -x; }
void ins(int pos, int k) {
  for (; pos <= dfncnt; pos += lowbit(pos)) tr[pos] += k;
}
int qry(int pos) {
  int ret = 0;
  for (; pos; pos -= lowbit(pos)) ret += tr[pos];
  return ret;
}
int main() {
  scanf("%d%d", &N, &Q), B = 320;
  for (int i = 1; i <= N; ++i) {
    scanf("%s", S + 1), L = strlen(S + 1), ins(S, L, i);
    if (L > B) {
      id[i] = ++idcnt;
      for (int j : V[i]) ++val[idcnt][j];
    }
  }
  build(), dfs(0);
  int l, r, k;
  for (int i = 1; i <= Q; ++i)
    scanf("%d%d%d", &l, &r, &k), q[++ql] = query(l - 1, k, i, -1),
                                 q[++ql] = query(r, k, i, 1);
  sort(q + 1, q + ql + 1, cmp);
  for (int i = 1, j = 1; i <= ql; ++i) {
    while (j <= q[i].r) {
      int u = V[j][V[j].size() - 1];
      for (int k = 1; k <= idcnt; ++k) sum[k] += val[k][u];
      ins(dfn[u], 1), ins(dfn[u] + siz[u], -1);
      ++j;
    }
    if (id[q[i].k]) {
      ans[q[i].id] += q[i].op * sum[id[q[i].k]];
    } else {
      for (int k : V[q[i].k]) ans[q[i].id] += q[i].op * qry(dfn[k]);
    }
  }
  for (int i = 1; i <= Q; ++i) printf("%lld\n", ans[i]);
  return 0;
}
