import std.stdio, std.string, std.array, std.conv;

struct Card {
    char suit, value;
}

void bubble(int n, Card[] x) {
    foreach (i; 0 .. n) {
        foreach_reverse (j; i+1 .. n) {
            if (x[j].value < x[j-1].value) {
                Card t = x[j];
                x[j] = x[j-1];
                x[j-1] = t;
            }
        }
    }
}

void selection(int n, Card[] x) {
    foreach (i; 0 .. n) {
        int minj = i;
        foreach (j; i .. n) {
            if (x[j].value < x[minj].value) {
                minj = j;
            }
        }
        Card t = x[i];
        x[i] = x[minj];
        x[minj] = t;
    }
}

bool isStable(int n, Card[] x, Card[] y) {
    foreach (i; 0 .. n) {
        if (x[i].suit != y[i].suit) return false;
    }
    return true;
}

void main() {
    int n = readln.chomp.to!int;
    string[] _a = readln.chomp.split;
    Card[] a, b;
    foreach (x; _a) {
        a ~= Card(x[0], x[1]);
        b ~= Card(x[0], x[1]);
    }
    bubble(n, a);
    selection(n, b);
    foreach (i, x; a) {
        write(x.suit, x.value);
        if (i != n - 1) " ".write;
        else writeln;
    }
    "Stable".writeln;
    foreach (j, y; b) {
        write(y.suit, y.value);
        if (j != n - 1) " ".write;
        else writeln;
    }
    writeln(isStable(n, a, b) ? "Stable" : "Not stable");
}
