Problem:
Stable Sort

Let's arrange a deck of cards. There are totally 36 cards of 4 suits(S, H, C, D) and 9 values (1, 2, ... 9). For example, 'eight of heart' is represented by H8 and 'one of diamonds' is represented by D1.

Your task is to write a program which sorts a given set of cards in ascending order by their values using the Bubble Sort algorithms and the Selection Sort algorithm respectively. These algorithms should be based on the following pseudocode:

BubbleSort(C)
1 for i = 0 to C.length-1
2 for j = C.length-1 downto i+1
3 if C[j].value < C[j-1].value
4 swap C[j] and C[j-1]

SelectionSort(C)
1 for i = 0 to C.length-1
2 mini = i
3 for j = i to C.length-1
4 if C[j].value < C[mini].value
5 mini = j
6 swap C[i] and C[mini]

Note that, indices for array elements are based on 0-origin.

For each algorithm, report the stability of the output for the given input (instance). Here, 'stability of the output' means that: cards with the same value appear in the output in the same order as they do in the input (instance).

Input

The first line contains an integer N, the number of cards.

N cards are given in the following line. Each card is represented by two characters. Two consecutive cards are separated by a space character.

Output

In the first line, print the arranged cards provided by the Bubble Sort algorithm. Two consecutive cards should be separated by a space character.

In the second line, print the stability ("Stable" or "Not stable") of this output.

In the third line, print the arranged cards provided by the Selection Sort algorithm. Two consecutive cards should be separated by a space character.

In the fourth line, print the stability ("Stable" or "Not stable") of this output.

Constraints

1 ≤ N ≤ 36

Sample Input 1

5
H4 C9 S4 D2 C3

Sample Output 1

D2 C3 H4 S4 C9
Stable
D2 C3 S4 H4 C9
Not stable

Sample Input 2

2
S1 H1

Sample Output 2

S1 H1
Stable
S1 H1
Stable

Solution in C# (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Numerics;
using System.IO;
using System.Runtime.InteropServices;
using static System.Math;
using static io;
public class CompetitiveProgramming
{
  public void Solve()
  {
    var N = Int1;
    var A = Strs;
    bubble(A, N);
    selection(A, N);
  }

  public void bubble(string[] A, int N)
  {
    var s = A.ToArray();
    var t = A.ToArray();
    bool flg = true;
    while (flg)
    {
      flg = false;
      for (int j = N - 1; j > 0; --j)
      {
        if ((int)(t[j - 1][1]) > (int)(t[j][1]))
        {
          var x = t[j - 1];
          t[j - 1] = t[j];
          t[j] = x;
          flg = true;
        }
      }
    }
    Write(string.Join(" ", t));
    Write(isStable(s, t) ? "Stable" : "Not stable");
  }
  public void selection(string[] A, int N)
  {
    var s = A.ToArray();
    var t = A.ToArray();
    for (int i = 0; i < N; ++i)
    {
      int min = i;
      for (int j = i + 1; j < N; ++j)
      {
        if (t[min][1] > t[j][1])
        {
          min = j;
        }
      }
      var x = t[min];
      t[min] = t[i];
      t[i] = x;

    }
    Write(string.Join(" ", t));
    Write(isStable(s, t) ? "Stable" : "Not stable");
  }
  public bool isStable(string[] A, string[] B)
  {
    for (int i = 0; i < A.Length; ++i)
    {
      for (int j = i + 1; j < A.Length; ++j)
      {
        for (int k = 0; k < A.Length; ++k)
        {
          for (int l = k + 1; l < A.Length; ++l)
          {
            if (A[i][1] == A[j][1] && A[i] == B[l] && A[j] == B[k])
            {
              return false;
            }
          }
        }
      }
    }
    return true;
  }
}

class BIT
{

}



class Program
{
  static void Main(string[] args)
  {
    var CompetitiveProgramming = new CompetitiveProgramming();
    CompetitiveProgramming.Solve();
  }
}
public static class io
{
  public static string Str => Console.ReadLine();
  public static string[] Strs => Str.Split(' ');
  public static long[] Longs => Strs.Select(long.Parse).ToArray();
  public static int[] Ints => Strs.Select(int.Parse).ToArray();
  public static char[] Chars => Str.ToArray();
  public static double[] Doubles => Strs.Select(double.Parse).ToArray();
  public static long Long1 => Longs[0];
  public static int Int1 => Ints[0];
  public static char Char1 => Chars[0];
  public static double Double1 => Doubles[0];
  public static long[] VerticalRead(int N) { long[] A = new long[N]; for (int i = 0; i < N; ++i) A[i] = Long1; return A; }
  public static void Write(string a) => Console.WriteLine(a);
  public static void Write(params object[] i) => Write(string.Join(" ", i));
  public static void Write<T>(IEnumerable<T> a) => Write(string.Join(" ", a));
  public static void Verticalwrite<T>(IEnumerable<T> a) { foreach (var z in a) Write(z); }
  public static void YN(bool i) { if (i) Write("Yes"); else Write("No"); }
}