#include <iostream>
#include <stack>
#include <string>
#include <queue>
using namespace std;
void ordena(queue<string> v, queue<string> &K);

void elimina(queue<string> &v, queue<string> K){ 
	
	if(K.empty()) return;
	
	if(K.front()==v.front()){
		K.pop();
		v.pop();
	}
	else{
		v.push(v.front());
		v.pop();
	}
	elimina(v,K);
}

void mostra(queue<string> K){
	while(K.empty()==false){
	    cout<<K.front()<<" ";
	    K.pop();
	}
    cout<<endl;
}


int main(){

	string s;
	queue<string> v;
	queue<string> v1;
	queue<string> KC;
	queue<string> KD;
	queue<string> KP;
	queue<string> KT;
	int n;
	cin>>n;
	
	for(int i=0; i<n; i++){
	    cin>>s;
	    v.push(s);
		if(s=="KC") KC.push(s);
		if(s=="KD") KD.push(s);
		if(s=="KP") KP.push(s);
		if(s=="KT") KT.push(s);
	}

    cout<<endl;  			//deixa un espai entre l'input i output
	if(KC.empty()==false){
	    ordena(v, KC);
	    mostra(KC);
		elimina(v,KC);
	}
	
	if(KD.empty()==false){
	    ordena(v, KD);
	    mostra(KD);
		elimina(v,KD);
	}
	
	if(KP.empty()==false){
	    ordena(v, KP);
	    mostra(KP);
		elimina(v,KP);
	}
	
	if(KT.empty()==false){
	    ordena(v, KT);
	    mostra(KT);
		elimina(v,KT);
	}

	if(KC.empty() and KD.empty() and KP.empty() and KT.empty()) cout<<"No s'ha pogut ordenar cap element."<<endl;
}