#include <stdio.h>

#define CNT_INIT 0
#define LOOP_INIT 0
#define ARRAY_INIT 0
#define CARD_ARRAY_MAX 37
#define EXIT_SUCCESS 0
#define PREV_ITEM( x ) x - 1

int main( void )
{
	unsigned char ubW_Dcard_cnt;
	unsigned char aubW_Dbubble_mark[ CARD_ARRAY_MAX ] = { ARRAY_INIT };
	unsigned char aubW_Dbubble_num[ CARD_ARRAY_MAX ] = { ARRAY_INIT };
	unsigned char aubW_Dselect_mark[ CARD_ARRAY_MAX ] = { ARRAY_INIT };
	unsigned char aubW_Dselect_num[ CARD_ARRAY_MAX ] = { ARRAY_INIT };
	unsigned char ubW_Dtrade;
	unsigned char ubW_Dcard_min;
	unsigned char ubW_Cloop_cnt;
	unsigned char ubW_Cloop_sub_cnt;
	unsigned char ubW_Cstable_cnt = CNT_INIT;

	scanf( "%d", &ubW_Dcard_cnt );
	for( ubW_Cloop_cnt = LOOP_INIT; ubW_Cloop_cnt < ubW_Dcard_cnt; ubW_Cloop_cnt++ ){
		scanf( " %c%d", &aubW_Dbubble_mark[ ubW_Cloop_cnt ], &aubW_Dbubble_num[ ubW_Cloop_cnt ] );
		aubW_Dselect_mark[ ubW_Cloop_cnt ] = aubW_Dbubble_mark[ ubW_Cloop_cnt ];
		aubW_Dselect_num[ ubW_Cloop_cnt ] = aubW_Dbubble_num[ ubW_Cloop_cnt ];
	}

	/* ?????????????????? */
	for( ubW_Cloop_cnt = LOOP_INIT; ubW_Cloop_cnt < ubW_Dcard_cnt; ubW_Cloop_cnt++ ){
		for( ubW_Cloop_sub_cnt = PREV_ITEM( ubW_Dcard_cnt ); ubW_Cloop_sub_cnt > ubW_Cloop_cnt; ubW_Cloop_sub_cnt-- ){
			if( aubW_Dbubble_num[ ubW_Cloop_sub_cnt ] < aubW_Dbubble_num[ PREV_ITEM( ubW_Cloop_sub_cnt ) ] ){
				ubW_Dtrade = aubW_Dbubble_num[ ubW_Cloop_sub_cnt ];
				aubW_Dbubble_num[ ubW_Cloop_sub_cnt ] = aubW_Dbubble_num[ PREV_ITEM( ubW_Cloop_sub_cnt ) ];
				aubW_Dbubble_num[ PREV_ITEM( ubW_Cloop_sub_cnt ) ] = ubW_Dtrade;

				ubW_Dtrade = aubW_Dbubble_mark[ ubW_Cloop_sub_cnt ];
				aubW_Dbubble_mark[ ubW_Cloop_sub_cnt ] = aubW_Dbubble_mark[ PREV_ITEM( ubW_Cloop_sub_cnt ) ];
				aubW_Dbubble_mark[ PREV_ITEM( ubW_Cloop_sub_cnt ) ] = ubW_Dtrade;
			}
		}
	}
	/* ??????????????????????????? */

	/* ??????????????? */
	for( ubW_Cloop_cnt = LOOP_INIT; ubW_Cloop_cnt < ubW_Dcard_cnt; ubW_Cloop_cnt++ ){
		ubW_Dcard_min = ubW_Cloop_cnt;
		for( ubW_Cloop_sub_cnt = ubW_Cloop_cnt; ubW_Cloop_sub_cnt < ubW_Dcard_cnt; ubW_Cloop_sub_cnt++ ){
			if( aubW_Dselect_num[ ubW_Cloop_sub_cnt ] < aubW_Dselect_num[ ubW_Dcard_min ] ){
				ubW_Dcard_min = ubW_Cloop_sub_cnt;
			}
			ubW_Dtrade = aubW_Dselect_num[ ubW_Cloop_cnt ];
			aubW_Dselect_num[ ubW_Cloop_cnt ] = aubW_Dselect_num[ ubW_Dcard_min ];
			aubW_Dselect_num[ ubW_Dcard_min ] = ubW_Dtrade;

			ubW_Dtrade = aubW_Dselect_mark[ ubW_Cloop_cnt ];
			aubW_Dselect_mark[ ubW_Cloop_cnt ] = aubW_Dselect_mark[ ubW_Dcard_min ];
			aubW_Dselect_mark[ ubW_Dcard_min ] = ubW_Dtrade;
		}
	}
	/* ???????????????????????? */

	for( ubW_Cloop_cnt = LOOP_INIT; ubW_Cloop_cnt < ubW_Dcard_cnt; ubW_Cloop_cnt++ ){
		if( aubW_Dbubble_mark[ ubW_Cloop_cnt ] == aubW_Dselect_mark[ ubW_Cloop_cnt ] ){
			ubW_Cstable_cnt++;
		}
	}
	for( ubW_Cloop_cnt = LOOP_INIT; ubW_Cloop_cnt < ubW_Dcard_cnt; ubW_Cloop_cnt++ ){
		printf( "%c%d ", aubW_Dbubble_mark[ ubW_Cloop_cnt ], aubW_Dbubble_num[ ubW_Cloop_cnt ] );
	}
	printf( "\n" );
	printf("Stable\n");
	for( ubW_Cloop_cnt = LOOP_INIT; ubW_Cloop_cnt < ubW_Dcard_cnt; ubW_Cloop_cnt++ ){
		printf( "%c%d ", aubW_Dselect_mark[ ubW_Cloop_cnt ], aubW_Dselect_num[ ubW_Cloop_cnt ]);
	}
	printf( "\n" );
	if( ubW_Cstable_cnt == ubW_Dcard_cnt ){
		printf( "Stable\n" );
	}
	else{
		printf( "Not stable\n" );
	}
	return EXIT_SUCCESS;
}