#include<bits/stdc++.h>
#define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;i=i+1)
#define FORD(i,b,a) for (int i=(b),_a=(a);i>=_a;i=i-1)
#define REP(i,n) for (int i=0,_n=(n);i<_n;i=i+1)
#define FORE(i,v) for (__typeof((v).begin()) i=(v).begin();i!=(v).end();i++)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MASK(i) (1LL<<(i))
#define BIT(x,i) (((x)>>(i))&1)
#define div ___div
#define next ___next
#define prev ___prev
#define left ___left
#define right ___right
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<class X,class Y>
void minimize(X &x,const Y &y) {
if (x>y) x=y;
}
template<class X,class Y>
void maximize(X &x,const Y &y) {
if (x<y) x=y;
}
template<class T>
T Abs(const T &x) {
return (x<0?-x:x);
}
/* Author: Van Hanh Pham */
/** END OF TEMPLATE - ACTUAL SOLUTION COMES HERE **/
class FenwickTree {
private:
int n;
vector<int> v;
public:
FenwickTree(int n = 0) {
this->n = n;
v.assign(n + 7, 0);
}
void update(int x, int d) {
if (x == 0) return;
for (; x <= n; x += x & -x) v[x] += d;
}
int get(int x) const {
int res = 0;
for (; x >= 1; x &= x - 1) res += v[x];
return res;
}
int getSum(int l, int r) const {
return l == 1 ? get(r) : get(r) - get(l - 1);
}
};
struct Node {
Node *child[26];
int last;
Node() {
REP(i, 26) child[i] = NULL;
last = 0;
}
};
#define MAX 200200
int numWord, numQuery;
string words[MAX];
Node *prefixRoot, *suffixRoot;
vector<pair<int, int> > queryAt[MAX];
long long answer[MAX];
void init(void) {
cin >> numWord >> numQuery;
FOR(i, 1, numWord) cin >> words[i];
FOR(i, 1, numQuery) {
int l, r; cin >> l >> r;
queryAt[r].push_back(make_pair(l, i));
}
}
void addString(Node *root, const string &s) {
Node *p = root;
FORE(it, s) {
if (p->child[*it - 'a'] == NULL) p->child[*it - 'a'] = new Node();
p = p->child[*it - 'a'];
}
}
void prepare(void) {
prefixRoot = new Node();
suffixRoot = new Node();
FOR(i, 1, numWord) {
addString(prefixRoot, words[i]);
reverse(ALL(words[i]));
addString(suffixRoot, words[i]);
reverse(ALL(words[i]));
}
}
void process(void) {
FenwickTree prefixBit[26], suffixBit[26], allSuffixBit;
REP(i, 26) {
prefixBit[i] = FenwickTree(numWord);
suffixBit[i] = FenwickTree(numWord);
}
allSuffixBit = FenwickTree(numWord);
FOR(i, 1, numWord) {
Node *p = prefixRoot;
FORE(it, words[i]) {
p = p->child[*it - 'a'];
if (it != words[i].begin()) prefixBit[*it - 'a'].update(p->last, -1);
p->last = i;
if (it != words[i].begin()) prefixBit[*it - 'a'].update(p->last, 1);
}
reverse(ALL(words[i]));
p = suffixRoot;
FORE(it, words[i]) {
p = p->child[*it - 'a'];
allSuffixBit.update(p->last, -1);
if (it != words[i].begin()) suffixBit[*it - 'a'].update(p->last, -1);
p->last = i;
allSuffixBit.update(p->last, 1);
if (it != words[i].begin()) suffixBit[*it - 'a'].update(p->last, 1);
}
reverse(ALL(words[i]));
FORE(jt, queryAt[i]) {
int l = jt->fi, r = i, id = jt->se;
REP(k, 26) {
if (prefixRoot->child[k] != NULL && prefixRoot->child[k]->last >= l)
answer[id] += allSuffixBit.getSum(l, r);
int numPre = prefixBit[k].getSum(l, r);
int numSuf = allSuffixBit.getSum(l, r) - suffixBit[k].getSum(l, r);
answer[id] += 1LL * numPre * numSuf;
}
}
}
FOR(i, 1, numQuery) cout << answer[i] << "\n";
}
int main(void) {
#ifdef SKY
freopen("tmp.txt", "r", stdin);
#endif // SKY
init();
prepare();
process();
return 0;
}
/*** LOOK AT MY CODE. MY CODE IS AMAZING :D ***/
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBGT1IoaSxhLGIpIGZvciAoaW50IGk9KGEpLF9iPShiKTtpPD1fYjtpPWkrMSkKI2RlZmluZSBGT1JEKGksYixhKSBmb3IgKGludCBpPShiKSxfYT0oYSk7aT49X2E7aT1pLTEpCiNkZWZpbmUgUkVQKGksbikgZm9yIChpbnQ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4gdjsKCnB1YmxpYzoKICAgIEZlbndpY2tUcmVlKGludCBuID0gMCkgewogICAgICAgIHRoaXMtPm4gPSBuOwogICAgICAgIHYuYXNzaWduKG4gKyA3LCAwKTsKICAgIH0KCiAgICB2b2lkIHVwZGF0ZShpbnQgeCwgaW50IGQpIHsKICAgICAgICBpZiAoeCA9PSAwKSByZXR1cm47CiAgICAgICAgZm9yICg7IHggPD0gbjsgeCArPSB4ICYgLXgpIHZbeF0gKz0gZDsKICAgIH0KCiAgICBpbnQgZ2V0KGludCB4KSBjb25zdCB7CiAgICAgICAgaW50IHJlcyA9IDA7CiAgICAgICAgZm9yICg7IHggPj0gMTsgeCAmPSB4IC0gMSkgcmVzICs9IHZbeF07CiAgICAgICAgcmV0dXJuIHJlczsKICAgIH0KCiAgICBpbnQgZ2V0U3VtKGludCBsLCBpbnQgcikgY29uc3QgewogICAgICAgIHJldHVybiBsID09IDEgPyBnZXQocikgOiBnZXQocikgLSBnZXQobCAtIDEpOwogICAgfQp9OwoKc3RydWN0IE5vZGUgewogICAgTm9kZSAqY2hpbGRbMjZdOwogICAgaW50IGxhc3Q7CgogICAgTm9kZSgpIHsKICAgICAgICBSRVAoaSwgMjYpIGNoaWxkW2ldID0gTlVMTDsKICAgICAgICBsYXN0ID0gMDsKICAgIH0KfTsKCiNkZWZpbmUgTUFYICAgMjAwMjAwCgppbnQgbnVtV29yZCwgbnVtUXVlcnk7CnN0cmluZyB3b3Jkc1tNQVhdOwpOb2RlICpwcmVmaXhSb290LCAqc3VmZml4Um9vdDsKdmVjdG9yPHBhaXI8aW50LCBpbnQ+ID4gcXVlcnlBdFtNQVhdOwpsb25nIGxvbmcgYW5zd2VyW01BWF07Cgp2b2lkIGluaXQodm9pZCkgewogICAgY2luID4+IG51bVdvcmQgPj4gbnVtUXVlcnk7CiAgICBGT1IoaSwgMSwgbnVtV29yZCkgY2luID4+IHdvcmRzW2ldOwogICAgRk9SKGksIDEsIG51bVF1ZXJ5KSB7CiAgICAgICAga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Tm9kZSAqcCA9IHByZWZpeFJvb3Q7CiAgICAgICAgRk9SRShpdCwgd29yZHNbaV0pIHsKICAgICAgICAgICAgcCA9IHAtPmNoaWxkWyppdCAtICdhJ107CiAgICAgICAgICAgIGlmIChpdCAhPSB3b3Jkc1tpXS5iZWdpbigpKSBwcmVmaXhCaXRbKml0IC0gJ2EnXS51cGRhdGUocC0+bGFzdCwgLTEpOwogICAgICAgICAgICBwLT5sYXN0ID0gaTsKICAgICAgICAgICAgaWYgKGl0ICE9IHdvcmRzW2ldLmJlZ2luKCkpIHByZWZpeEJpdFsqaXQgLSAnYSddLnVwZGF0ZShwLT5sYXN0LCAxKTsKICAgICAgICB9CgogICAgICAgIHJldmVyc2UoQUxMKHdvcmRzW2ldKSk7CgogICAgICAgIHAgPSBzdWZmaXhSb290OwogICAgICAgIEZPUkUoaXQsIHdvcmRzW2ldKSB7CiAgICAgICAgICAgIHAgPSBwLT5jaGlsZFsqaXQgLSAnYSddOwogICAgICAgICAgICBhbGxTdWZmaXhCaXQudXBkYXRlKHAtPmxhc3QsIC0xKTsKICAgICAgICAgICAgaWYgKGl0ICE9IHdvcmRzW2ldLmJlZ2luKCkpIHN1ZmZpeEJpdFsqaXQgLSAnYSddLnVwZGF0ZShwLT5sYXN0LCAtMSk7CiAgICAgICAgICAgIHAtPmxhc3QgPSBpOwogICAgICAgICAgICBhbGxTdWZmaXhCaXQudXBkYXRlKHAtPmxhc3QsIDEpOwogICAgICAgICAgICBpZiAoaXQgIT0gd29yZHNbaV0uYmVnaW4oKSkgc3VmZml4Qml0WyppdCAtICdhJ10udXBkYXRlKHAtPmxhc3QsIDEpOwogICAgICAgIH0KCiAgICAgICAgcmV2ZXJzZShBTEwod29yZHNbaV0pKTsKCiAgICAgICAgRk9SRShqdCwgcXVlcnlBdFtpXSkgewogICAgICAgICAgICBpbnQgbCA9IGp0LT5maSwgciA9IGksIGlkID0ganQtPnNlOwogICAgICAgICAgICBSRVAoaywgMjYpIHsKICAgICAgICAgICAgICAgIGlmIChwcmVmaXhSb290LT5jaGlsZFtrXSAhPSBOVUxMICYmIHByZWZpeFJvb3QtPmNoaWxkW2tdLT5sYXN0ID49IGwpCiAgICAgICAgICAgICAgICAgICAgYW5zd2VyW2lkXSArPSBhbGxTdWZmaXhCaXQuZ2V0U3VtKGwsIHIpOwogICAgICAgICAgICAgICAgaW50IG51bVByZSA9IHByZWZpeEJpdFtrXS5nZXRTdW0obCwgcik7CiAgICAgICAgICAgICAgICBpbnQgbnVtU3VmID0gYWxsU3VmZml4Qml0LmdldFN1bShsLCByKSAtIHN1ZmZpeEJpdFtrXS5nZXRTdW0obCwgcik7CiAgICAgICAgICAgICAgICBhbnN3ZXJbaWRdICs9IDFMTCAqIG51bVByZSAqIG51bVN1ZjsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICBGT1IoaSwgMSwgbnVtUXVlcnkpIGNvdXQgPDwgYW5zd2VyW2ldIDw8ICJcbiI7Cn0KCmludCBtYWluKHZvaWQpIHsKI2lmZGVmIFNLWQogICAgZnJlb3BlbigidG1wLnR4dCIsICJyIiwgc3RkaW4pOwojZW5kaWYgLy8gU0tZCgogICAgaW5pdCgpOwogICAgcHJlcGFyZSgpOwogICAgcHJvY2VzcygpOwogICAgcmV0dXJuIDA7Cn0KCgovKioqIExPT0sgQVQgTVkgQ09ERS4gTVkgQ09ERSBJUyBBTUFaSU5HIDpEICoqKi8K