#include <bits/stdc++.h>   using namespace std;   int n, Q;   string s[100005];   int L[100005], R[100005], K[100005];   long long ans[100005];   vector<int> in[100005], ot[100005], G[100005 << 1];   long long sm[100005];   int last, fa[100005 << 1], tr[100005 << 1][26], len[100005 << 1], tot,       f[100005 << 1], sa[100005 << 1], c[100005 << 1], pos[100005];   void ins(int c) {     if (tr[last][c]) {       int p = last, q;       if (len[q = tr[p][c]] != len[p] + 1) {         int v = ++tot;         memcpy(tr[v], tr[q], sizeof tr[q]), fa[v] = fa[q], len[v] = len[p] + 1;         for (; p != -1 && tr[p][c] == q; p = fa[p]) tr[p][c] = v;         fa[q] = v;       }       last = tr[last][c];       return;     }     int u = ++tot, p = last, q;     len[last = u] = len[p] + 1;     for (; p != -1 && tr[p][c] == 0; p = fa[p]) tr[p][c] = u;     if (p == -1)       fa[u] = 0;     else if (len[q = tr[p][c]] == len[p] + 1)       fa[u] = q;     else {       int v = ++tot;       memcpy(tr[v], tr[q], sizeof tr[q]), fa[v] = fa[q], len[v] = len[p] + 1;       for (; p != -1 && tr[p][c] == q; p = fa[p]) tr[p][c] = v;       fa[q] = fa[u] = v;     }   }   int st[100005 << 1], ed[100005 << 1], tim, ST[100005 << 1], ED[100005 << 1];   void dfs0(int u) {     st[u] = ++tim;     for (int v : G[u]) dfs0(v);     ed[u] = tim;   }   int bl[100005], sb[100005 << 1], id[100005 << 1];   int main() {     ios::sync_with_stdio(false);     cin >> n >> Q;     for (int i = (1), LIM = (n); i <= LIM; i++) cin >> s[i];     for (int i = (1), LIM = (Q); i <= LIM; i++) {       cin >> L[i] >> R[i] >> K[i];       if (s[K[i]].length() <= 305)         ot[R[i]].push_back(i), in[L[i] - 1].push_back(i);       else         G[K[i]].push_back(i);     }     for (int i = (1), LIM = (n); i <= LIM; i++)       if (!G[i].empty()) {         last = 0, fa[0] = -1;         for (int j = (0), LIM = (s[i].length() - 1); j <= LIM; j++)           ins(s[i][j] -  a ), f[last]++;         for (int j = (0), LIM = (tot); j <= LIM; j++) c[j] = 0;         for (int j = (0), LIM = (tot); j <= LIM; j++) c[len[j]]++;         for (int j = (1), LIM = (tot); j <= LIM; j++) c[j] += c[j - 1];         for (int j = (0), LIM = (tot); j <= LIM; j++) sa[--c[len[j]]] = j;         for (int j = (tot), LIM = (1); j >= LIM; j--) {           int u = sa[j];           f[fa[u]] += f[u];         }         for (int j = (1), LIM = (n); j <= LIM; j++) {           int u = 0, L = 0;           for (int k = (0), LIM = (s[j].length() - 1); k <= LIM; k++) {             int v = s[j][k] -  a ;             for (; u != -1 && !tr[u][v]; u = fa[u])               ;             if (u == -1)               u = 0, L = 0;             else               L = min(L + 1, len[u] + 1), u = tr[u][v];           }           sm[j] = sm[j - 1] + (L == s[j].length()) * (f[u]);         }         for (int v : G[i]) ans[v] = sm[R[v]] - sm[L[v] - 1];         G[i].clear();         memset(tr, 0, sizeof(tr[0]) * (tot + 1));         for (int j = (0), LIM = (tot); j <= LIM; j++) f[j] = 0;         tot = 0;       }     last = 0;     fa[0] = -1;     for (int i = (1), LIM = (n); i <= LIM; i++) {       last = 0;       for (int j = (0), LIM = (s[i].length() - 1); j <= LIM; j++)         ins(s[i][j] -  a );       pos[i] = last;     }     for (int i = (1), LIM = (tot); i <= LIM; i++) G[fa[i]].push_back(i);     dfs0(0);     for (int i = (1), LIM = (tim); i <= LIM; i++)       id[i] = i / 305, ST[id[i]] = ST[id[i]] ? ST[id[i]] : i, ED[id[i]] = i;     ;     for (int i = (1), LIM = (n); i <= LIM; i++) {       int u = st[pos[i]], v = ed[pos[i]];       for (int j = (id[u] + 1), LIM = (id[v] - 1); j <= LIM; j++) bl[j]++;       if (id[u] == id[v]) {         for (int j = (u), LIM = (v); j <= LIM; j++) sb[j]++;       } else {         for (int j = (u), LIM = (ED[id[u]]); j <= LIM; j++) sb[j]++;         for (int j = (ST[id[v]]), LIM = (v); j <= LIM; j++) sb[j]++;       }       for (int p : in[i]) {         int u = 0, t = K[p];         for (int j = (0), LIM = (s[t].length() - 1); j <= LIM; j++) {           v = s[t][j] -  a ;           for (; u != -1 && !tr[u][v]; u = fa[u])             ;           if (u == -1)             u = 0;           else             u = tr[u][v];           ans[p] -= sb[st[u]] + bl[id[st[u]]];         }       }       for (int p : ot[i]) {         int u = 0, t = K[p];         for (int j = (0), LIM = (s[t].length() - 1); j <= LIM; j++) {           v = s[t][j] -  a ;           for (; u != -1 && !tr[u][v]; u = fa[u])             ;           if (u == -1)             u = 0;           else             u = tr[u][v];           ans[p] += sb[st[u]] + bl[id[st[u]]];         }       }     }     for (int i = (1), LIM = (Q); i <= LIM; i++) printf( %lld n , ans[i]);   }