import std.stdio;
import std.string;
import std.conv;
import std.algorithm;

class Card{
	private:
		string _name;
		string _mark;
		int _value;

	public:
		@property{
			string name() const { return _name; }
			int value() const { return _value; }
		}

		this(string name){
			_name = name;
			_mark = name[0..1];
			_value = name[1].to!int();
		}
}

void BubbleSort(Card[] arr, int n){
	for(int i = 0; i < n; ++i){
		for(int j = n-1; j > i; --j){
			if(arr[j].value < arr[j-1].value)
				swap(arr[j], arr[j-1]);
		}
	}
}

void SelectionSort(Card[] arr, int n){
	for(int i = 0; i < n; ++i){
		int minj = i;
		for(int j = i; j < n; ++j){
			if(arr[j].value < arr[minj].value)
				minj = j;
		}
		swap(arr[i], arr[minj]);
	}
}

int main(string[] argv)
{
	auto n = readln.chomp.to!int();
	Card[] b, s;

	string[] input = readln.split();
	for(int i = 0; i < n; i++){
		b ~= new Card(input[i]);
		s ~= new Card(input[i]);
	}

	BubbleSort(b, b.length.to!int());
	foreach(e; b){ writeln(e.name + " "); }
	writeln();
	writeln("Stable");
	SelectionSort(s, s.length.to!int());
	foreach(e; s){ writeln(e.name + " "); }
	writeln();

	for(int i = 0; i < n; ++i){
		if(b[i].name != s[i].name){
			writeln("Not stable");
			return 0;
		}
	}
	writeln("Stable");

    return 0;
}