#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:LonlyCoder&_Codes")
using namespace std;
const int N(2e5 + 20);
const int Q(5e5 + 20);
struct NODE {
  int v, nxt;
} edges[N];
int n, q, en, ind, ed[N], tot, a[Q], point[N], in[N], out[N], fail[N],
    son[N][26], ans[Q], c[N], fa[N];
char s[N];
vector<pair<int, int> > ask[N];
inline void addedge(int u, int v) {
  edges[++en] = (NODE){v, point[u]};
  point[u] = en;
}
inline void make_fail() {
  static queue<int> q;
  for (int i(0); i < 26; ++i)
    if (son[0][i]) q.push(son[0][i]);
  for (; !q.empty(); q.pop()) {
    int u = q.front();
    for (int i(0); i < 26; ++i) {
      if (!son[u][i]) {
        son[u][i] = son[fail[u]][i];
        continue;
      }
      int x = son[u][i];
      q.push(x);
      fail[x] = son[fail[u]][i];
    }
  }
  for (int i(1); i <= tot; ++i) addedge(fail[i], i);
}
inline void dfs(int x) {
  in[x] = ++ind;
  for (int i(point[x]); i; i = edges[i].nxt) dfs(edges[i].v);
  out[x] = ind;
}
inline void add(int x) {
  for (int i(x); i <= ind; i += i & (-i)) c[i]++;
}
inline int query(int x) {
  int res(0);
  for (int i(x); i; i -= i & (-i)) res += c[i];
  return res;
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i(1); i <= n; ++i) {
    scanf("%s", s);
    int now = 0, len = strlen(s);
    for (int j(0); j < len; ++j) {
      int c = s[j] - 'a';
      if (!son[now][c]) fa[son[now][c] = ++tot] = now;
      now = son[now][c];
    }
    ed[i] = now;
  }
  make_fail();
  dfs(0);
  for (int i(1), l, r; i <= q; ++i) {
    scanf("%d%d%d", &l, &r, a + i);
    ask[l - 1].push_back(pair<int, int>(i, -1));
    ask[r].push_back(pair<int, int>(i, 1));
  }
  for (int i(1); i <= n; ++i) {
    for (int x = ed[i]; x; x = fa[x]) add(in[x]);
    for (vector<pair<int, int> >::iterator j = ask[i].begin();
         j != ask[i].end(); ++j) {
      int p = j->first, x = ed[a[p]];
      ans[p] += j->second * (query(out[x]) - query(in[x] - 1));
    }
  }
  for (int i(1); i <= q; ++i) printf("%d\n", ans[i]);
  return 0;
}
