#include <bits/stdc++.h>
using namespace std;
const int Max = 3e5 + 10;
const int Sq = 350;
int n, sz = 1;
struct QSolver {
  vector<int> A[Max];
  int BL[Max], BR[Max];
  vector<pair<pair<int, int>, int>> QL, QR;
  long long ANS[Max];
  vector<pair<int, int>> Ql[Max], Qr[Max];
  int posl[Max], posr[Max];
  vector<pair<int, int>> Bl, Br;
  int cnt[Max];
  int tmpl[Max], tmpr[Max];
  long long leftres[Max];
  void SOLVE() {
    sort(QL.begin(), QL.end()), sort(QR.begin(), QR.end());
    for (pair<pair<int, int>, int> a : QL)
      Ql[a.first.second].push_back(make_pair(a.first.first, a.second));
    for (pair<pair<int, int>, int> a : QR)
      Qr[a.first.second].push_back(make_pair(a.first.first, a.second));
    for (int sq = 1; sq <= n; sq += Sq) {
      Bl.clear(), Br.clear();
      for (int i = sq; i < sq + Sq; i++)
        Bl.push_back(make_pair(BL[i], i)), Br.push_back(make_pair(BR[i], i));
      sort(Bl.begin(), Bl.end());
      sort(Br.begin(), Br.end());
      for (int i = 1; i <= sz; i++) cnt[i] = 0;
      for (int i = sq; i < sq + Sq; i++) cnt[BL[i]]++, cnt[BR[i]]--;
      for (int i = 1; i <= sz; i++) cnt[i] += cnt[i - 1];
      for (int i = 1; i <= n; i++) {
        if ((posl[i] < Ql[i].size() && Ql[i][posl[i]].first < sq + Sq) ||
            (posr[i] < Qr[i].size() && Qr[i][posr[i]].first < sq + Sq)) {
          int ptr1 = 0, ptr2 = 0;
          while (ptr1 < Bl.size()) {
            while (ptr2 < A[i].size() && A[i][ptr2] < Bl[ptr1].first) ptr2++;
            tmpl[Bl[ptr1].second] = ptr2;
            ptr1++;
          }
          ptr1 = 0, ptr2 = 0;
          while (ptr1 < Br.size()) {
            while (ptr2 < A[i].size() && A[i][ptr2] < Br[ptr1].first) ptr2++;
            tmpr[Br[ptr1].second] = ptr2;
            ptr1++;
          }
          while (posl[i] < Ql[i].size() && Ql[i][posl[i]].first < sq + Sq) {
            ANS[Ql[i][posl[i]].second] -= leftres[i];
            for (int j = sq; j <= Ql[i][posl[i]].first; j++)
              ANS[Ql[i][posl[i]].second] -= tmpr[j] - tmpl[j];
            posl[i]++;
          }
          while (posr[i] < Qr[i].size() && Qr[i][posr[i]].first < sq + Sq) {
            ANS[Qr[i][posr[i]].second] += leftres[i];
            for (int j = sq; j <= Qr[i][posr[i]].first; j++)
              ANS[Qr[i][posr[i]].second] += tmpr[j] - tmpl[j];
            posr[i]++;
          }
        }
      }
      for (int i = 1; i <= n; i++)
        for (int j : A[i]) leftres[i] += cnt[j];
    }
  }
};
string second[Max];
int C[Max][26], first[Max], pos[Max], Q[Max];
vector<int> N[Max];
int sttime[Max], edtime[Max], ttime;
QSolver solver;
void ADD(const string& s, int i) {
  int p = 1;
  for (int i = 1; i < s.size(); i++) {
    if (C[p][s[i] - 'a'])
      p = C[p][s[i] - 'a'];
    else
      p = C[p][s[i] - 'a'] = ++sz;
  }
  pos[i] = p;
}
void DFS(int v) {
  sttime[v] = ++ttime;
  for (int u : N[v]) DFS(u);
  edtime[v] = ttime + 1;
}
void AHO() {
  int L = 1, R = 0;
  first[1] = 1;
  for (int ch = 0; ch < 26; ch++)
    if (C[1][ch])
      first[C[1][ch]] = 1, Q[++R] = C[1][ch];
    else
      C[1][ch] = 1;
  while (L <= R) {
    int v = Q[L++];
    for (int ch = 0; ch < 26; ch++)
      if (C[v][ch])
        first[C[v][ch]] = C[first[v]][ch], Q[++R] = C[v][ch];
      else
        C[v][ch] = C[first[v]][ch];
  }
  for (int i = 2; i <= sz; i++) N[first[i]].push_back(i);
  DFS(1);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> second[i];
    second[i] = '.' + second[i];
    ADD(second[i], i);
  }
  AHO();
  for (int i = 1; i <= n; i++) {
    int p = 1;
    for (int j = 1; j < second[i].size(); j++) {
      p = C[p][second[i][j] - 'a'];
      solver.A[i].push_back(sttime[p]);
    }
    sort(solver.A[i].begin(), solver.A[i].end());
    solver.BL[i] = sttime[pos[i]];
    solver.BR[i] = edtime[pos[i]];
  }
  cout << '\n';
  for (int i = 1; i <= q; i++) {
    int l, r, k;
    cin >> l >> r >> k;
    solver.QL.push_back(make_pair(make_pair(l - 1, k), i));
    solver.QR.push_back(make_pair(make_pair(r, k), i));
  }
  solver.SOLVE();
  for (int i = 1; i <= q; i++) cout << solver.ANS[i] << '\n';
}
