#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAX = 1000005;
int ans[MAX], rmq[20][MAX], r[21][MAX], pos[MAX], size[MAX], lg[MAX];
pair<int, pair<pair<int, int>, pair<int, int> > > qq[MAX];
pair<pair<int, int>, int> p[MAX];
int lcp(int x, int y)
{
int lim = min(size[x], size[y]);
int ans = 0;
for (int i = lg[lim]; i >= 0; i--)
if (r[i][x] == r[i][y])
{
x += (1 << i);
y += (1 << i);
ans |= (1 << i);
}
return ans;
}
int fen[MAX], a[MAX];
void add(int p, int val)
{
for (p++; p < MAX; p += p & -p)
fen[p] += val;
}
int get(int p)
{
int ans = 0;
for (; p > 0; p -= p & -p)
ans += fen[p];
return ans;
}
char s[MAX], t[MAX];
int main()
{
for (int i = 2; i < MAX; i++)
lg[i] = lg[i / 2] + 1;
int n, q;
scanf("%d %d", &n, &q);
int last = 0;
for (int i = 0; i < n; i++)
{
scanf("%s", t);
int m = strlen(t);
pos[i] = last;
for (int j = 0; j < m; j++)
{
size[last] = m;
s[last++] = t[j];
}
size[last] = m;
s[last++] = '#';
}
pos[n] = last;
n = last;
for (int i = 0; i < n; i++)
if (s[i] == '#')
r[0][i] = i + 1000;
else
r[0][i] = s[i];
for (int i = 1; i <= 20; i++)
{
for (int j = 0; j < n; j++)
if (j + (1 << (i - 1)) >= n)
p[j] = make_pair(make_pair(r[i - 1][j], -1), j);
else
p[j] = make_pair(make_pair(r[i - 1][j], r[i - 1][j + (1 << (i - 1))]), j);
sort(p, p + n);
for (int j = 0; j < n; j++)
if (j > 0 && p[j].first == p[j - 1].first)
r[i][p[j].second] = r[i][p[j - 1].second];
else
r[i][p[j].second] = j + 1;
}
for (int i = 0; i < n; i++)
{
a[i] = r[20][i] - 1;
p[i] = make_pair(make_pair(a[i], -1), i);
}
sort(p, p + n);
for (int i = 0; i < n - 1; i++)
rmq[0][i] = lcp(p[i].second, p[i + 1].second);
for (int i = 1; i < 20; i++)
for (int j = 0; j < n - 1; j++)
if (j + (1 << (i - 1)) >= n - 1)
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 ind = 0; ind < q; ind++)
{
int l, r, k;
scanf("%d %d %d", &l, &r, &k);
l--;
k--;
l = pos[l];
r = pos[r];
int s = pos[k];
int e = pos[k + 1] - 1;
int lo = a[s], hi = a[s] + 1;
for (int i = 19; i >= 0; i--)
{
if (lo - (1 << i) >= 0 && rmq[i][lo - (1 << i)] >= e - s)
lo -= (1 << i);
if (hi + (1 << i) <= n && rmq[i][hi - 1] >= e - s)
hi += (1 << i);
}
qq[t++] = make_pair(lo, make_pair(make_pair(l, r), make_pair(-1, ind)));
qq[t++] = make_pair(hi, make_pair(make_pair(l, r), make_pair(1, ind)));
}
sort(qq, qq + t);
int pos = 0;
for (int i = 0; i < t; i++)
{
while (pos < n && p[pos].first.first < qq[i].first)
add(p[pos++].second, 1);
ans[qq[i].second.second.second] += qq[i].second.second.first * (get(qq[i].second.first.second) - get(qq[i].second.first.first));
}
for (int i = 0; i < q; i++)
printf("%d\n", ans[i]);
return 0;
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxjc3RkaW8+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBNQVggPSAxMDAwMDA1OwppbnQgYW5zW01BWF0sIHJtcVsyMF1bTUFYXSwgclsyMV1bTUFYXSwgcG9zW01BWF0sIHNpemVbTUFYXSwgbGdbTUFYXTsKcGFpcjxpbnQsIHBhaXI8cGFpcjxpbnQsIGludD4sIHBhaXI8aW50LCBpbnQ+ID4gPiBxcVtNQVhdOwpwYWlyPHBhaXI8aW50LCBpbnQ+LCBpbnQ+IHBbTUFYXTsKaW50IGxjcChpbnQgeCwgaW50IHkpCnsKCWludCBsaW0gPSBtaW4oc2l6ZVt4XSwgc2l6ZVt5XSk7CglpbnQgYW5zID0gMDsKCWZ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