#include <bits/stdc++.h>
using namespace std;
const int chr = 26;
struct node {
  int nxt[chr];
  node() { memset(nxt, -1, sizeof(nxt)); }
};
int suffix[250010], indx, ES[250010];
int NES[250010], ed[250010], E[250010];
long long fr[250010], cnt[250010];
node T[250010];
string S[250010];
struct Aho_Corasick {
  int root;
  vector<int> ids;
  Aho_Corasick() {
    root = ++indx;
    suffix[root] = root;
    ES[root] = root;
    NES[root] = cnt[root] = 0;
    T[root] = node();
  }
  void insert(int p, int f) {
    int now = root;
    for (int i = 0; i < S[p].size(); i++) {
      int id = S[p][i] - 'a';
      if (T[now].nxt[id] == -1) {
        T[now].nxt[id] = ++indx;
        T[indx] = node();
      }
      now = T[now].nxt[id];
    }
    if (f) ed[p] = now;
    E[now] = p;
    cnt[now]++;
  }
  void reverse_link() {
    queue<int> q;
    for (int i = 0; i < chr; i++) {
      if (T[root].nxt[i] != -1) {
        q.push(T[root].nxt[i]);
        suffix[T[root].nxt[i]] = root;
        if (cnt[T[root].nxt[i]]) NES[T[root].nxt[i]] = cnt[T[root].nxt[i]];
        ES[T[root].nxt[i]] = root;
      } else
        T[root].nxt[i] = root;
    }
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = 0; i < chr; i++) {
        int v = T[u].nxt[i];
        if (v == -1) {
          T[u].nxt[i] = T[suffix[u]].nxt[i];
          continue;
        }
        suffix[v] = T[suffix[u]].nxt[i];
        q.push(v);
        if (cnt[v])
          NES[v] = cnt[v] + NES[suffix[v]];
        else
          NES[v] = NES[suffix[v]];
        if (cnt[suffix[v]])
          ES[v] = suffix[v];
        else
          ES[v] = ES[suffix[v]];
      }
    }
  }
  long long search(string s, int f) {
    if (f) {
      for (int i = 0; i < ids.size(); i++) fr[ids[i]] = 0;
      ids.clear();
    }
    long long sm = 0;
    int now = root;
    for (int i = 0; i < s.size(); i++) {
      int id = s[i] - 'a';
      now = T[now].nxt[id];
      sm += NES[now];
      if (f) {
        if (cnt[now]) {
          fr[now]++;
          ids.push_back(now);
        }
        for (int xx = ES[now]; xx > root; xx = ES[xx]) {
          fr[xx]++;
          ids.push_back(xx);
        }
      }
    }
    return sm;
  }
};
Aho_Corasick AC[505], AH;
vector<int> q[250010];
long long res[250010], ans[250010];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc, cs = 1, n, Q, L, R, k;
  int sq;
  cin >> n >> Q;
  sq = sqrt(n) + 2;
  for (int i = 1; i <= n; i++) {
    cin >> S[i];
    AH.insert(i, 1);
    AC[i / sq].insert(i, 0);
  }
  for (int i = 0; i <= sq; i++) AC[i].reverse_link();
  AH.reverse_link();
  for (int i = 1; i <= Q; i++) {
    cin >> L >> R >> k;
    q[k].push_back(L);
    q[k].push_back(R);
    q[k].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    if (!q[i].size()) continue;
    AH.search(S[i], 1);
    for (int j = 0; j <= sq; j++) res[j] = AC[j].search(S[i], 0);
    for (int j = 0; j < q[i].size(); j += 3) {
      L = q[i][j];
      R = q[i][j + 1];
      int id = q[i][j + 2];
      int l = L / sq + 1;
      int h = R / sq - 1;
      if (l <= h) {
        for (int xx = l; xx <= h; xx++) ans[id] += res[xx];
        for (int xx = L; xx < l * sq; xx++) ans[id] += fr[ed[xx]];
        for (int xx = sq * (h + 1); xx <= R; xx++) ans[id] += fr[ed[xx]];
      } else {
        for (int xx = L; xx <= R; xx++) ans[id] += fr[ed[xx]];
      }
    }
  }
  for (int i = 1; i <= Q; i++) cout << ans[i] << '\n';
  return 0;
}

### Prompt

Develop a solution in cpp to the problem described below:

Duff is mad at her friends. That's why she sometimes makes Malek to take candy from one of her friends for no reason!

<image>

She has n friends. Her i-th friend's name is si (their names are not necessarily unique). q times, she asks Malek to take candy from her friends. She's angry, but also she acts with rules. When she wants to ask Malek to take candy from one of her friends, like k, she chooses two numbers l and r and tells Malek to take exactly <image> candies from him/her, where occur(t, s) is the number of occurrences of string t in s.

