# include <iostream>
# include <string>
using namespace std;
int main(void)
{
	int n,ans = 0;
	string a[100],b[100];
	scanf("%d",&n);
	for(int i = 0;i < n;i++)
	{
		cin>>a[i];
		b[i] = a[i];
	}
	for(int i = 0;i < n-1;i++)
	{
		for(int j = n-1;j > i;j--)
			if(a[j][1] < a[j-1][1]) swap(a[j],a[j-1]);
	}
	for(int i = 0;i < n;i++) cout<<a[i]<<(i == n-1?"\n":" ");
	cout<<"Stable"<<endl;
	for(int i = 0;i < n-1;i++)
	{
		int index = i;
		for(int j = i+1;j < n;j++)
			if(b[j][1] < b[index][1]) index = j;
		swap(b[i],b[index]);
	}
	for(int i = 0;i < n;i++) cout<<b[i]<<(i == n-1?"\n":" ");
	for(int i = 0;i < n;i++)
	{
		if(b[i] != a[i])
		{
			cout<<"Not stable"<<endl;
			return 0;
		}
	}
	cout<<"Stable"<<endl;
	return 0;
}
