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



const bool adhoc_comparator(const string &a, const string &b) {
    ll check_len = min(a.size(), b.size());
    for (ll i = 0; i < check_len; i++) {
        if (a[i] != b[i]) {
            return a[i] > b[i];
        }
    }
    if (a.size() < b.size()){
        adhoc_comparator(a, b.substr(check_len));
    }
    else if (a.size() > b.size()) {
        adhoc_comparator(a.substr(check_len), b);
    }
    else {
        return false;
    }
}


int main() {
    ll n, k;
    cin >> n >> k;
    vector<string> v(n);
    for (ll i = 0; i < n; i++) {
        cin >> v[i];
    }
    sort(v.begin(), v.end(), adhoc_comparator);
    for (ll i = 0; i < k; i++) {
        for (ll j = 0; j < n; j++) {
            cout << v[j];
        }
        if (i != k-1) cout << "\n";
        next_permutation(v.begin(), v.end(), adhoc_comparator);
    }
    cout << flush;
}