#include <bits/stdc++.h>   using namespace std;   const double PI = acos(-1);   template <class T>   T sqr(T x) {     return x * x;   }   template <class T>   T alex(T x) {     return (x > 0) ? x : -x;   }   template <class T>   void read(T& x) {     x = 0;     T f = 1;     char ch = getchar();     while ((ch < 48 || ch > 57) && ch != 45) ch = getchar();     if (ch == 45) f = -1, ch = getchar();     while (ch >= 48 && ch <= 57) x = x * 10 + ch - 48, ch = getchar();     x *= f;   }   int n, q;   char s[200005];   int t[200005 << 1];   int st[200005], ed[200005], len;   int tmp[200005 << 1], sa[200005 << 1], rnk[200005 << 1], sum[200005 << 1];   bool cmp(int* tmp, int x, int y, int l) {     return tmp[x] == tmp[y] && tmp[x + l] == tmp[y + l];   }   void getsa(int n, int m) {     memset(sum, 0, sizeof(sum));     for (int i = (1); i <= (n); i++) sum[rnk[i] = t[i]]++;     for (int i = (1); i <= (m); i++) sum[i] += sum[i - 1];     for (int i = (n); i >= (1); i--) sa[sum[rnk[i]]--] = i;     for (int j = 1; j <= n; j <<= 1) {       int p = 0;       for (int i = (n); i >= (n - j + 1); i--) tmp[++p] = i;       for (int i = (1); i <= (n); i++)         if (sa[i] > j) tmp[++p] = sa[i] - j;       memset(sum, 0, sizeof(sum));       for (int i = (1); i <= (n); i++) sum[rnk[tmp[i]]]++;       for (int i = (1); i <= (max(n, m)); i++) sum[i] += sum[i - 1];       for (int i = (n); i >= (1); i--) sa[sum[rnk[tmp[i]]]--] = tmp[i];       for (int i = (1); i <= (n); i++) tmp[i] = rnk[i];       p = 1;       rnk[sa[1]] = 1;       for (int i = (2); i <= (n); i++)         rnk[sa[i]] = p += !cmp(tmp, sa[i - 1], sa[i], j);     }   }   int height[200005 << 1][20];   void getheight(int n) {     for (int i = (1); i <= (n); i++) {       int l = max(height[rnk[i - 1]][0] - 1, 0), j = sa[rnk[i] - 1];       while (i + l <= n && j + l <= n && t[i + l] == t[j + l]) l++;       height[rnk[i]][0] = l;     }     for (int j = (1); j < (20); j++)       for (int i = (1); i <= (n - (1 << j) + 1); i++)         height[i][j] = min(height[i][j - 1], height[i + (1 << (j - 1))][j - 1]);   }   int _log2[200005 << 1];   int LCP(int x, int y) {     if (x == y) return n - sa[x] + 1;     if (x > y) swap(x, y);     x++;     int j = _log2[y - x + 1];     return min(height[x][j], height[y - (1 << j) + 1][j]);   }   struct Node {     int idx, l, sign;     Node() {}     Node(int idx, int l, int sign) : idx(idx), l(l), sign(sign) {}   };   int sum_bit[200005 << 1];   int lowbit(int x) { return x & (-x); }   void insert(int v, int x) {     for (int i = x; i <= len; i += lowbit(i)) sum_bit[i]++;   }   int ask(int l, int r) {     int res = 0;     for (int i = l - 1; i; i -= lowbit(i)) res -= sum_bit[i];     for (int i = r; i; i -= lowbit(i)) res += sum_bit[i];     return res;   }   vector<Node> que[200005];   int ans[500005];   void insert(int idx) {     for (int i = (st[idx]); i <= (ed[idx]); i++) insert(1, rnk[i]);   }   int findl(int l, int r, int pos, int val) {     while (l <= r) {       int mid = (l + r) >> 1;       if (LCP(mid, pos) >= val)         r = mid - 1;       else         l = mid + 1;     }     return l;   }   int findr(int l, int r, int pos, int val) {     while (l <= r) {       int mid = (l + r) >> 1;       if (LCP(pos, mid) >= val)         l = mid + 1;       else         r = mid - 1;     }     return r;   }   int query(int idx) {     int len2 = ed[idx] - st[idx] + 1;     int l = findl(1, rnk[st[idx]] - 1, rnk[st[idx]], len2);     int r = findr(rnk[st[idx]] + 1, len, rnk[st[idx]], len2);     return ask(l, r);   }   int main() {     read(n);     read(q);     int m = 26;     len = 0;     for (int i = (1); i <= (n); i++) {       scanf( %s , s + 1);       int l = strlen(s + 1);       st[i] = len + 1;       for (int k = (1); k <= (l); k++) t[++len] = s[k] -  a  + 1;       ed[i] = len;       t[++len] = ++m;     }     getsa(len, m);     getheight(len);     for (int i = (0); i <= (len); i++) _log2[i] = log2(i);     for (int i = (1); i <= (q); i++) {       int l, r, k;       read(l);       read(r);       read(k);       que[l - 1].push_back(Node(i, k, -1));       que[r].push_back(Node(i, k, 1));     }     for (int i = (1); i <= (n); i++) {       insert(i);       for (int k = (0); k < (que[i].size()); k++)         if (que[i][k].sign == -1)           ans[que[i][k].idx] -= query(que[i][k].l);         else           ans[que[i][k].idx] += query(que[i][k].l);     }     for (int i = (1); i <= (q); i++) printf( %d n , ans[i]);     return 0;   }