/*
Jackson Simoneau
SI@UCF 2026 - Intro to Competitive Programming
Contest 2

Solution to Streets Ahead: https://open.kattis.com/problems/streetsahead
*/

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n, q;
    cin >> n >> q;
    map<string, int> inter;
    for(int i = 0; i < n; i++) {
        string s; cin >> s;
        inter[s] = i;
    }
    while(q--) {
        string a, b;
        cin >> a >> b;
        cout << abs(inter[a] - inter[b])-1 << endl;
    }
}