#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

void print_cards(vector<string> &cards, int n)
{
	rep(i, n) {
		if (i) cout << " ";
		cout << cards[i];
	}
	cout << endl;
}

void bubble_sort(vector<string> &cards, int n)
{
	for (int i = 0; i < n; i++) {
		for (int j = n-1; j > i; j--) {
			if (cards[j-1][1] > cards[j][1])
				swap(cards[j-1], cards[j]);
		}
	}
}

void selection_sort(vector<string> &cards, int n)
{
	for (int i = 0; i < n-1; i++) {
		int minj = i;
		for (int j = i+1; j < n; j++) {
			if (cards[minj][1] > cards[j][1])
				minj = j;
		}
		swap(cards[i], cards[minj]);
	}
}

int main()
{
	int n;
	cin >> n;
	vector<string> a1(n), a2(n);
	for (int i = 0; i < n; i++)
		cin >> a1[i];
	a2 = a1;

	bubble_sort(a1, n);
	print_cards(a1, n);
	cout << "Stable" << endl;

	selection_sort(a2, n);
	print_cards(a2, n);

	bool stable = true;
	rep(i, n) {
		if (a1[i] != a2[i]) {
			stable = false;
			break;
		}
	}
	if (stable)
		cout << "Stable" << endl;
	else
		cout << "Not stable" << endl;
}

