#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, q, m, siz = 1, T, tim;
int len[N], ed[N], fail[N], trie[N][26], fa[N], dfn[N];
long long S[N], c[N], ans[N];
vector<int> E[N];
vector<pair<int, int> > L[N], R[N], lft[N], rgt[N];
char s[N];
queue<int> que;
template <typename T>
inline void chkmax(T &x, T y) {
  x = max(x, y);
}
template <typename T>
inline void chkmin(T &x, T y) {
  x = min(x, y);
}
template <typename T>
inline void read(T &x) {
  T f = 1;
  x = 0;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -f;
  for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
  x *= f;
}
inline void dfs1(int u) {
  for (auto v : E[u]) {
    dfs1(v);
    S[u] += S[v];
  }
}
inline void dfs2(int u) {
  S[u] = 1;
  dfn[u] = ++tim;
  for (auto v : E[u]) {
    dfs2(v);
    S[u] += S[v];
  }
}
inline void add(int x, int val) {
  for (int i = x; i <= siz; i += i & -i) c[i] += val;
}
inline int ask(int x) {
  int ret = 0;
  for (int i = x; i; i -= i & -i) ret += c[i];
  return ret;
}
int main() {
  read(n);
  read(q);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s + 1);
    len[i] = strlen(s + 1);
    m += len[i];
    int now = 1;
    for (int j = 1; j <= len[i]; ++j) {
      int ch = s[j] - 'a';
      if (!trie[now][ch]) trie[now][ch] = ++siz, fa[siz] = now;
      now = trie[now][ch];
    }
    ed[i] = now;
  }
  for (int i = 0; i < 26; ++i)
    if (trie[1][i])
      fail[trie[1][i]] = 1, que.push(trie[1][i]);
    else
      trie[1][i] = 1;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = 0; i < 26; ++i) {
      if (trie[u][i])
        fail[trie[u][i]] = trie[fail[u]][i], que.push(trie[u][i]);
      else
        trie[u][i] = trie[fail[u]][i];
    }
  }
  for (int i = 2; i <= siz; ++i) E[fail[i]].push_back(i);
  T = m / sqrt(q * log2(m));
  for (int i = 1, l, r, k; i <= q; ++i) {
    read(l), read(r), read(k);
    if (len[k] > T)
      L[k].push_back(make_pair(l, i)), R[k].push_back(make_pair(r, i));
    else
      lft[l].push_back(make_pair(k, i)), rgt[r].push_back(make_pair(k, i));
  }
  for (int i = 1; i <= n; ++i)
    if (len[i] > T) {
      int p = ed[i];
      while (p != 1) S[p] = 1, p = fa[p];
      dfs1(1);
      sort(L[i].begin(), L[i].end());
      reverse(L[i].begin(), L[i].end());
      sort(R[i].begin(), R[i].end());
      reverse(R[i].begin(), R[i].end());
      long long now = 0;
      for (int j = 1; j <= n; ++j) {
        while (L[i].size() && L[i].back().first == j)
          ans[L[i].back().second] -= now, L[i].pop_back();
        now += S[ed[j]];
        while (R[i].size() && R[i].back().first == j)
          ans[R[i].back().second] += now, R[i].pop_back();
      }
      for (int i = 2; i <= siz; ++i) S[i] = 0;
    }
  dfs2(1);
  for (int i = 1; i <= n; ++i) {
    for (pair<int, int> foo : lft[i]) {
      int p = ed[foo.first];
      while (p != 1) ans[foo.second] -= ask(dfn[p]), p = fa[p];
    }
    add(dfn[ed[i]], 1), add(dfn[ed[i]] + S[ed[i]], -1);
    for (pair<int, int> foo : rgt[i]) {
      int p = ed[foo.first];
      while (p != 1) ans[foo.second] += ask(dfn[p]), p = fa[p];
    }
  }
  for (int i = 1; i <= q; ++i) printf("%lld\n", ans[i]);
  return 0;
}
