#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, M = 26, NM = N * M;
string s[N];
int n, q, l[N], r[N], k[N], tot = 1, fa[NM], ed[N], in[NM], out[NM], ch[N][M],
                            idx, T;
int fail[NM], to[NM], head[NM], nxt[NM], etot;
long long ans[N], b[NM], f[NM];
void link(int x, int y) { to[++etot] = y, nxt[etot] = head[x], head[x] = etot; }
void add(int x, int v) {
  for (int i = x; i <= tot; i += i & -i) b[i] += v;
}
long long query(int x) {
  long long ret = 0;
  for (int i = x; i; i -= i & -i) ret += b[i];
  return ret;
}
void insert(int x) {
  int p = 1;
  for (int i = 0; i < s[x].length(); ++i) {
    int c = s[x][i] - 'a';
    if (!ch[p][c]) fa[ch[p][c] = ++tot] = p;
    p = ch[p][c];
  }
  ed[x] = p;
}
void build() {
  queue<int> q;
  for (int i = 0; i < 26; ++i)
    if (ch[1][i])
      fail[ch[1][i]] = 1, link(1, ch[1][i]), q.push(ch[1][i]);
    else
      ch[1][i] = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < 26; ++i)
      if (ch[x][i]) {
        int y = ch[x][i];
        int _x = fail[x];
        while (_x != 1 && !ch[_x][i]) _x = fail[_x];
        fail[y] = ch[_x][i], link(ch[_x][i], y);
        q.push(y);
      }
  }
}
void dfs(int x) {
  in[x] = ++idx;
  for (int i = head[x]; i; i = nxt[i]) dfs(to[i]);
  out[x] = idx;
}
void dfs2(int x) {
  for (int i = head[x]; i; i = nxt[i]) dfs2(to[i]), f[x] += f[to[i]];
}
vector<pair<int, int> > e[N];
vector<pair<int, int> > ql[N], qr[N];
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> q;
  T = sqrt(max(n, q));
  for (int i = 1; i <= n; ++i) cin >> s[i], insert(i);
  build();
  dfs(1);
  for (int i = 1; i <= q; ++i) {
    cin >> l[i] >> r[i] >> k[i];
    if (s[k[i]].length() < T) {
      e[l[i] - 1].push_back(make_pair(i, -1));
      e[r[i]].push_back(make_pair(i, 1));
    } else
      ql[k[i]].push_back(make_pair(l[i] - 1, i)),
          qr[k[i]].push_back(make_pair(r[i], i));
  }
  for (int i = 1; i <= n; ++i) {
    add(in[ed[i]], 1);
    add(out[ed[i]] + 1, -1);
    for (auto t : e[i]) {
      int x = ed[k[t.first]];
      long long ret = 0;
      while (x) ret += query(in[x]), x = fa[x];
      ans[t.first] += t.second * ret;
    }
    if (s[i].length() >= T) {
      sort(ql[i].begin(), ql[i].end());
      sort(qr[i].begin(), qr[i].end());
      for (int j = 1; j <= tot; ++j) f[j] = 0;
      int x = ed[i];
      while (x) ++f[x], x = fa[x];
      dfs2(1);
      long long ret = 0;
      for (int j = 0, p1 = 0, p2 = 0; j <= n; ++j) {
        ret += f[ed[j]];
        while (p1 < ql[i].size() && ql[i][p1].first == j)
          ans[ql[i][p1++].second] -= ret;
        while (p2 < qr[i].size() && qr[i][p2].first == j)
          ans[qr[i][p2++].second] += ret;
      }
    }
  }
  for (int i = 1; i <= q; ++i) cout << ans[i] << endl;
  return 0;
}
