#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int res = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (isdigit(ch)) {
    res = (res << 3) + (res << 1) + ch - '0';
    ch = getchar();
  }
  return res * f;
}
namespace qiqi {
const int N = 1e5 + 5, B = 325;
int n, Q, cnt = 1, ecnt, head[N], fa[N], id[N], inv[N], fail[N], ch[N][26],
          edp[N], dfn[N], siz[N];
long long s[N], ans[N];
char str[N];
struct Edge {
  int v, nxt;
} e[N];
inline void add(int u, int v) {
  e[++ecnt] = (Edge){v, head[u]};
  head[u] = ecnt;
}
struct Qry {
  int k, p, v, id;
  inline bool friend operator<(Qry a, Qry b) { return a.p < b.p; }
} q[N << 1];
struct Opt {
  int k, l, r, id;
  inline bool friend operator<(Opt a, Opt b) { return a.k < b.k; }
} o[N];
struct BIT {
  int a[N];
  inline int lbt(int x) { return x & (-x); }
  inline void upd(int x, int k) {
    while (x <= cnt) {
      a[x] += k;
      x += lbt(x);
    }
  }
  inline int qry(int x) {
    int res = 0;
    while (x) {
      res += a[x];
      x -= lbt(x);
    }
    return res;
  }
} bit, pos;
inline void build() {
  queue<int> q;
  for (int i = (0); i <= (25); ++i)
    if (ch[1][i]) {
      fail[ch[1][i]] = 1;
      q.push(ch[1][i]);
    }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = (0); i <= (25); ++i) {
      if (ch[u][i]) {
        if (!(fail[ch[u][i]] = ch[fail[u]][i])) fail[ch[u][i]] = 1;
        q.push(ch[u][i]);
      } else
        ch[u][i] = ch[fail[u]][i];
    }
  }
  for (int i = (2); i <= (cnt); ++i) add(fail[i], i);
}
void dfs(int u) {
  dfn[u] = ++cnt;
  siz[u] = 1;
  for (int i = head[u], v = e[i].v; i; v = e[i = e[i].nxt].v) {
    dfs(v);
    siz[u] += siz[v];
  }
}
inline int calc(int id, int q) {
  int p = inv[id], x = edp[p], k;
  while (x) {
    pos.upd(dfn[x], 1);
    x = fa[x];
  }
  for (int i = (1); i <= (n); ++i) {
    k = edp[i];
    s[i] = s[i - 1] + pos.qry(dfn[k] + siz[k] - 1) - pos.qry(dfn[k] - 1);
  }
  x = edp[p];
  while (x) {
    pos.upd(dfn[x], -1);
    x = fa[x];
  }
  while (o[q].k == p) {
    ans[o[q].id] = s[o[q].r] - s[o[q].l - 1];
    ++q;
  }
  return q;
}
void main() {
  int l, r, x, p, qry = 0, opt = 0;
  n = read();
  Q = read();
  for (int i = (1); i <= (n); ++i) {
    scanf("%s", str + 1);
    l = strlen(str + 1);
    x = 1;
    if (l > B) inv[id[i] = ++id[0]] = i;
    for (int j = (1); j <= (l); ++j) {
      if (!ch[x][str[j] - 'a']) fa[ch[x][str[j] - 'a'] = ++cnt] = x;
      x = ch[x][str[j] - 'a'];
    }
    edp[i] = x;
  }
  build();
  cnt = 0;
  dfs(1);
  for (int i = (1); i <= (Q); ++i) {
    l = read();
    r = read();
    x = read();
    if (id[x])
      o[++opt] = (Opt){x, l, r, i};
    else {
      q[++qry] = (Qry){x, l - 1, -1, i};
      q[++qry] = (Qry){x, r, 1, i};
    }
  }
  sort(q + 1, q + qry + 1);
  sort(o + 1, o + opt + 1);
  p = 1;
  for (int i = (1); i <= (id[0]); ++i) p = calc(i, p);
  p = 0;
  for (int i = (1); i <= (qry); ++i) {
    while (p < q[i].p) {
      x = edp[++p];
      bit.upd(dfn[x], 1);
      bit.upd(dfn[x] + siz[x], -1);
    }
    x = edp[q[i].k];
    while (x) {
      ans[q[i].id] += q[i].v * bit.qry(dfn[x]);
      x = fa[x];
    }
  }
  for (int i = (1); i <= (Q); ++i) printf("%lld\n", ans[i]);
}
}  // namespace qiqi
int main() {
  qiqi::main();
  return 0;
}
