#include <bits/stdc++.h>
using namespace std;
const int maxn = 100011;
vector<int> C[maxn];
int trans[maxn][26], fail[maxn];
int cur;
vector<int> q[maxn], End[maxn];
long long ANS[maxn];
bool flag[maxn];
int n, K;
char ch[maxn];
int _q[maxn];
void AC_build() {
  for (int i = 0; i <= 100000; i++) {
    q[i].clear();
    End[i].clear();
  }
  K = (int)sqrt(n);
  cur = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%s", ch + 1);
    int l = strlen(ch + 1);
    flag[i] = (l > K);
    int p = 0;
    q[0].push_back(i);
    for (int j = 1; j <= l; j++) {
      int k = ch[j] - 'a';
      if (!trans[p][k]) trans[p][k] = ++cur;
      p = trans[p][k];
      q[p].push_back(i);
    }
    End[p].push_back(i);
  }
  int head, tail;
  head = tail = 0;
  for (int i = 0; i < 26; i++)
    if (trans[0][i]) _q[tail++] = trans[0][i];
  while (head < tail) {
    int u = _q[head++];
    for (int i = 0; i < 26; i++) {
      int son = trans[u][i], p = fail[u];
      if (son) {
        _q[tail++] = son;
        while (p && !trans[p][i]) p = fail[p];
        fail[son] = trans[p][i];
      }
    }
  }
  for (int i = 0; i <= cur; i++) C[i].clear();
  for (int i = 1; i <= cur; i++) C[fail[i]].push_back(i);
}
struct QQ {
  int u, l, r, k;
  QQ(int _u, int _l, int _r, int _k) {
    u = _u;
    l = _l;
    r = _r;
    k = _k;
  }
  QQ() {}
};
bool cmp(QQ a, QQ b) { return a.k < b.k; }
long long sum[maxn];
vector<QQ> ask;
vector<QQ> que[maxn];
long long dfs1(int u, int t) {
  long long tmp = 0;
  for (int i = 0; i < (int)q[u].size(); i++)
    if (q[u][i] == t) tmp++;
  for (int i = 0; i < (int)C[u].size(); i++) {
    int v = C[u][i];
    tmp += dfs1(v, t);
  }
  for (int i = 0; i < (int)End[u].size(); i++) sum[End[u][i]] += tmp;
  return tmp;
}
void solve1() {
  for (int i = 1; i <= n; i++) que[i].clear();
  for (int i = 0; i < (int)ask.size(); i++) {
    QQ nw = ask[i];
    if (flag[nw.k]) que[nw.k].push_back(nw);
  }
  for (int i = 1; i <= n; i++) {
    if (flag[i]) {
      memset(sum, 0, sizeof(sum));
      dfs1(0, i);
      for (int j = 2; j <= n; j++) sum[j] += sum[j - 1];
      for (int j = 0; j < (int)que[i].size(); j++) {
        QQ nw = que[i][j];
        ANS[nw.u] = sum[nw.r] - sum[nw.l - 1];
      }
    }
  }
}
long long sq[maxn], a[maxn];
void sqAdd(int i, long long x) {
  for (; i % K != 0 && i <= n; i++) a[i] += x;
  for (; i <= n; i += K) sq[i / K] += x;
}
long long sqGet(int i) { return a[i] + sq[i / K]; }
void dfs2(int u) {
  for (int i = 0; i < (int)End[u].size(); i++) sqAdd(End[u][i], 1ll);
  for (int i = 0; i < (int)q[u].size(); i++) {
    int ii = q[u][i];
    for (int j = 0; j < (int)que[ii].size(); j++) {
      QQ nw = que[ii][j];
      ANS[nw.u] += sqGet(nw.r) - sqGet(nw.l - 1);
    }
  }
  for (int i = 0; i < (int)C[u].size(); i++) dfs2(C[u][i]);
  for (int i = 0; i < (int)End[u].size(); i++) sqAdd(End[u][i], -1ll);
}
void solve2() {
  for (int i = 1; i <= n; i++) que[i].clear();
  for (int i = 0; i < (int)ask.size(); i++) {
    QQ nw = ask[i];
    if (!flag[nw.k]) que[nw.k].push_back(nw);
  }
  memset(sq, 0, sizeof(sq));
  memset(a, 0, sizeof(a));
  dfs2(0);
}
int main() {
  int q;
  cin >> n >> q;
  AC_build();
  ask.clear();
  for (int qq = 1; qq <= q; qq++) {
    int l, r, k;
    scanf("%d%d%d", &l, &r, &k);
    ask.push_back(QQ(qq, l, r, k));
  }
  sort(ask.begin(), ask.end(), cmp);
  solve1();
  solve2();
  for (int i = 1; i <= q; i++) printf("%I64d\n", ANS[i]);
  return 0;
}

