using System;
using System.Collections.Generic;
using System.Linq;

namespace AizuOnlineJudge
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
            var arr = Console.ReadLine().Split(' ').Select((e, i) => new Card(e, i)).ToArray();

            var bubbleArr = arr.ToArray();

            BubbleSort(ref bubbleArr); Console.WriteLine(string.Join<Card>(" ", bubbleArr));
            StableCheck(bubbleArr);
            SelectionSort(ref arr); Console.WriteLine(string.Join<Card>(" ", arr));
            StableCheck(arr);
            
            
        }

        private static void StableCheck(Card[] array)
        {
            for(var i = 0; i < array.Length; ++i)
            {
                for(var k = i + 1; k < array.Length && array[k].SecondaryNumber == array[i].SecondaryNumber; ++k)
                {
                    if(array[i].InputOrder > array[k].InputOrder)
                    {
                        Console.WriteLine("Not stable"); return;
                    }
                }
            }
            Console.WriteLine("Stable");
        }

        private class Card : IComparable<Card>
        {
            private static Dictionary<char, int> PrimarySelector { set; get; } = new Dictionary<char, int>();

            public int PrimaryNumber { private set; get; }
            public int SecondaryNumber { private set; get; }
            public int InputOrder { private set; get; }
            private string str;

            static Card()
            {
                PrimarySelector.Add('S', 0);
                PrimarySelector.Add('H', 1);
                PrimarySelector.Add('C', 2);
                PrimarySelector.Add('D', 3);
            }

            public Card(string e, int i)
            {
                PrimaryNumber = PrimarySelector[e[0]];
                SecondaryNumber = e[1] - '0';
                InputOrder = i;
                str = e;
            }

            public override string ToString()
            {
                return str;
            }

            public int CompareTo(Card other)
            {
                //if (PrimaryNumber < other.PrimaryNumber) return -1;
                //if (PrimaryNumber > other.PrimaryNumber) return 1;
                if (SecondaryNumber < other.SecondaryNumber) return -1;
                if (SecondaryNumber > other.SecondaryNumber) return 1;
                return 0;
            }
        }

        private static void BubbleSort<T>(ref T[] arr) where T : IComparable<T>
        {
             for(var i = 0; i < arr.Length; ++i)
            {
                for(var j = arr.Length - 1; j >= i + 1; --j)
                {
                    if (arr[j].CompareTo(arr[j - 1]) < 0) Swap(ref arr[j], ref arr[j - 1]);
                }
            }
        }

        private static void SelectionSort<T>(ref T[] arr) where T : IComparable<T>
        {
            for (var i = 0; i < arr.Length; ++i)
            {
                var minIndex = i;
                for (var k = i; k < arr.Length; ++k)
                {
                    if (arr[k].CompareTo(arr[minIndex]) < 0) minIndex = k;
                }
                if (minIndex != i) Swap(ref arr[i], ref arr[minIndex]);
            }
        }

        private static void Swap<T>(ref T a, ref T b)
        {
            var t = a;
            a = b;
            b = t;
        }
    }
}


Measured memory_bytes: 20256