#include <bits/stdc++.h>
using i64 = long long;
const int N = 100000 + 7;
int n, m = 1, q;
std::string s[N];
int ept[N];
int kid[N][26], lnk[N];
std::vector<int> e[N];
int dfn[N], siz[N], idfn;
int x[N], y[N], z[N];
i64 ans[N];
std::vector<int> g[N];
void dfs(int s) {
  dfn[s] = ++idfn, siz[s] = 1;
  for (auto t : e[s]) dfs(t), siz[s] += siz[t];
}
int bit[N];
void add(int i, int v) {
  for (; i <= m; i += i & -i) bit[i] += v;
}
int sum(int i) {
  int v = 0;
  for (; i; i -= i & -i) v += bit[i];
  return v;
}
int main() {
  std::cin >> n >> q;
  for (int i = 1; i <= n; ++i) {
    std::cin >> s[i];
    int p = 1;
    for (auto j : s[i]) {
      if (!kid[p][j - 'a']) kid[p][j - 'a'] = ++m;
      p = kid[p][j - 'a'];
    }
    ept[i] = p;
  }
  static std::queue<int> qu;
  for (int i = 0; i < 26; ++i) {
    if (kid[1][i]) qu.push(kid[1][i]);
    (kid[1][i] ? lnk[kid[1][i]] : kid[1][i]) = 1;
  }
  for (int p; qu.size();) {
    p = qu.front(), qu.pop();
    for (int i = 0; i < 26; ++i) {
      if (kid[p][i]) qu.push(kid[p][i]);
      (kid[p][i] ? lnk[kid[p][i]] : kid[p][i]) = kid[lnk[p]][i];
    }
  }
  for (int i = 2; i <= m; ++i) e[lnk[i]].push_back(i);
  dfs(1);
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d%d", x + i, y + i, z + i);
    if (s[z[i]].size() <= 330) g[x[i] - 1].push_back(i), g[y[i]].push_back(i);
  }
  for (int i = 1; i <= n; ++i) {
    add(dfn[ept[i]], 1), add(dfn[ept[i]] + siz[ept[i]], -1);
    for (auto j : g[i])
      for (int k = 0, p = 1; k < s[z[j]].size(); ++k)
        p = kid[p][s[z[j]][k] - 'a'],
        ans[j] += (i == y[j] ? 1 : -1) * sum(dfn[p]);
  }
  for (int i = 1; i <= n; ++i) {
    if (s[i].size() <= 330) continue;
    for (int j = 1; j <= n; ++j) g[j].clear();
    for (int j = 1; j <= q; ++j)
      if (z[j] == i) g[x[j] - 1].push_back(j), g[y[j]].push_back(j);
    std::fill(bit + 1, bit + m + 1, 0);
    for (int j = 0, p = 1; j < s[i].size(); ++j)
      p = kid[p][s[i][j] - 'a'], add(dfn[p], 1);
    i64 k = 0;
    for (int j = 1; j <= n; ++j) {
      k += sum(dfn[ept[j]] + siz[ept[j]] - 1) - sum(dfn[ept[j]] - 1);
      for (int l : g[j]) ans[l] += (j == y[l] ? 1 : -1) * k;
    }
  }
  for (int i = 1; i <= q; ++i) std::cout << ans[i] << std::endl;
  return 0;
}
