#include <bits/stdc++.h>
using namespace std;
const int N = 200500, M = 500005;
int lowbit(int x) { return x & (-x); }
struct Edge {
  int next, to;
} edge[N];
int head[N], en;
void add(int u, int v) {
  edge[++en].to = v;
  edge[en].next = head[u];
  head[u] = en;
}
int c[N], con[N][26], fail[N], dfn[N], tot, dft;
int n, m, root[N], last[N];
string s[N];
queue<int> q;
struct Ques {
  int pos, k, num, ans;
} qu[M * 2];
bool cmp1(Ques i, Ques j) { return i.pos < j.pos; }
bool cmp2(Ques i, Ques j) { return i.num < j.num; }
void change(int xx) {
  for (xx; xx <= dft; xx += lowbit(xx)) c[xx]++;
}
int query(int xx) {
  int ans = 0;
  for (xx; xx > 0; xx -= lowbit(xx)) ans += c[xx];
  return ans;
}
void dfs(int u) {
  dfn[u] = ++dft;
  for (int i = head[u]; i; i = edge[i].next) {
    int v = edge[i].to;
    dfs(v);
  }
  last[u] = dft;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    int now = 0;
    for (int j = 0; j < s[i].size(); j++) {
      if (!con[now][s[i][j] - 'a']) con[now][s[i][j] - 'a'] = ++tot;
      now = con[now][s[i][j] - 'a'];
    }
    root[i] = now;
  }
  for (int i = 0; i < 26; i++)
    if (con[0][i]) q.push(con[0][i]);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    add(fail[u], u);
    for (int i = 0; i < 26; i++) {
      if (con[u][i]) {
        fail[con[u][i]] = con[fail[u]][i];
        q.push(con[u][i]);
      } else
        con[u][i] = con[fail[u]][i];
    }
  }
  dfs(0);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &qu[i * 2 - 1].pos, &qu[i * 2].pos, &qu[i * 2].k);
    qu[i * 2 - 1].k = qu[i * 2].k;
    qu[i * 2 - 1].pos--;
    qu[i * 2 - 1].num = i * 2 - 1;
    qu[i * 2].num = i * 2;
  }
  sort(qu + 1, qu + m * 2 + 1, cmp1);
  int j = 1;
  for (j; j <= m * 2 && !qu[j].pos; j++)
    ;
  for (int i = 1; i <= n; i++) {
    int now = 0;
    for (int k = 0; k < s[i].size(); k++) {
      now = con[now][s[i][k] - 'a'];
      change(dfn[now]);
    }
    for (j; j <= m * 2 && qu[j].pos <= i; j++)
      qu[j].ans = query(last[root[qu[j].k]]) - query(dfn[root[qu[j].k]] - 1);
  }
  sort(qu + 1, qu + m * 2 + 1, cmp2);
  for (int i = 1; i <= m; i++)
    printf("%d\n", qu[i * 2].ans - qu[i * 2 - 1].ans);
  return 0;
}
