#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 5;
const int LG = 20;
string s[MAX];
int st[MAX];
int en[MAX];
int sa[LG][MAX];
int finsa[MAX];
int rev[MAX];
pair<pair<int, int>, int> tmp[MAX];
int lcp[MAX];
int rep[MAX];
int rmq[LG][MAX];
long long ans[MAX];
pair<int, pair<pair<int, int>, pair<int, int> > > qq[MAX];
int bit[MAX];
void update(int x, int val) {
  x++;
  while (x < MAX) {
    bit[x] += val;
    x += (x & (-x));
  }
}
long long query(int x) {
  x++;
  long long ans = 0;
  while (x) {
    ans += bit[x];
    x -= (x & (-x));
  }
  return ans;
}
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  string cur = "";
  int len = 0;
  for (int i = 1; i <= n; i++) {
    st[i] = cur.length();
    for (int j = 0; j < s[i].length(); j++) {
      rep[cur.length()] = i;
      cur += s[i][j];
    }
    en[i] = cur.length();
    cur += '$';
  }
  st[n + 1] = cur.length();
  for (int i = 0; i < cur.length(); i++) {
    if (cur[i] == '$') {
      sa[0][i] = 1000 + i;
    } else {
      sa[0][i] = cur[i] - 'a' + 1;
    }
  }
  for (int i = 1; i < LG; i++) {
    for (int j = 0; j < cur.length(); j++) {
      int x = (1 << (i - 1));
      tmp[j] =
          make_pair(make_pair(sa[i - 1][j],
                              ((j + x) < cur.length()) ? sa[i - 1][j + x] : -1),
                    j);
    }
    sort(tmp, tmp + (int)cur.length());
    sa[i][tmp[0].second] = 1;
    int val = 1;
    for (int j = 1; j < cur.length(); j++) {
      val += (tmp[j].first != tmp[j - 1].first);
      sa[i][tmp[j].second] = val;
    }
  }
  for (int i = 0; i < cur.length(); i++) {
    finsa[sa[LG - 1][i]] = i;
  }
  for (int i = 1; i < cur.length(); i++) {
    int x = finsa[i];
    int y = finsa[i + 1];
    for (int j = LG - 1; j >= 0; j--) {
      if (sa[j][x] == sa[j][y]) {
        x += (1 << j);
        y += (1 << j);
        lcp[i] += (1 << j);
      }
    }
  }
  for (int i = 1; i < cur.length(); i++) {
    rmq[0][i] = lcp[i];
  }
  for (int i = 1; i < LG; i++) {
    for (int j = 1; j < cur.length(); j++) {
      if ((j + (1 << (i - 1))) >= cur.length()) {
        rmq[i][j] = rmq[i - 1][j];
      } else {
        rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
      }
    }
  }
  int t = 0;
  for (int i = 1; i <= q; i++) {
    int l, r, k;
    scanf("%d %d %d", &l, &r, &k);
    l = st[l];
    r = st[r + 1] - 1;
    int sta = st[k];
    int fin = st[k + 1] - 1;
    int len = (fin - sta);
    int re = sa[LG - 1][sta];
    int hi = re;
    int lo = re;
    for (int j = LG - 1; j >= 0; j--) {
      if (((lo - (1 << j)) >= 1) && rmq[j][lo - (1 << j)] >= len) {
        lo -= (1 << j);
      }
      if (((hi + (1 << j)) < cur.length()) && rmq[j][hi] >= len) {
        hi += (1 << j);
      }
    }
    qq[t++] = make_pair(lo - 1, make_pair(make_pair(l, r), make_pair(-1, i)));
    qq[t++] = make_pair(hi, make_pair(make_pair(l, r), make_pair(1, i)));
  }
  sort(qq, qq + t);
  int pos = 1;
  for (int i = 0; i < t; i++) {
    while (pos <= cur.size() && pos <= qq[i].first) {
      update(finsa[pos], 1);
      pos++;
    }
    ans[qq[i].second.second.second] += (query(qq[i].second.first.second) -
                                        query(qq[i].second.first.first - 1)) *
                                       1ll * qq[i].second.second.first;
  }
  for (int i = 1; i <= q; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}
