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 MAX = 1e6 + 5;
const int LG = 20;
string s[MAX];
int st[MAX];
int en[MAX];
int sa[LG][MAX];
int finsa[MAX];
int rev[MAX];
pair<pair<int, int>, int> tmp[MAX];
int lcp[MAX];
int rep[MAX];
int rmq[LG][MAX];
long long ans[MAX];
pair<int, pair<pair<int, int>, pair<int, int> > > qq[MAX];
int bit[MAX];
void update(int x, int val) {
  x++;
  while (x < MAX) {
    bit[x] += val;
    x += (x & (-x));
  }
}
long long query(int x) {
  x++;
  long long ans = 0;
  while (x) {
    ans += bit[x];
    x -= (x & (-x));
  }
  return ans;
}
int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  string cur = "";
  int len = 0;
  for (int i = 1; i <= n; i++) {
    st[i] = cur.length();
    for (int j = 0; j < s[i].length(); j++) {
      rep[cur.length()] = i;
      cur += s[i][j];
    }
    en[i] = cur.length();
    cur += '$';
  }
  st[n + 1] = cur.length();
  for (int i = 0; i < cur.length(); i++) {
    if (cur[i] == '$') {
      sa[0][i] = 1000 + i;
    } else {
      sa[0][i] = cur[i] - 'a' + 1;
    }
  }
  for (int i = 1; i < LG; i++) {
    for (int j = 0; j < cur.length(); j++) {
      int x = (1 << (i - 1));
      tmp[j] =
          make_pair(make_pair(sa[i - 1][j],
                              ((j + x) < cur.length()) ? sa[i - 1][j + x] : -1),
                    j);
    }
    sort(tmp, tmp + (int)cur.length());
    sa[i][tmp[0].second] = 1;
    int val = 1;
    for (int j = 1; j < cur.length(); j++) {
      val += (tmp[j].first != tmp[j - 1].first);
      sa[i][tmp[j].second] = val;
    }
  }
  for (int i = 0; i < cur.length(); i++) {
    finsa[sa[LG - 1][i]] = i;
  }
  for (int i = 1; i < cur.length(); i++) {
    int x = finsa[i];
    int y = finsa[i + 1];
    for (int j = LG - 1; j >= 0; j--) {
      if (sa[j][x] == sa[j][y]) {
        x += (1 << j);
        y += (1 << j);
        lcp[i] += (1 << j);
      }
    }
  }
  for (int i = 1; i < cur.length(); i++) {
    rmq[0][i] = lcp[i];
  }
  for (int i = 1; i < LG; i++) {
    for (int j = 1; j < cur.length(); j++) {
      if ((j + (1 << (i - 1))) >= cur.length()) {
        rmq[i][j] = rmq[i - 1][j];
      } else {
        rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
      }
    }
  }
  int t = 0;
  for (int i = 1; i <= q; i++) {
    int l, r, k;
    scanf("%d %d %d", &l, &r, &k);
    l = st[l];
    r = st[r + 1] - 1;
    int sta = st[k];
    int fin = st[k + 1] - 1;
    int len = (fin - sta);
    int re = sa[LG - 1][sta];
    int hi = re;
    int lo = re;
    for (int j = LG - 1; j >= 0; j--) {
      if (((lo - (1 << j)) >= 1) && rmq[j][lo - (1 << j)] >= len) {
        lo -= (1 << j);
      }
      if (((hi + (1 << j)) < cur.length()) && rmq[j][hi] >= len) {
        hi += (1 << j);
      }
    }
    qq[t++] = make_pair(lo - 1, make_pair(make_pair(l, r), make_pair(-1, i)));
    qq[t++] = make_pair(hi, make_pair(make_pair(l, r), make_pair(1, i)));
  }
  sort(qq, qq + t);
  int pos = 1;
  for (int i = 0; i < t; i++) {
    while (pos <= cur.size() && pos <= qq[i].first) {
      update(finsa[pos], 1);
      pos++;
    }
    ans[qq[i].second.second.second] += (query(qq[i].second.first.second) -
                                        query(qq[i].second.first.first - 1)) *
                                       1ll * qq[i].second.second.first;
  }
  for (int i = 1; i <= q; i++) {
    printf("%lld\n", ans[i]);
  }
  return 0;
}