#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline T read(register T& t) {     register T f = 1;     register char ch = getchar();     t = 0;     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -f;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) t = t * 10 + ch -  0 , ch = getchar();     t *= f;     return t;   }   template <typename T, typename... Args>   inline void read(T& t, Args&... args) {     read(t);     read(args...);   }   const long long p = 998244353;   inline long long power(register long long x, register long long k = p - 2) {     register long long re = 1;     for (; k; k >>= 1, x = x * x % p)       if (k & 1) re = re * x % p;     return re;   }   int n, Q;   char* c[100005];   char _c[100005];   int len[100005];   int B;   struct Ask {     int r, k, id, col;   };   vector<Ask> bf[100005], buf;   bool cmp(Ask a, Ask b) { return a.r < b.r; }   int trie[200005][26];   int tot;   inline int Insert(char* c) {     int p = 0;     int len = strlen(c);     for (int i = 0; i < len; i++) {       int x = c[i] -  a ;       if (!trie[p][x]) trie[p][x] = ++tot;       p = trie[p][x];     }     return p;   }   int fail[200005];   void build_fail() {     queue<int> q;     for (int i = 0; i < 26; i++)       if (trie[0][i]) q.push(trie[0][i]);     while (q.size()) {       int p = q.front();       q.pop();       for (int i = 0; i < 26; i++)         if (trie[p][i])           fail[trie[p][i]] = trie[fail[p]][i], q.push(trie[p][i]);         else           trie[p][i] = trie[fail[p]][i];     }   }   int head[200005], ne[400005], to[400005], total = 1;   inline void add(int a, int b) {     total++;     to[total] = b;     ne[total] = head[a];     head[a] = total;   }   void build_tree() {     for (int i = 1; i <= tot; i++) add(fail[i], i);   }   int M;   int cnt[200005];   void build_cnt(int now) {     for (int i = head[now]; i; i = ne[i])       build_cnt(to[i]), cnt[now] += cnt[to[i]];   }   int id[200005];   long long Ans[200005];   int dfn[200005], dfn_top;   int Size[200005];   void build_dfn(int now) {     Size[now] = 1;     dfn[now] = ++dfn_top;     for (int i = head[now]; i; i = ne[i])       build_dfn(to[i]), Size[now] += Size[to[i]];   }   struct BIT {     long long c[200005];     inline void add(int x, int val) {       for (; x <= 200000; x += (x & -x)) c[x] += val;     }     inline long long ask(int x) {       long long re = 0;       for (; x; x -= (x & -x)) re += c[x];       return re;     }   } T;   int main() {     read(n, Q);     for (int i = 1; i <= n; i++) {       scanf( %s , _c);       len[i] = strlen(_c);       c[i] = new char[len[i] + 10];       for (int j = 0; j <= len[i]; j++) c[i][j] = _c[j];       M += len[i];     }     B = 1.0 * M / (sqrt(1.0 * Q * log2(M)) + 1);     for (int i = 1; i <= n; i++) id[i] = Insert(c[i]);     build_fail();     build_tree();     for (int i = 1; i <= Q; i++) {       int l, r, k;       read(l, r, k);       if (len[k] > B) {         bf[k].push_back((Ask){l - 1, k, i, -1});         bf[k].push_back((Ask){r, k, i, 1});       } else {         buf.push_back((Ask){l - 1, k, i, -1});         buf.push_back((Ask){r, k, i, 1});       }     }     for (int i = 1; i <= n; i++)       if (bf[i].size()) {         sort(bf[i].begin(), bf[i].end(), cmp);         for (int j = 0; j <= tot; j++) cnt[j] = 0;         int p = 0;         for (int j = 0; j < len[i]; j++) {           int x = c[i][j] -  a ;           p = trie[p][x];           cnt[p]++;         }         build_cnt(0);         int k = 0;         while (bf[i][k].r == 0 && k + 1 != bf[i].size()) k++;         static long long pre[200005];         for (int j = 1; j <= n; j++) {           pre[j] = pre[j - 1] + cnt[id[j]];           while (k != bf[i].size() && bf[i][k].r == j)             Ans[bf[i][k].id] += 1ll * bf[i][k].col * pre[j], k++;         }       }     sort(buf.begin(), buf.end(), cmp);     int k = 0;     build_dfn(0);     while (k != buf.size() && buf[k].r == 0) k++;     for (int i = 1; i <= n; i++) {       int tp = id[i];       T.add(dfn[tp], 1);       T.add(dfn[tp] + Size[tp], -1);       while (k != buf.size() && buf[k].r == i) {         long long ans = 0;         int p = 0;         for (int j = 0; j < len[buf[k].k]; j++) {           int x = c[buf[k].k][j] -  a ;           p = trie[p][x];           ans += T.ask(dfn[p]);         }         Ans[buf[k].id] += 1ll * buf[k].col * ans;         k++;       }     }     for (int i = 1; i <= Q; i++) printf( %lld n , Ans[i]);     return 0;   }