#include <bits/stdc++.h>
using namespace std;
template <class T>
T gi() {
  T x = 0;
  bool f = 0;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  if (c == '-') f = 1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return f ? -x : x;
}
const int N = 1e5 + 10;
int ch[N][26], tot, pos[N];
string s[N];
vector<int> End[N];
void insert(char *str, int id) {
  int n = strlen(str), now = 0;
  for (int i = 0; i < n; i++) {
    if (!ch[now][str[i] - 'a']) ch[now][str[i] - 'a'] = ++tot;
    End[now = ch[now][str[i] - 'a']].push_back(id);
  }
  for (int i = 0; i < n; i++) s[id].push_back(str[i]);
  pos[id] = now;
}
int fail[N];
void build() {
  queue<int> q;
  for (int i = 0; i < 26; i++)
    if (ch[0][i]) q.push(ch[0][i]);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < 26; i++)
      if (!ch[u][i])
        ch[u][i] = ch[fail[u]][i];
      else
        fail[ch[u][i]] = ch[fail[u]][i], q.push(ch[u][i]);
  }
}
char str[N];
vector<int> e[N];
int L[N], R[N], tim, n, Q, f[N][500], cnt, bel[N];
void dfs(int u) {
  L[u] = ++tim;
  for (auto v : e[u]) {
    dfs(v);
    for (int j = 1; j <= cnt; j++) f[u][j] += f[v][j];
  }
  for (auto i : End[u])
    if (bel[i]) f[u][bel[i]]++;
  R[u] = tim;
}
int c[N];
void add(int x, int k) {
  for (; x <= tim; x += (x & (-x))) c[x] += k;
}
long long sum(int x) {
  long long res = 0;
  for (; x; x -= (x & (-x))) res += c[x];
  return res;
}
vector<pair<int, int> > q[N];
long long ans[N], tmp[N];
int main() {
  n = gi<int>(), Q = gi<int>();
  for (int i = 1; i <= n; i++) scanf("%s", str), insert(str, i);
  int T = 400;
  build();
  for (int i = 1; i <= tot; i++) e[fail[i]].push_back(i);
  for (int i = 1; i <= n; i++)
    if (s[i].length() > T) bel[i] = ++cnt;
  dfs(0);
  for (int i = 1; i <= Q; i++) {
    int l = gi<int>(), r = gi<int>(), x = gi<int>();
    q[l - 1].emplace_back(x, -i);
    q[r].emplace_back(x, i);
  }
  for (int i = 1; i <= n; i++) {
    add(L[pos[i]], 1);
    add(R[pos[i]] + 1, -1);
    for (int j = 1; j <= cnt; j++) tmp[j] += f[pos[i]][j];
    for (auto j : q[i]) {
      long long res = 0;
      int x = j.first;
      if (s[x].length() <= T) {
        int now = 0, len = s[x].length();
        for (int k = 0; k < len; k++)
          res += sum(L[now = ch[now][s[x][k] - 'a']]);
      } else
        res = tmp[bel[x]];
      if (j.second < 0)
        ans[-j.second] -= res;
      else
        ans[j.second] += res;
    }
  }
  for (int i = 1; i <= Q; i++) printf("%lld\n", ans[i]);
  return 0;
}
