#[allow(unused_imports)]
use std::io;
#[allow(unused_imports)]
use std::cmp::max;
#[allow(unused_imports)]
use std::cmp::min;
#[allow(unused_imports)]
use std::cell::RefCell;

#[allow(unused_macros)]
macro_rules! get {
    ($i:ident) => {
        $i = String::new();
        io::stdin().read_line(&mut $i).ok();
    };
}

#[allow(unused_macros)]
macro_rules! to_vec {
    ($s:ident, $t:ty) => {
        $s.split_whitespace().map(|i| i.parse().ok().unwrap()).collect::<Vec<$t>>()
    }
}

#[allow(unused_macros)]
macro_rules! parse {
    ($s:ident, $t:ty) => {
        $s.trim().parse::<$t>().ok().unwrap()
    }
}

#[allow(unused_macros)]
macro_rules! max {
    ($v:ident) => {
        $v.iter().max().unwrap()
    };
}

#[allow(unused_macros)]
macro_rules! min {
    ($v:ident) => {
        $v.iter().max().unwrap()
    };
}

#[allow(unused_macros)]
macro_rules! sort {
    ($v:ident) => {
        $v.as_mut_slice().sort()
    };
}

#[derive(PartialEq, Clone, Copy)]
enum Suit {
    S,
    H,
    C,
    D,
    Unknown,
}

#[derive(Clone, Copy)]
struct Card {
    s: Suit,
    n: u32,
}

fn to_card(mut a: Vec<String>, n: usize) -> Vec<Card> {
    let mut v: Vec<Card> = Vec::new();
    for i in 0..n {
        let cs = a.get_mut(i).unwrap();
        let n = cs.pop().unwrap().to_digit(10).unwrap();
        let s = match cs.pop().unwrap() {
            'S' => Suit::S,
            'H' => Suit::H,
            'C' => Suit::C,
            'D' => Suit::D,
            _ => Suit::Unknown,
        };

        v.push(Card { s, n });
    }

    v
}

#[allow(unused_variables)]
#[allow(unused_mut)]
fn bubble_sort(mut a: Vec<Card>, n: usize) -> Vec<Card> {
    let mut flag = true;
    while flag {
        flag = false;
        for j in 0..(n - 1) {
            if a[j].n > a[j + 1].n {
                a.swap(j, j + 1);
                flag = true;
            }
        }
    }
    
    return a;
}

#[allow(unused_variables)]
#[allow(unused_mut)]
fn selection_sort(mut a: Vec<Card>, n: usize) -> Vec<Card> {
    for i in 0..n {
        let mut minj = i;
        for j in i..n {
            if a[j].n < a[minj].n {
                minj = j;
            }
        }
        a.swap(i, minj);
    }

    return a;
}

#[allow(unused_variables)]
#[allow(unused_mut)]
fn cmp_card(a: Vec<Card>, b: Vec<Card>, n: usize) -> bool {
    for i in 0..n {
        let ai = a.get(i).unwrap();
        let bi = b.get(i).unwrap();
        if ai.n != bi.n || ai.s != bi.s {
            return false;
        }
    }

    return true;
}

#[allow(unused_variables)]
#[allow(unused_mut)]
fn print_card(a: &Vec<Card>, n: usize) {
    for i in 0..n {
        let e = a.get(i).unwrap();
        let mut s = String::new();
        s.push(
            match e.s {
                Suit::S => 'S',
                Suit::H => 'H',
                Suit::C => 'C',
                Suit::D => 'D',
                _ => 'U',
            }
        );
        s.push((e.n as u8 + ('0' as u8)) as char);

        print!("{}", s);
        if i != (n - 1) {
            print!(" ");
        } else {
            println!();
        }
    }
}

#[allow(unused_variables)]
#[allow(unused_mut)]
fn main() {
    let mut s: String;
    get!(s);
    let n = parse!(s, usize);
    get!(s);
    let mut o = to_vec!(s, String);
    let mut v = RefCell::new(to_card(o, n));
    let a = bubble_sort(v.borrow_mut().to_vec(), n);
    print_card(&a, n);
    println!("Stable");
    let b = selection_sort(v.borrow_mut().to_vec(), n);
    print_card(&b, n);
    if cmp_card(a, b, n) {
        println!("Stable");
    } else {
        println!("Not stable");
    }
}


Measured memory_bytes: 5032