#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 50;
const int S = 300;
int n, m, ps[N];
char str[N];
bool vs[N];
int in[N], out[N], sgn;
vector<int> G[N];
long long as[N];
vector<pair<int, int> > v[N], A[N];
void dfs(int u) {
  in[u] = ++sgn;
  for (int v : G[u]) dfs(v);
  out[u] = sgn;
}
namespace BLK {
const int M = 500;
int a[N], sm[M], L[M], R[M], blk[N], Sz, ct;
void pre_work() {
  Sz = sqrt(sgn);
  for (int i = 1; i <= sgn; i++) blk[i] = (i - 1) / Sz + 1;
  for (int i = 1; i <= sgn; i += Sz) L[++ct] = i, R[ct] = i + Sz - 1;
  R[ct] = n;
}
void mdf(int l, int r) {
  if (blk[l] == blk[r] || blk[l] + 1 == blk[r]) {
    for (int i = l; i <= r; i++) ++a[i];
    return;
  }
  for (int i = l; i <= R[blk[l]]; i++) ++a[i];
  for (int i = L[blk[r]]; i <= r; i++) ++a[i];
  for (int i = blk[l] + 1; i < blk[r]; i++) ++sm[i];
}
int ask(int x) { return a[x] + sm[blk[x]]; }
}  // namespace BLK
namespace AC {
int ch[N][26], fa[N], lk[N], nd;
void ins(int k) {
  scanf("%s", str + 1);
  int len = strlen(str + 1);
  if (len <= S) vs[k] = true;
  int now = 0;
  for (int i = 1; i <= len; i++) {
    int c = str[i] - 'a';
    if (!ch[now][c]) ch[now][c] = ++nd, fa[nd] = now;
    now = ch[now][c];
  }
  ps[k] = now;
}
void build() {
  queue<int> q;
  for (int i = 0; i < 26; i++)
    if (ch[0][i]) q.push(ch[0][i]);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < 26; i++)
      if (ch[x][i])
        q.push(ch[x][i]), lk[ch[x][i]] = ch[lk[x]][i];
      else
        ch[x][i] = ch[lk[x]][i];
  }
  for (int i = 1; i <= nd; i++) G[lk[i]].push_back(i);
}
}  // namespace AC
using AC::fa;
void work(int k) {
  static int sm[N];
  memset(sm, 0, sizeof(int) * (sgn + 1));
  for (int x = ps[k]; x; x = fa[x]) sm[in[x]]++;
  for (int i = 1; i <= sgn; i++) sm[i] += sm[i - 1];
  sort(A[k].begin(), A[k].end());
  long long Sm = 0;
  for (int i = 1, j = 0; i <= n; i++) {
    Sm += (long long)sm[out[ps[i]]] - sm[in[ps[i]] - 1];
    while (j < (int)A[k].size() && A[k][j].first == i) {
      int c = A[k][j].second;
      if (c < 0)
        as[-c] -= Sm;
      else
        as[c] += Sm;
      ++j;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) AC ::ins(i);
  AC ::build();
  dfs(0);
  for (int i = 1, l, r, k; i <= m; i++) {
    scanf("%d%d%d", &l, &r, &k);
    if (vs[k]) {
      if (l > 1) v[l - 1].push_back(pair<int, int>(-i, k));
      v[r].push_back(pair<int, int>(i, k));
    } else {
      if (l > 1) A[k].push_back(pair<int, int>(l - 1, -i));
      A[k].push_back(pair<int, int>(r, i));
    }
  }
  BLK::pre_work();
  for (int i = 1; i <= n; i++) {
    BLK ::mdf(in[ps[i]], out[ps[i]]);
    for (auto t : v[i]) {
      int k = t.second, c = t.first;
      for (int j = ps[k]; j; j = fa[j])
        if (c < 0)
          as[-c] -= (long long)BLK ::ask(in[j]);
        else
          as[c] += (long long)BLK ::ask(in[j]);
    }
  }
  for (int i = 1; i <= n; i++)
    if (A[i].size()) work(i);
  for (int i = 1; i <= m; i++) cout << as[i] << '\n';
  return 0;
}

### Prompt

In Cpp, your task is to solve the following problem:

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 + 50;
const int S = 300;
int n, m, ps[N];
char str[N];
bool vs[N];
int in[N], out[N], sgn;
vector<int> G[N];
long long as[N];
vector<pair<int, int> > v[N], A[N];
void dfs(int u) {
  in[u] = ++sgn;
  for (int v : G[u]) dfs(v);
  out[u] = sgn;
}
namespace BLK {
const int M = 500;
int a[N], sm[M], L[M], R[M], blk[N], Sz, ct;
void pre_work() {
  Sz = sqrt(sgn);
  for (int i = 1; i <= sgn; i++) blk[i] = (i - 1) / Sz + 1;
  for (int i = 1; i <= sgn; i += Sz) L[++ct] = i, R[ct] = i + Sz - 1;
  R[ct] = n;
}
void mdf(int l, int r) {
  if (blk[l] == blk[r] || blk[l] + 1 == blk[r]) {
    for (int i = l; i <= r; i++) ++a[i];
    return;
  }
  for (int i = l; i <= R[blk[l]]; i++) ++a[i];
  for (int i = L[blk[r]]; i <= r; i++) ++a[i];
  for (int i = blk[l] + 1; i < blk[r]; i++) ++sm[i];
}
int ask(int x) { return a[x] + sm[blk[x]]; }
}  // namespace BLK
namespace AC {
int ch[N][26], fa[N], lk[N], nd;
void ins(int k) {
  scanf("%s", str + 1);
  int len = strlen(str + 1);
  if (len <= S) vs[k] = true;
  int now = 0;
  for (int i = 1; i <= len; i++) {
    int c = str[i] - 'a';
    if (!ch[now][c]) ch[now][c] = ++nd, fa[nd] = now;
    now = ch[now][c];
  }
  ps[k] = now;
}
void build() {
  queue<int> q;
  for (int i = 0; i < 26; i++)
    if (ch[0][i]) q.push(ch[0][i]);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < 26; i++)
      if (ch[x][i])
        q.push(ch[x][i]), lk[ch[x][i]] = ch[lk[x]][i];
      else
        ch[x][i] = ch[lk[x]][i];
  }
  for (int i = 1; i <= nd; i++) G[lk[i]].push_back(i);
}
}  // namespace AC
using AC::fa;
void work(int k) {
  static int sm[N];
  memset(sm, 0, sizeof(int) * (sgn + 1));
  for (int x = ps[k]; x; x = fa[x]) sm[in[x]]++;
  for (int i = 1; i <= sgn; i++) sm[i] += sm[i - 1];
  sort(A[k].begin(), A[k].end());
  long long Sm = 0;
  for (int i = 1, j = 0; i <= n; i++) {
    Sm += (long long)sm[out[ps[i]]] - sm[in[ps[i]] - 1];
    while (j < (int)A[k].size() && A[k][j].first == i) {
      int c = A[k][j].second;
      if (c < 0)
        as[-c] -= Sm;
      else
        as[c] += Sm;
      ++j;
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) AC ::ins(i);
  AC ::build();
  dfs(0);
  for (int i = 1, l, r, k; i <= m; i++) {
    scanf("%d%d%d", &l, &r, &k);
    if (vs[k]) {
      if (l > 1) v[l - 1].push_back(pair<int, int>(-i, k));
      v[r].push_back(pair<int, int>(i, k));
    } else {
      if (l > 1) A[k].push_back(pair<int, int>(l - 1, -i));
      A[k].push_back(pair<int, int>(r, i));
    }
  }
  BLK::pre_work();
  for (int i = 1; i <= n; i++) {
    BLK ::mdf(in[ps[i]], out[ps[i]]);
    for (auto t : v[i]) {
      int k = t.second, c = t.first;
      for (int j = ps[k]; j; j = fa[j])
        if (c < 0)
          as[-c] -= (long long)BLK ::ask(in[j]);
        else
          as[c] += (long long)BLK ::ask(in[j]);
    }
  }
  for (int i = 1; i <= n; i++)
    if (A[i].size()) work(i);
  for (int i = 1; i <= m; i++) cout << as[i] << '\n';
  return 0;
}

```