#include<bits/stdc++.h>
using namespace std;
int ts[1100];
int main(){
int n, q;
cin >> n >> q;
for(int i = 1; i <= n; i ++) cin >> ts[i];
sort(ts + 1, ts + n + 1);
int Has = 0, l, xq;
for(int i = 1; i <= q; i ++){
cin >> l >> xq;
Has = 0;
l = pow(10, l);
for(int j = 1; j <= n; j ++){
if(ts[j] % l == xq){
cout << ts[j] << endl;
Has = 1;
break;
}
}
if(!Has) cout << "-1" << endl;
}
return 0;
}