#include <bits/stdc++.h>
using namespace std;
const int MN = 5e5 + 10;
int n, q;
string str[MN];
long long ans[MN];
int nxt[26][MN], f[MN];
int sz = 1;
vector<int> child[MN];
int fen[MN], ind[MN];
struct query {
  bool f;
  int id, K;
  query(int F = 0, int Id = 0, int K_ = 0) { f = F, id = Id, K = K_; }
};
vector<query> qr[MN];
int Add(const string &s) {
  int cur = 0;
  for (int i = 0; i < ((int)(s).size()); ++i) {
    if (!nxt[s[i] - 'a'][cur]) nxt[s[i] - 'a'][cur] = sz++;
    cur = nxt[s[i] - 'a'][cur];
  }
  return cur;
}
int que[MN], L, R;
int st[MN], ft[MN], Time;
void dfs(int s) {
  st[s] = Time++;
  for (auto v : child[s]) dfs(v);
  ft[s] = Time;
}
void add(int p) {
  for (p++; p < MN; p += p & -p) fen[p]++;
}
int get(int p) {
  int ret = 0;
  for (; p; p -= p & -p) ret += fen[p];
  return ret;
}
void check(const string &s) {
  int cur = 0;
  for (int i = 0; i < ((int)(s).size()); ++i) {
    cur = nxt[s[i] - 'a'][cur];
    add(st[cur]);
  }
}
void Aho() {
  for (int i = 0; i < 26; ++i)
    if (nxt[i][0]) que[R++] = nxt[i][0];
  while (L < R) {
    int v = que[L++];
    for (int i = 0; i < 26; ++i)
      if (nxt[i][v]) {
        f[nxt[i][v]] = nxt[i][f[v]];
        que[R++] = nxt[i][v];
      } else
        nxt[i][v] = nxt[i][f[v]];
    child[f[v]].push_back(v);
  }
  dfs(0);
}
int main() {
  ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> q;
  for (int i = 0; i < n; ++i) {
    cin >> str[i];
    ind[i] = Add(str[i]);
  }
  Aho();
  for (int i = 0; i < q; ++i) {
    int l, r, id;
    cin >> l >> r >> id;
    --id;
    l -= 2, --r;
    if (l >= 0) qr[l].push_back(query(0, i, ind[id]));
    qr[r].push_back(query(1, i, ind[id]));
  }
  for (int i = 0; i < n; ++i) {
    check(str[i]);
    for (auto x : qr[i]) {
      if (x.f)
        ans[x.id] += get(ft[x.K]) - get(st[x.K]);
      else
        ans[x.id] -= get(ft[x.K]) - get(st[x.K]);
    }
  }
  for (int i = 0; i < q; ++i) cout << ans[i] << '\n';
  return 0;
}
