using System;

namespace ALDS1_2_C
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = int.Parse(Console.ReadLine());
            string[] input = Console.ReadLine().Split(' ');
            string[] input2 = new string[num];
            string[] input_b = new string[num];
            string[] input_s = new string[num];
            input.CopyTo(input2, 0);
            input.CopyTo(input_b, 0);
            input.CopyTo(input_s, 0);
            
            BubbleSort(input_b, num);
            IsStable(input, input_b, num);
            
            SelectionSort(input_s, num);
            IsStable(input2, input_s, num);

        }

        static void BubbleSort(string[] str, int n)
        {
            for (int i=0; i<n; i++)
            {
                for (int j=n-1; j>i; j--)
                {
                    int a = int.Parse(str[j].Substring(1,1));
                    int b = int.Parse(str[j-1].Substring(1,1)); 
                    if (a < b)
                    {
                        string tmp = str[j];
                        str[j] = str[j-1];
                        str[j - 1] = tmp;
                    }
                }
            }

            for (int i=0; i<n; i++)
            {
                if (i != n-1)
                {
                    Console.Write("{0} ", str[i]);
                }
                else
                {
                    Console.Write(str[i]);
                    Console.WriteLine();
                }
            }
        }

        static void SelectionSort(string[] str, int n)
        {
            for (int i=0; i<n; i++)
            {
                int minj = i;
                for (int j=i; j<n; j++)
                {
                    int c = int.Parse(str[j].Substring(1,1));
                    int d = int.Parse(str[minj].Substring(1,1));

                    if (c < d) minj = j;
                }
                string tmp = str[i];
                str[i] = str[minj];
                str[minj] = tmp;
            }

            for (int i=0; i<n; i++)
            {
                if (i != n-1)
                {
                    Console.Write("{0} ", str[i]);
                }
                else
                {
                    Console.Write(str[i]);
                    Console.WriteLine();
                }
            }
        }

        static void IsStable(string[] input, string[] output, int n)
        {
            bool stable = true;
            for (int i=0; i<n; i++)
            {
                for (int j=i+1; j<n; j++)
                {
                    for (int a=0; a<n; a++)
                    {
                        for (int b=a+1; b<n; b++)
                        {
                            int num1 = int.Parse(input[i].Substring(1,1));
                            int num2 = int.Parse(input[j].Substring(1,1));

                            if (num1 == num2 && input[i] == output[b] && input[j] == output[a])
                            {
                                stable = false;
                                break;
                            }
                        }
                    }
                    if (stable == false) break;
                }
                if (stable == false) break;
            }

            if(stable == true)
            {
                Console.WriteLine("Stable");
            }
            else
            {
                Console.WriteLine("Not stable");
            }
        }
    }
}

Measured memory_bytes: 9072