#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
using namespace std;

int n, q;
vector<vector<string>> seg;
vector<string> w;
string pre, suf;

void build() {
    seg.resize(2 * n + 1);
    for (int i = 0; i < n; i++) {
        string s = w[i];
        reverse(s.begin(), s.end());
        seg[i + n].push_back(s);
    }

    for (int i = n - 1; i > 0; i--) {
        for (auto s:seg[i * 2]) {
            seg[i].push_back(s);
        }
        for (auto s:seg[i * 2 + 1]) {
            seg[i].push_back(s);
        }
        sort(seg[i].begin(), seg[i].end());
    }
}

int seg_query(int l, int r, string &q) {
    int ans = 0;
    reverse(q.begin(), q.end());
    string q2 = q;
    q2.back()++;
    l += n;
    r += n;
    while (l < r) {
        if (l & 1) {
            ans += lower_bound(seg[l].begin(), seg[l].end(), q2)
            - lower_bound(seg[l].begin(), seg[l].end(), q);
            l++;
        }
        if (r & 1) {
            r--;
            ans += lower_bound(seg[r].begin(), seg[r].end(), q2)
            - lower_bound(seg[r].begin(), seg[r].end(), q);
        }
        l >>= 1;
        r >>= 1;
    }

    return ans;
}

int query(string &p, string q) {
    string p2 = p;
    p2.back()++;
    int s = lower_bound(w.begin(), w.end(), p) - w.begin();
    int t = lower_bound(w.begin(), w.end(), p2) - w.begin();

    //cout << s << " " << t << endl;
    return seg_query(s, t, q);
}

int main() {
    cin >> n >> q;
    w.resize(n);
    for (auto &s:w) cin >> s;
    sort(w.begin(), w.end());

    build();
    while (q--) {
        cin >> pre >> suf;

        cout << query(pre, suf) << endl;
        //query(pre, suf);
        //cout << seg_query(0, n, suf) << endl;
    }
}

