#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long ans[N], val[N];
int n, m, q, K[N];
char str[N];
vector<int> str1, str2, add[N], del[N];
vector<pair<int, int> > str1_query[N];
template <typename _Tp>
inline void IN(_Tp& x) {
  char ch;
  bool flag = 0;
  x = 0;
  while (ch = getchar(), !isdigit(ch))
    if (ch == '-') flag = 1;
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  if (flag) x = -x;
}
namespace AC_AutoMaton {
vector<int> son[N];
int cnt = 1, E[N], len[N], ch[N][26], fail[N];
int tim, fa[N], siz[N], dfn[N];
inline void ins(int id) {
  int now = 1;
  for (int i = 0; i < len[id]; ++i) {
    if (!ch[now][str[i] - 'a']) ch[now][str[i] - 'a'] = ++cnt, fa[cnt] = now;
    now = ch[now][str[i] - 'a'];
  }
  E[id] = now;
}
void dfs(int u) {
  dfn[u] = ++tim, siz[u] = 1;
  for (auto v : son[u]) dfs(v), siz[u] += siz[v];
}
inline void build() {
  queue<int> que;
  for (int i = 0; i < 26; ++i)
    if (ch[1][i])
      fail[ch[1][i]] = 1, que.push(ch[1][i]);
    else
      ch[1][i] = 1;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = 0; i < 26; ++i)
      if (ch[u][i])
        fail[ch[u][i]] = ch[fail[u]][i], que.push(ch[u][i]);
      else
        ch[u][i] = ch[fail[u]][i];
  }
  for (int i = 2; i <= cnt; ++i) son[fail[i]].push_back(i);
  dfs(1);
}
}  // namespace AC_AutoMaton
using namespace AC_AutoMaton;
struct BIT {
  int c[N], res;
  inline int lowbit(int x) { return x & (-x); }
  inline void update(int x, int y) {
    for (; x <= cnt; x += lowbit(x)) c[x] += y;
  }
  inline int sum(int x) {
    res = 0;
    for (; x; x -= lowbit(x)) res += c[x];
    return res;
  }
  inline int query(int l, int r) { return sum(r) - sum(l - 1); }
} bit;
inline int abs(int x) { return x > 0 ? x : -x; }
int main() {
  IN(n), IN(q);
  for (int i = 1; i <= n; ++i)
    scanf("%s\n", str), len[i] = strlen(str), ins(i), m += strlen(str);
  build();
  int lim = sqrt(m), L, R;
  for (int i = 1; i <= n; ++i)
    (len[i] > lim) ? str1.push_back(i) : str2.push_back(i);
  for (int i = 1; i <= q; ++i) {
    IN(L), IN(R), IN(K[i]);
    if (len[K[i]] <= lim)
      del[L - 1].push_back(i), add[R].push_back(i);
    else
      str1_query[K[i]].push_back(make_pair(-L + 1, i)),
          str1_query[K[i]].push_back(make_pair(R, i));
  }
  for (auto now : str1) {
    int tmp;
    tmp = E[now];
    while (tmp) bit.update(dfn[tmp], 1), tmp = fa[tmp];
    for (int i = 1; i <= n; ++i)
      val[i] = bit.query(dfn[E[i]], dfn[E[i]] + siz[E[i]] - 1),
      val[i] += val[i - 1];
    tmp = E[now];
    while (tmp) bit.update(dfn[tmp], -1), tmp = fa[tmp];
    for (auto que : str1_query[now]) {
      int op = que.first > 0 ? 1 : -1, pos = abs(que.first);
      ans[que.second] += val[pos] * op;
    }
  }
  for (int i = 1; i <= n; ++i) {
    bit.update(dfn[E[i]], 1), bit.update(dfn[E[i]] + siz[E[i]], -1);
    for (auto now : del[i]) {
      int tmp = E[K[now]];
      long long res = 0;
      while (tmp) res += bit.sum(dfn[tmp]), tmp = fa[tmp];
      ans[now] -= res;
    }
    for (auto now : add[i]) {
      int tmp = E[K[now]];
      long long res = 0;
      while (tmp) res += bit.sum(dfn[tmp]), tmp = fa[tmp];
      ans[now] += res;
    }
  }
  for (int i = 1; i <= q; ++i) printf("%lld\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 N = 1e5 + 5;
long long ans[N], val[N];
int n, m, q, K[N];
char str[N];
vector<int> str1, str2, add[N], del[N];
vector<pair<int, int> > str1_query[N];
template <typename _Tp>
inline void IN(_Tp& x) {
  char ch;
  bool flag = 0;
  x = 0;
  while (ch = getchar(), !isdigit(ch))
    if (ch == '-') flag = 1;
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  if (flag) x = -x;
}
namespace AC_AutoMaton {
vector<int> son[N];
int cnt = 1, E[N], len[N], ch[N][26], fail[N];
int tim, fa[N], siz[N], dfn[N];
inline void ins(int id) {
  int now = 1;
  for (int i = 0; i < len[id]; ++i) {
    if (!ch[now][str[i] - 'a']) ch[now][str[i] - 'a'] = ++cnt, fa[cnt] = now;
    now = ch[now][str[i] - 'a'];
  }
  E[id] = now;
}
void dfs(int u) {
  dfn[u] = ++tim, siz[u] = 1;
  for (auto v : son[u]) dfs(v), siz[u] += siz[v];
}
inline void build() {
  queue<int> que;
  for (int i = 0; i < 26; ++i)
    if (ch[1][i])
      fail[ch[1][i]] = 1, que.push(ch[1][i]);
    else
      ch[1][i] = 1;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = 0; i < 26; ++i)
      if (ch[u][i])
        fail[ch[u][i]] = ch[fail[u]][i], que.push(ch[u][i]);
      else
        ch[u][i] = ch[fail[u]][i];
  }
  for (int i = 2; i <= cnt; ++i) son[fail[i]].push_back(i);
  dfs(1);
}
}  // namespace AC_AutoMaton
using namespace AC_AutoMaton;
struct BIT {
  int c[N], res;
  inline int lowbit(int x) { return x & (-x); }
  inline void update(int x, int y) {
    for (; x <= cnt; x += lowbit(x)) c[x] += y;
  }
  inline int sum(int x) {
    res = 0;
    for (; x; x -= lowbit(x)) res += c[x];
    return res;
  }
  inline int query(int l, int r) { return sum(r) - sum(l - 1); }
} bit;
inline int abs(int x) { return x > 0 ? x : -x; }
int main() {
  IN(n), IN(q);
  for (int i = 1; i <= n; ++i)
    scanf("%s\n", str), len[i] = strlen(str), ins(i), m += strlen(str);
  build();
  int lim = sqrt(m), L, R;
  for (int i = 1; i <= n; ++i)
    (len[i] > lim) ? str1.push_back(i) : str2.push_back(i);
  for (int i = 1; i <= q; ++i) {
    IN(L), IN(R), IN(K[i]);
    if (len[K[i]] <= lim)
      del[L - 1].push_back(i), add[R].push_back(i);
    else
      str1_query[K[i]].push_back(make_pair(-L + 1, i)),
          str1_query[K[i]].push_back(make_pair(R, i));
  }
  for (auto now : str1) {
    int tmp;
    tmp = E[now];
    while (tmp) bit.update(dfn[tmp], 1), tmp = fa[tmp];
    for (int i = 1; i <= n; ++i)
      val[i] = bit.query(dfn[E[i]], dfn[E[i]] + siz[E[i]] - 1),
      val[i] += val[i - 1];
    tmp = E[now];
    while (tmp) bit.update(dfn[tmp], -1), tmp = fa[tmp];
    for (auto que : str1_query[now]) {
      int op = que.first > 0 ? 1 : -1, pos = abs(que.first);
      ans[que.second] += val[pos] * op;
    }
  }
  for (int i = 1; i <= n; ++i) {
    bit.update(dfn[E[i]], 1), bit.update(dfn[E[i]] + siz[E[i]], -1);
    for (auto now : del[i]) {
      int tmp = E[K[now]];
      long long res = 0;
      while (tmp) res += bit.sum(dfn[tmp]), tmp = fa[tmp];
      ans[now] -= res;
    }
    for (auto now : add[i]) {
      int tmp = E[K[now]];
      long long res = 0;
      while (tmp) res += bit.sum(dfn[tmp]), tmp = fa[tmp];
      ans[now] += res;
    }
  }
  for (int i = 1; i <= q; ++i) printf("%lld\n", ans[i]);
  return 0;
}

```