#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string str[1000] = {};
bool cmp(const string& a, const string& b) {
// First, use the sign to determine the order
if (a[0] == '-' && b[0] != '-') {
return true;
} else if (a[0] != '-' && b[0] == '-') {
return false;
}
// Both numbers are positive
if (a[0] != '-' && b[0] != '-') {
if (a.length() != b.length()) {
return a.length() < b.length();
}
return a < b;
}
// Both numbers are negative
// Skip the '-' sign for comparison
string a_abs = a.substr(1);
string b_abs = b.substr(1);
if (a_abs.length() != b_abs.length()) {
return a_abs.length() > b_abs.length();
}
return a_abs > b_abs;
}
int main() {
int num = 0;
while (cin >> num) {
// Input
for (int i = 0; i < num; i++) {
cin >> str[i];
}
// Sort using the custom comparison function
sort(str, str + num, cmp);
// Output
for (int i = 0; i < num; i++) {
cout << str[i] << endl;
}
}
return 0;
}