#include <iostream>
#include <string>
using namespace std;
class Car {
public:
void setting ( string bra, string col );
string getBrand ( void );
string getColor ( void );
private:
string brand;
string color;
};
void Car::setting ( string bra, string col ) {
this->brand = bra;
this->color = col;
}
inline string Car::getBrand() {
return brand;
}
inline string Car::getColor() {
return color;
}
int main() {
int n, m;
string bra, col, cmd;
Car parkingLotA[21];
while ( cin >> n >> m ) {
for ( int i = 0; i < n; ++i ) {
cin >> bra >> col;
parkingLotA[i].setting ( bra, col );
}
for ( int j = 0; j < m; ++j ) {
cin >> cmd;
if ( cmd == "brand" ) {
cin >> bra;
for ( int i = 0; i < n; ++i )
if ( parkingLotA[i].getBrand() == bra )
cout << parkingLotA[i].getBrand() << " " << parkingLotA[i].getColor() << endl;
} else {
cin >> col;
for ( int i = 0; i < n; ++i )
if ( parkingLotA[i].getColor() == col )
cout << parkingLotA[i].getBrand() << " " << parkingLotA[i].getColor() << endl;
}
}
}
return 0;
}