#include <bits/stdc++.h>
using namespace std;
string s[200005];
namespace FastIO {
const int L = 1 << 20;
char buf[L], *S, *T;
int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
void reads(int id) {
  s[id] = "";
  char c = getchar();
  while (c < 'a' || 'z' < c) c = getchar();
  while ('a' <= c && c <= 'z') s[id] += c, c = getchar();
}
}  // namespace FastIO
using FastIO::read;
using FastIO::reads;
int n, m, all, etot, qtot, h[200005], loc[200005];
struct Edge {
  int to, next;
} a[200005];
struct query {
  int type, r, k, id;
} g[500005 << 1];
bool cmp(const query &x, const query &y) { return x.r < y.r; }
void Add(int x, int y) {
  etot++;
  a[etot].to = y;
  a[etot].next = h[x];
  h[x] = etot;
}
inline int Newnode() { return ++all; }
struct Trie {
  int bj, fail, ch[26];
} tr[200005];
void Insert(int id) {
  int len = s[id].length(), now = 1;
  for (int i = 0; i < len; ++i) {
    int &v = tr[now].ch[s[id][i] - 'a'];
    if (!v) v = Newnode();
    now = v;
  }
  tr[now].bj++;
  loc[id] = now;
}
void Build() {
  queue<int> q;
  for (int i = 0; i < 26; ++i) tr[0].ch[i] = 1;
  q.push(1);
  while (!q.empty()) {
    int x = q.front(), fail = tr[x].fail;
    q.pop();
    Add(fail, x);
    for (int i = 0; i <= 25; ++i) {
      int &y = tr[x].ch[i];
      if (!y)
        y = tr[fail].ch[i];
      else
        tr[y].fail = tr[fail].ch[i], q.push(y);
    }
  }
}
int step, dfn[200005], siz[200005];
long long ans[500005 << 1], c[200005];
void dfs(int x) {
  dfn[x] = ++step;
  siz[x] = 1;
  for (int i = h[x]; i; i = a[i].next) dfs(a[i].to), siz[x] += siz[a[i].to];
}
void add(int x) {
  for (; x <= all; x += x & -x) c[x]++;
}
long long sum(int x) {
  long long s = 0;
  for (; x; x -= x & -x) s += c[x];
  return s;
}
void Modify(int id) {
  int len = s[id].length(), now = 1;
  for (int i = 0; i < len; ++i) {
    now = tr[now].ch[s[id][i] - 'a'];
    add(dfn[now]);
  }
}
int main() {
  n = read();
  m = read();
  all = 1;
  for (int i = 1; i <= n; ++i) reads(i), Insert(i);
  Build();
  dfs(1);
  for (int i = 1; i <= m; ++i) {
    int l = read(), r = read(), k = read();
    g[++qtot] = (query){1, r, k, i};
    if (l > 1) g[++qtot] = (query){-1, l - 1, k, i};
  }
  sort(g + 1, g + qtot + 1, cmp);
  int o = 0;
  for (int i = 1; i <= qtot; ++i) {
    int x = loc[g[i].k];
    while (o + 1 <= g[i].r) Modify(o + 1), o++;
    ans[g[i].id] += g[i].type * (sum(dfn[x] + siz[x] - 1) - sum(dfn[x] - 1));
  }
  for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';
}
