// Arup Guha
// 2/25/2023
// Solution to 2022 SER D2 Problem: Streets Ahead

using namespace std;

#include <iostream>
#include <map>
#include <string>
#include <cmath>

int main() {

    // Read mod info.
    int n, q;
    cin >> n >> q;

    // Place each street in a map to index.
    map<string,int> mymap;
    for (int i=0; i<n; i++) {
        string tmp;
        cin >> tmp;
        mymap.emplace(tmp, i);
    }

    // Process queries.
    for (int i=0; i<q; i++) {
        string a, b;
        cin >> a >> b;
        cout << abs(mymap[a]-mymap[b])-1 << endl;
    }

    return 0;
}
