Problem: What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercase English letters. What is double strange that a phone number can be associated with several bears!

In that country there is a rock band called CF consisting of n bears (including Mike) numbered from 1 to n. 

<image>

Phone number of i-th member of CF is si. May 17th is a holiday named Phone Calls day. In the last Phone Calls day, everyone called all the numbers that are substrings of his/her number (one may call some number several times). In particular, everyone called himself (that was really strange country).

Denote as call(i, j) the number of times that i-th member of CF called the j-th member of CF. 

The geek Mike has q questions that he wants to ask you. In each question he gives you numbers l, r and k and you should tell him the number 

<image>

Input

The first line of input contains integers n and q (1 ≤ n ≤ 2 × 105 and 1 ≤ q ≤ 5 × 105).

The next n lines contain the phone numbers, i-th line contains a string si consisting of lowercase English letters (<image>).

The next q lines contain the information about the questions, each of them contains integers l, r and k (1 ≤ l ≤ r ≤ n and 1 ≤ k ≤ n).

Output

Print the answer for each question in a separate line.

Examples

Input

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


Output

7
5
6
3
6

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int n, q;
char s1[500200];
string s[500200];
struct node {
  int st, in;
  node() { st = -1, in = -1; }
};
node nd[500200];
int k = 0;
pair<int, pair<int, int> > sortt[500200];
pair<int, pair<int, int> > t2[500200];
int pl[500200];
int t1[500200];
vector<int> p[500200];
vector<int> p1[500200];
void sortP() {
  t1[sortt[0].first] = 0;
  for (int i = 1; i < k; i++) {
    if (sortt[i].first != sortt[i - 1].first) t1[sortt[i].first] = i;
  }
  for (int i = 0; i < k; i++) {
    if (sortt[i].second.first == -1) {
      t2[t1[sortt[i].first]++] = sortt[i];
    }
    p[sortt[i].second.first].push_back(i);
  }
  int nm;
  for (int i = 0; i < k; i++) {
    while (!p[i].empty()) {
      nm = p[i].back();
      p[i].pop_back();
      t2[t1[sortt[nm].first]++] = sortt[nm];
    }
  }
  for (int i = 0; i < k; i++) sortt[i] = t2[i];
}
void pre() {
  int lg = 1;
  for (int i = 0; i < k; i++) {
    sortt[i].first = s1[i];
    sortt[i].second.first = -1;
    sortt[i].second.second = i;
  }
  sort(sortt, sortt + k);
  while (lg <= k) {
    int nm = 1;
    for (int i = 1; i < k; i++) {
      if (make_pair(sortt[i].first, sortt[i].second.first) !=
          make_pair(sortt[i - 1].first, sortt[i - 1].second.first)) {
        sortt[i - 1].first = nm;
        nm++;
      } else
        sortt[i - 1].first = nm;
    }
    sortt[k - 1].first = nm;
    for (int i = 0; i < k; i++) {
      pl[sortt[i].second.second] = sortt[i].first;
    }
    for (int i = 0; i < k; i++) {
      sortt[i].second.first = -1;
      if (sortt[i].second.second + lg < k)
        sortt[i].second.first = pl[sortt[i].second.second + lg];
    }
    sort(sortt, sortt + k);
    lg *= 2;
  }
}
int pre1[500500];
vector<int> lcp() {
  pre1[sortt[0].second.second] = -1;
  vector<int> res;
  for (int i = 1; i < k; i++)
    pre1[sortt[i].second.second] = sortt[i - 1].second.second;
  int l = 0;
  for (int i = 0; i < k; i++) {
    while (i + l < k && pre1[i] + l < k && s1[i + l] == s1[pre1[i] + l]) l++;
    res.push_back(l);
    l = max(l - 1, 0);
  }
  return res;
}
int st1[200200];
int en1[200200];
struct query {
  int l, r, k, in;
  bool operator<(const query &z) const { return l > z.l; }
  query() {}
  query(const query &z) {
    l = z.l;
    r = z.r;
    k = z.k;
    in = z.in;
  }
};
query qu[500500];
int dp[1600800];
void add(int a, int b, int x, int l = 0, int h = k, int p = 1) {
  if (a == l && b == h) {
    dp[p] += x;
    return;
  }
  int m = (l + h) / 2;
  if (b <= m) {
    add(a, b, x, l, m, 2 * p);
  } else if (a > m) {
    add(a, b, x, m + 1, h, 2 * p + 1);
  } else {
    add(m + 1, b, x, m + 1, h, 2 * p + 1);
    add(a, m, x, l, m, 2 * p);
  }
  dp[p] = dp[2 * p] + dp[2 * p + 1];
}
int cal(int a, int b, int x = 0, int l = 0, int h = k, int p = 1) {
  if (a == l && b == h) {
    return dp[p];
  }
  int m = (l + h) / 2;
  if (b <= m) {
    return cal(a, b, x, l, m, 2 * p);
  } else if (a > m) {
    return cal(a, b, x, m + 1, h, 2 * p + 1);
  } else {
    return cal(m + 1, b, x, m + 1, h, 2 * p + 1) + cal(a, m, x, l, m, 2 * p);
  }
}
int res[500500];
int main() {
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    scanf("%s", s1);
    s[i] = string(s1);
  }
  for (int i = 0; i < n; i++) {
    int m = s[i].size();
    for (int j = 0; j < m; j++) {
      nd[k].st = i;
      nd[k].in = j;
      s1[k++] = s[i][j];
    }
    s1[k++] = 'a' - 1;
  }
  pre();
  vector<int> lcps = lcp();
  for (int i = k - 1; i >= 0; i--) {
    int in = sortt[i].second.second;
    if (nd[in].in == 0) {
      en1[nd[in].st] = i;
      for (int j = i + 1; j < k; j++) {
        if (lcps[sortt[j].second.second] < s[nd[in].st].size()) break;
        int in1 = sortt[j].second.second;
        en1[nd[in].st] = j;
        if (nd[in1].in == 0) {
          en1[nd[in].st] = en1[nd[in1].st];
          j = en1[nd[in1].st];
        }
      }
    }
    if (nd[in].st != -1) p[nd[in].st].push_back(i);
  }
  for (int i = 0; i < k; i++) {
    int in = sortt[i].second.second;
    if (nd[in].in == 0) {
      st1[nd[in].st] = i;
      for (int j = i - 1; j >= 0; j--) {
        if (lcps[sortt[j + 1].second.second] < s[nd[in].st].size()) break;
        int in1 = sortt[j].second.second;
        st1[nd[in].st] = j;
        if (nd[in1].in == 0) {
          st1[nd[in].st] = st1[nd[in1].st];
          j = st1[nd[in1].st];
        }
      }
    }
  }
  for (int i = 0; i < q; i++) {
    scanf("%d", &qu[i].l);
    scanf("%d", &qu[i].r);
    scanf("%d", &qu[i].k);
    qu[i].l--;
    qu[i].r--;
    qu[i].k--;
    qu[i].in = i;
  }
  sort(qu, qu + q);
  for (int i = 0; i < q; i++) {
    p1[qu[i].r].push_back(i);
  }
  int j = n - 1;
  for (int i = 0; i < q; i++) {
    while (j >= 0 && qu[i].l <= j) {
      for (int k = 0; k < p1[j].size(); k++) {
        int in = p1[j][k];
        res[qu[p1[j][k]].in] -= cal(st1[qu[in].k], en1[qu[in].k]);
      }
      for (int k = 0; k < p[j].size(); k++) {
        add(p[j][k], p[j][k], 1);
      }
      j--;
    }
    res[qu[i].in] += cal(st1[qu[i].k], en1[qu[i].k]);
  }
  for (int i = 0; i < q; i++) printf("%d\n", res[i]);
  return 0;
}