#include <bits/stdc++.h>
using namespace std;

int main() {
#ifndef LOCAL
    ifstream cin("forta.in");
    ofstream cout("forta.out");
#endif

    int n;
    assert(cin >> n);
    assert(1 <= n && n <= 1000);
    vector<long long> nums(n);
    vector<vector<int>> v(n, vector<int>(10, 0));
    for (int i = 0; i < n; ++i) {
        long long x;
        assert(cin >> x);
        assert(0 <= x && x <= 1000000000000000000LL);
        nums[i] = x;

        if (x == 0) {
            v[i][0] = 1;
            continue;
        }

        while (x) {
            int c = x % 10;
            v[i][c]++;
            x /= 10;
        }
    }

    vector<int> force(n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (i == j) continue;
            for (int c = 0; c < 10; ++c) force[i] += min(v[i][c], v[j][c]);
        }
    }

    vector<int> indices(n);
    iota(indices.begin(), indices.end(), 0);
    sort(indices.begin(), indices.end(), [&](int a, int b) {
        return force[a] == force[b] ? a > b : force[a] < force[b];
    });

    for (int i = 0; i < n; ++i) cout << nums[indices[i]] << " \n"[i == n - 1];

    // sort(force.begin(), force.end());
    // for (int i = 1; i < n; ++i) assert(force[i] != force[i - 1]);

    return 0;
}
