You can slightly improve your code by removing queue, you don’t need this.
The rest of the code looks algorithmically optimal. 
#include <map>
#include <iostream>
using namespace std;
int main()
{
    int a,b;
    map<int, int> AB;
    map<int, int>::iterator it;
    std::ios::sync_with_stdio(false);
    while (1)
    {
        cin >> a;
        if (a == -1)break;
        cin >> b;
        AB.insert(pair<int, int>(a, b));
    }
    while (1)
    {
        cin >> a;
        if (a == -1) {
            cout << -1;
            return 0;
        }
        it = AB.find(a);
        if (it == AB.end())
            cout << 0 << " ";
        else
            cout << it->second << " ";
    }
    return 0;
}
8
solved How can I still optimize the code?