#include <iostream>
#include <algorithm>
using namespace std;
string sarr[1000];
int n;
int getSum(string s) {
int length = s.length();
int sum = 0;
for (int i = 0; i < length; i++) {
if (s[i] - '0' <= 9 && s[i] - '0' >= 0)
sum += s[i] - '0';
}
return sum;
}
bool cmp(string a, string b) {
if (a.length() < b.length())
return 1;
else if (a.length() > b.length())
return 0;
else {
int aSum = getSum(a);
int bSum = getSum(b);
if (aSum != bSum) {
return aSum < bSum;
}
else {
return a < b;
}
}
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> sarr[i];
}
sort(sarr, sarr + n, cmp);
for (int i = 0; i < n; i++)
cout << sarr[i] << endl;
}