#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a, const void *b)
{ return *(int *)a - *(int *)b; }
int main(){
int n, m, a, c, g;
char str[100][51], temp[51];
int count[100] = { 0 };
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++){
scanf("%s", str[i]);
a = c = g = 0;
for (int j = n - 1; j >= 0; j--) {
switch (str[i][j]){
case'A': a++; break;
case'C': c++; count[i] += a; break;
case'G': g++; count[i] += a + c; break;
default: count[i] += a + c + g; break;
}
}
count[i] = count[i] * 100 + i;//用个、十位存储字符串的秩
}
qsort(count, m, sizeof(int), cmp);
for (int i = 0; i < m; i++)
printf("%s\n", str[count[i] % 100]);
return 0;
}