import std.stdio;
import std.algorithm;
import std.conv;
import std.string;

void main() {
    int N = readln.chomp.to!int;
    auto cards = readln.chomp.split;
    auto cards2 = cards.dup;
    string b = cards.bubbleSort.join(" ");
    b.writeln;
    "Stable".writeln;
    string s = cards2.selectionSort.join(" ");
    s.writeln;
    writeln(s == b ? "Stable" : "Not stable");
}

string[] bubbleSort(string[] cc) {
    string[] c = cc;
    auto n = c.length;
    for (int i = 0; i < n - 1; i++) {
        for (int j = n.to!int - 1; j > i; j--) {
            if (c[j][1] < c[j - 1][1]) {
                swap(c[j], c[j - 1]);
            }
        }
    }

    return c;
}

string[] selectionSort(string[] cc) {
    string[] c = cc;
    auto n = c.length;
    for (int i = 0; i < n - 1; i++) {
        int minv = c[i][1];
        int minj = i;

        for (int j = i + 1; j < n; j++) {
            if (minv > c[j][1]) {
                minv = c[j][1];
                minj = j;
            }
        }

        swap(c[i], c[minj]);
    }

    return c;
}