### Prompt

Construct a cpp code solution to the problem outlined:

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 maxn = 100011;
vector<int> C[maxn];
int trans[maxn][26], fail[maxn];
int cur;
vector<int> q[maxn], End[maxn];
long long ANS[maxn];
bool flag[maxn];
int n, K;
char ch[maxn];
int _q[maxn];
void AC_build() {
  for (int i = 0; i <= 100000; i++) {
    q[i].clear();
    End[i].clear();
  }
  K = (int)sqrt(n);
  cur = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%s", ch + 1);
    int l = strlen(ch + 1);
    flag[i] = (l > K);
    int p = 0;
    q[0].push_back(i);
    for (int j = 1; j <= l; j++) {
      int k = ch[j] - 'a';
      if (!trans[p][k]) trans[p][k] = ++cur;
      p = trans[p][k];
      q[p].push_back(i);
    }
    End[p].push_back(i);
  }
  int head, tail;
  head = tail = 0;
  for (int i = 0; i < 26; i++)
    if (trans[0][i]) _q[tail++] = trans[0][i];
  while (head < tail) {
    int u = _q[head++];
    for (int i = 0; i < 26; i++) {
      int son = trans[u][i], p = fail[u];
      if (son) {
        _q[tail++] = son;
        while (p && !trans[p][i]) p = fail[p];
        fail[son] = trans[p][i];
      }
    }
  }
  for (int i = 0; i <= cur; i++) C[i].clear();
  for (int i = 1; i <= cur; i++) C[fail[i]].push_back(i);
}
struct QQ {
  int u, l, r, k;
  QQ(int _u, int _l, int _r, int _k) {
    u = _u;
    l = _l;
    r = _r;
    k = _k;
  }
  QQ() {}
};
bool cmp(QQ a, QQ b) { return a.k < b.k; }
long long sum[maxn];
vector<QQ> ask;
vector<QQ> que[maxn];
long long dfs1(int u, int t) {
  long long tmp = 0;
  for (int i = 0; i < (int)q[u].size(); i++)
    if (q[u][i] == t) tmp++;
  for (int i = 0; i < (int)C[u].size(); i++) {
    int v = C[u][i];
    tmp += dfs1(v, t);
  }
  for (int i = 0; i < (int)End[u].size(); i++) sum[End[u][i]] += tmp;
  return tmp;
}
void solve1() {
  for (int i = 1; i <= n; i++) que[i].clear();
  for (int i = 0; i < (int)ask.size(); i++) {
    QQ nw = ask[i];
    if (flag[nw.k]) que[nw.k].push_back(nw);
  }
  for (int i = 1; i <= n; i++) {
    if (flag[i]) {
      memset(sum, 0, sizeof(sum));
      dfs1(0, i);
      for (int j = 2; j <= n; j++) sum[j] += sum[j - 1];
      for (int j = 0; j < (int)que[i].size(); j++) {
        QQ nw = que[i][j];
        ANS[nw.u] = sum[nw.r] - sum[nw.l - 1];
      }
    }
  }
}
long long sq[maxn], a[maxn];
void sqAdd(int i, long long x) {
  for (; i % K != 0 && i <= n; i++) a[i] += x;
  for (; i <= n; i += K) sq[i / K] += x;
}
long long sqGet(int i) { return a[i] + sq[i / K]; }
void dfs2(int u) {
  for (int i = 0; i < (int)End[u].size(); i++) sqAdd(End[u][i], 1ll);
  for (int i = 0; i < (int)q[u].size(); i++) {
    int ii = q[u][i];
    for (int j = 0; j < (int)que[ii].size(); j++) {
      QQ nw = que[ii][j];
      ANS[nw.u] += sqGet(nw.r) - sqGet(nw.l - 1);
    }
  }
  for (int i = 0; i < (int)C[u].size(); i++) dfs2(C[u][i]);
  for (int i = 0; i < (int)End[u].size(); i++) sqAdd(End[u][i], -1ll);
}
void solve2() {
  for (int i = 1; i <= n; i++) que[i].clear();
  for (int i = 0; i < (int)ask.size(); i++) {
    QQ nw = ask[i];
    if (!flag[nw.k]) que[nw.k].push_back(nw);
  }
  memset(sq, 0, sizeof(sq));
  memset(a, 0, sizeof(a));
  dfs2(0);
}
int main() {
  int q;
  cin >> n >> q;
  AC_build();
  ask.clear();
  for (int qq = 1; qq <= q; qq++) {
    int l, r, k;
    scanf("%d%d%d", &l, &r, &k);
    ask.push_back(QQ(qq, l, r, k));
  }
  sort(ask.begin(), ask.end(), cmp);
  solve1();
  solve2();
  for (int i = 1; i <= q; i++) printf("%I64d\n", ANS[i]);
  return 0;
}

```