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;
const int NMAX = 5e5;
const int QMAX = 5e5;
const int LGMAX = 18;
int n, q;
string s[NMAX + 5];
pair<int, int> sa[NMAX + 5];
int pos[LGMAX + 5][NMAX + 5];
int len_sa;
int gap;
pair<int, int> segm[NMAX + 5];
int ans[QMAX + 5];
int sp[NMAX + 5];
struct query_t {
  int sgn;
  int val;
  int l, r;
  int id;
  bool operator<(const query_t &other) const {
    if (val != other.val) {
      return val < other.val;
    }
    return id < other.id;
  }
};
vector<query_t> v;
bool cmp(pair<int, int> a, pair<int, int> b) {
  if (pos[gap][sp[a.first - 1] + a.second] !=
      pos[gap][sp[b.first - 1] + b.second]) {
    return pos[gap][sp[a.first - 1] + a.second] <
           pos[gap][sp[b.first - 1] + b.second];
  }
  if (a.second + (1 << gap) >= (int)s[a.first].size() ||
      b.second + (1 << gap) >= (int)s[b.first].size()) {
    return (int)s[a.first].size() - a.second <
           (int)s[b.first].size() - b.second;
  }
  a.second += (1 << gap);
  b.second += (1 << gap);
  return pos[gap][sp[a.first - 1] + a.second] <
         pos[gap][sp[b.first - 1] + b.second];
}
void build_sa() {
  int len = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (int)s[i].size(); j++) {
      sa[++len] = {i, j};
      pos[0][sp[i - 1] + j] = (s[i][j] - 'a') + 1;
    }
  }
  sort(sa + 1, sa + 1 + len, [](pair<int, int> a, pair<int, int> b) {
    if (s[a.first][a.second] != s[b.first][b.second]) {
      return s[a.first][a.second] < s[b.first][b.second];
    }
    if ((int)s[a.first].size() - a.second !=
        (int)s[b.first].size() - b.second) {
      return (int)s[a.first].size() - a.second <
             (int)s[b.first].size() - b.second;
    }
    return a.first < b.first;
  });
  for (gap = 0; (1 << gap) <= sp[n]; gap++) {
    sort(sa + 1, sa + 1 + len, cmp);
    pos[gap + 1][sp[sa[1].first - 1] + sa[1].second] = 1;
    int lst_pos = 1;
    for (int i = 2; i <= len; i++) {
      pos[gap + 1][sp[sa[i].first - 1] + sa[i].second] =
          (lst_pos += cmp(sa[i - 1], sa[i]));
    }
  }
  len_sa = len;
}
int lcp(pair<int, int> x, pair<int, int> y) {
  int ans = 0;
  for (int lvl = gap; lvl >= 0; lvl--) {
    if (x.second < (int)s[x.first].size() &&
        y.second < (int)s[y.first].size() &&
        pos[lvl][sp[x.first - 1] + x.second] ==
            pos[lvl][sp[y.first - 1] + y.second]) {
      x.second += (1 << lvl);
      y.second += (1 << lvl);
      ans += (1 << lvl);
    }
  }
  return ans;
}
int aib[NMAX + 5];
void u(int pos, int val) {
  for (; pos <= NMAX; pos += (-pos) & pos) {
    aib[pos] += val;
  }
}
int qu(int pos) {
  int ans = 0;
  for (; pos; pos -= (-pos & pos)) {
    ans += aib[pos];
  }
  return ans;
}
int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    sp[i] = sp[i - 1] + (int)s[i].size();
  }
  build_sa();
  for (int i = 1; i <= len_sa; i++) {
    if (sa[i].second == 0) {
      int st, dr;
      st = 0, dr = i;
      while (dr - st > 1) {
        int mid = (st + dr) / 2;
        if (lcp(sa[mid], sa[i]) >= (int)s[sa[i].first].size()) {
          dr = mid;
        } else {
          st = mid;
        }
      }
      segm[sa[i].first].first = dr;
      st = i, dr = len_sa + 1;
      while (dr - st > 1) {
        int mid = (st + dr) / 2;
        if (lcp(sa[mid], sa[i]) >= (int)s[sa[i].first].size()) {
          st = mid;
        } else {
          dr = mid;
        }
      }
      segm[sa[i].first].second = st;
    }
  }
  for (int i = 1; i <= q; i++) {
    int l, r, k;
    scanf("%d %d %d", &l, &r, &k);
    v.push_back({-1, l - 1, segm[k].first, segm[k].second, i});
    v.push_back({1, r, segm[k].first, segm[k].second, i});
  }
  for (int i = 1; i <= len_sa; i++) {
    v.push_back({0, sa[i].first, i, i, 0});
  }
  sort(v.begin(), v.end());
  for (auto it : v) {
    if (it.sgn == 0) {
      u(it.l, 1);
    } else {
      ans[it.id] += it.sgn * (qu(it.r) - qu(it.l - 1));
    }
  }
  for (int i = 1; i <= q; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}