Malek is not able to calculate how many candies to take in each request from Duff. That's why she asked for your help. Please tell him how many candies to take in each request.

Input

The first line of input contains two integers n and q (1 ≤ n, q ≤ 105).

The next n lines contain the names. i-th of them contains an string si, consisting of lowercase English letters (<image>).

The next q lines contain the requests. Each of them contains three integers, l, r and k (says that Malek should take <image> candies from Duff's k-th friend).

Output

Print the answer to each request in one line.

Examples

Input

5 5
a
ab
abab
ababab
b
1 5 4
3 5 4
1 5 2
1 5 3
1 4 1


Output

12
6
3
7
1

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int chr = 26;
struct node {
  int nxt[chr];
  node() { memset(nxt, -1, sizeof(nxt)); }
};
int suffix[250010], indx, ES[250010];
int NES[250010], ed[250010], E[250010];
long long fr[250010], cnt[250010];
node T[250010];
string S[250010];
struct Aho_Corasick {
  int root;
  vector<int> ids;
  Aho_Corasick() {
    root = ++indx;
    suffix[root] = root;
    ES[root] = root;
    NES[root] = cnt[root] = 0;
    T[root] = node();
  }
  void insert(int p, int f) {
    int now = root;
    for (int i = 0; i < S[p].size(); i++) {
      int id = S[p][i] - 'a';
      if (T[now].nxt[id] == -1) {
        T[now].nxt[id] = ++indx;
        T[indx] = node();
      }
      now = T[now].nxt[id];
    }
    if (f) ed[p] = now;
    E[now] = p;
    cnt[now]++;
  }
  void reverse_link() {
    queue<int> q;
    for (int i = 0; i < chr; i++) {
      if (T[root].nxt[i] != -1) {
        q.push(T[root].nxt[i]);
        suffix[T[root].nxt[i]] = root;
        if (cnt[T[root].nxt[i]]) NES[T[root].nxt[i]] = cnt[T[root].nxt[i]];
        ES[T[root].nxt[i]] = root;
      } else
        T[root].nxt[i] = root;
    }
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = 0; i < chr; i++) {
        int v = T[u].nxt[i];
        if (v == -1) {
          T[u].nxt[i] = T[suffix[u]].nxt[i];
          continue;
        }
        suffix[v] = T[suffix[u]].nxt[i];
        q.push(v);
        if (cnt[v])
          NES[v] = cnt[v] + NES[suffix[v]];
        else
          NES[v] = NES[suffix[v]];
        if (cnt[suffix[v]])
          ES[v] = suffix[v];
        else
          ES[v] = ES[suffix[v]];
      }
    }
  }
  long long search(string s, int f) {
    if (f) {
      for (int i = 0; i < ids.size(); i++) fr[ids[i]] = 0;
      ids.clear();
    }
    long long sm = 0;
    int now = root;
    for (int i = 0; i < s.size(); i++) {
      int id = s[i] - 'a';
      now = T[now].nxt[id];
      sm += NES[now];
      if (f) {
        if (cnt[now]) {
          fr[now]++;
          ids.push_back(now);
        }
        for (int xx = ES[now]; xx > root; xx = ES[xx]) {
          fr[xx]++;
          ids.push_back(xx);
        }
      }
    }
    return sm;
  }
};
Aho_Corasick AC[505], AH;
vector<int> q[250010];
long long res[250010], ans[250010];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc, cs = 1, n, Q, L, R, k;
  int sq;
  cin >> n >> Q;
  sq = sqrt(n) + 2;
  for (int i = 1; i <= n; i++) {
    cin >> S[i];
    AH.insert(i, 1);
    AC[i / sq].insert(i, 0);
  }
  for (int i = 0; i <= sq; i++) AC[i].reverse_link();
  AH.reverse_link();
  for (int i = 1; i <= Q; i++) {
    cin >> L >> R >> k;
    q[k].push_back(L);
    q[k].push_back(R);
    q[k].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    if (!q[i].size()) continue;
    AH.search(S[i], 1);
    for (int j = 0; j <= sq; j++) res[j] = AC[j].search(S[i], 0);
    for (int j = 0; j < q[i].size(); j += 3) {
      L = q[i][j];
      R = q[i][j + 1];
      int id = q[i][j + 2];
      int l = L / sq + 1;
      int h = R / sq - 1;
      if (l <= h) {
        for (int xx = l; xx <= h; xx++) ans[id] += res[xx];
        for (int xx = L; xx < l * sq; xx++) ans[id] += fr[ed[xx]];
        for (int xx = sq * (h + 1); xx <= R; xx++) ans[id] += fr[ed[xx]];
      } else {
        for (int xx = L; xx <= R; xx++) ans[id] += fr[ed[xx]];
      }
    }
  }
  for (int i = 1; i <= Q; i++) cout << ans[i] << '\n';
  return 0;
}

```