#include<bits/stdc++.h>
#define ll long long
#define pp push_back
#define endl '\n'
#define all(x) x.begin(),x.end()
#define ld long double
#define PI acos(-1)
#define sin(a) sin((a)*PI/180)
#define cos(a) cos((a)*PI/180)
#define ones(x) __builtin_popcountll(x)
//#define int ll
using namespace std;
void Drakon() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef Clion
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
}
unsigned long long inf = 1e10;
const double EPS = 1e-6;
int MOD = 1000000007, N = 200005, LOG = 25;
ll mul(const ll &a, const ll &b) {
return (a % MOD + MOD) * (b % MOD + MOD) % MOD;
}
ll add(const ll &a, const ll &b) {
return (a + b + 2 * MOD) % MOD;
}
ll pw(ll x, ll y) {
ll ret = 1;
while (y > 0) {
if (y % 2 == 0) {
x = mul(x, x);
y = y / 2;
} else {
ret = mul(ret, x);
y = y - 1;
}
}
return ret;
}
#define rep(aa, bb, cc) for(int aa = bb; aa < cc;aa++)
struct Dinic {
struct Edge {
int to, rev;
ll c, oc;
ll flow() { return max(oc - c, 0LL); } // if you need flows
};
vector<int> lvl, ptr, q;
vector<vector<Edge>> adj;
Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {}
void addEdge(int a, int b, ll c, ll rcap = 0) {
adj[a].push_back({b, (int)adj[b].size(), c, c});
adj[b].push_back({a, (int)adj[a].size() - 1, rcap, rcap});
}
ll dfs(int v, int t, ll f) {
if (v == t || !f) return f;
for (int& i = ptr[v]; i < (int)adj[v].size(); i++) {
Edge& e = adj[v][i];
if (lvl[e.to] == lvl[v] + 1)
if (ll p = dfs(e.to, t, min(f, e.c))) {
e.c -= p, adj[e.to][e.rev].c += p;
return p;
}
}
return 0;
}
ll calc(int s, int t) {
ll flow = 0; q[0] = s;
rep(L,0,31) do { // 'int L=30' maybe faster for random data
lvl = ptr = vector<int>((int)q.size());
int qi = 0, qe = lvl[s] = 1;
while (qi < qe && !lvl[t]) {
int v = q[qi++];
for (Edge e : adj[v])
if (!lvl[e.to] && e.c >> (30 - L))
q[qe++] = e.to, lvl[e.to] = lvl[v] + 1;
}
while (ll p = dfs(s, t, LLONG_MAX)) flow += p;
} while (lvl[t]);
return flow;
}
bool leftOfMinCut(int a) { return lvl[a] != 0; }
};
vector<int> can;
bool vis[505];
int n, k, m, possible[505];
void dfs(int u) {
if(vis[u])return;
vis[u] = true;
can.push_back(u);
for (int i = 0; i < m; ++i) {
if(~possible[i])
dfs((u + i) % m);
}
}
vector<int> cur, path;
void getPath(int u, int want){
if(vis[u])return;
vis[u] = true;
if(u == want) {
path = cur;
return;
}
for (int i = 0; i < m; ++i) {
if(~possible[i]){
cur[possible[i]] ++;
getPath((u + i) % m, want);
cur[possible[i]] --;
}
}
}
void solve() {
cin >> n >> k >> m;
MOD = m;
char c[n][k];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
cin >> c[i][j];
}
}
Dinic dinic(n + m + 2);
int s = n + m, e = n + m + 1;
for (int i = 0; i < n; ++i) {
can.clear();
for (int j = 0; j < m; ++j) {
vis[j] = false;
possible[j] = -1;
}
for (int j = 0; j < k; ++j) {
possible[pw(c[i][j] - 'a' + 1, j + 1)] = j;
}
dfs(0);
dinic.addEdge(s, i, 1);
for (int j = 0; j < can.size(); ++j) {
dinic.addEdge(i, n + can[j], 1);
}
}
for (int i = 0; i < m; ++i) {
dinic.addEdge(n + i, e, 1);
}
int mx = dinic.calc(s, e);
if(mx < n){
cout << "No\n";
return;
}
cout << "Yes\n";
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
vis[j] = false;
possible[j] = -1;
}
for (int j = 0; j < k; ++j) {
possible[pw(c[i][j] - 'a' + 1, j + 1)] = j;
}
for(auto md : dinic.adj[i]){
if(md.c == 0){
path.clear();
cur = vector<int>(k);
getPath(0, md.to - n);
break;
}
}
for(auto x : path){
cout << x << ' ';
}
cout << endl;
}
}
signed main() {
Drakon();
freopen("hash.in", "r", stdin);
int t = 1;
cin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ICAgd2hpbGUgKHkgPiAwKSB7CiAgICAgICAgaWYgKHkgJSAyID09IDApIHsKICAgICAgICAgICAgeCA9IG11bCh4LCB4KTsKICAgICAgICAgICAgeSA9IHkgLyAyOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHJldCA9IG11bChyZXQsIHgpOwogICAgICAgICAgICB5ID0geSAtIDE7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIHJldDsKfQoKI2RlZmluZSByZXAoYWEsIGJiLCBjYykgZm9yKGludCBhYSA9IGJiOyBhYSA8IGNjO2FhKyspCnN0cnVjdCBEaW5pYyB7CiAgICBzdHJ1Y3QgRWRnZSB7CiAgICAgICAgaW50IHRvLCByZXY7CiAgICAgICAgbGwgYywgb2M7CiAgICAgICAgbGwgZmxvdygpIHsgcmV0dXJuIG1heChvYyAtIGMsIDBMTCk7IH0gLy8gaWYgeW91IG5lZWQgZmxvd3MKICAgIH07CiAgICB2ZWN0b3I8aW50PiBsdmwsIHB0ciwgcTsKICAgIHZlY3Rvcjx2ZWN0b3I8RWRnZT4+IGFkajsKICAgIERpbmljKGludCBuKSA6IGx2bChuKSwgcHRyKG4pLCBxKG4pLCBhZGoobikge30KICAgIHZvaWQgYWRkRWRnZShpbnQgYSwgaW50IGIsIGxsIGMsIGxsIHJjYXAgPSAwKSB7CiAgICAgICAgYWRqW2FdLnB1c2hfYmFjayh7YiwgKGludClhZGpbYl0uc2l6ZSgpLCBjLCBjfSk7CiAgICAgICAgYWRqW2JdLnB1c2hfYmFjayh7YSwgKGludClhZGpbYV0uc2l6ZSgpIC0gMSwgcmNhcCwgcmNhcH0pOwogICAgfQogICAgbGwgZGZzKGludCB2LCBpbnQgdCwgbGwgZikgewogICAgICAgIGlmICh2ID09IHQgfHwgIWYpIHJldHVybiBmOwogICAgICAgIGZvciAoaW50JiBpID0gcHRyW3ZdOyBpIDwgKGludClhZGpbdl0uc2l6ZSgpOyBpKyspIHsKICAgICAgICAgICAgRWRnZSYgZSA9IGFkalt2XVtpXTsKICAgICAgICAgICAgaWYgKGx2bFtlLnRvXSA9PSBsdmxbdl0gKyAxKQogICAgICAgICAgICAgICAgaWYgKGxsIHAgPSBkZnMoZS50bywgdCwgbWluKGYsIGUuYykpKSB7CiAgICAgICAgICAgICAgICAgICAgZS5jIC09IHAsIGFkaltlLnRvXVtlLnJldl0uYyArPSBwOwogICAgICAgICAgICAgICAgICAgIHJldHVybiBwOwogICAgICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIGxsIGNhbGMoaW50IHMsIGludCB0KSB7CiAgICAgICAgbGwgZmxvdyA9IDA7IHFbMF0gPSBzOwogICAgICAgIHJlcChMLDAsMzEpIGRvIHsgLy8gJ2ludCBMPTMwJyBtYXliZSBmYXN0ZXIgZm9yIHJhbmRvbSBkYXRhCiAgICAgICAgICAgICAgICBsdmwgPSBwdHIgPSB2ZWN0b3I8aW50PigoaW50KXEuc2l6ZSgpKTsKICAgICAgICAgICAgICAgIGludCBxaSA9IDAsIHFlID0gbHZsW3NdID0gMTsKICAgICAgICAgICAgICAgIHdoaWxlIChxaSA8IHFlICYmICFsdmxbdF0pIHsKICAgICAgICAgICAgICAgICAgICBpbnQgdiA9IHFbcWkrK107CiAgICAgICAgICAgICAgICAgICAgZm9yIChFZGdlIGUgOiBhZGpbdl0pCiAgICAgICAgICAgICAgICAgICAgICAgIGlmICghbHZsW2UudG9dICYmIGUuYyA+PiAoMzAgLSBMKSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHFbcWUrK10gPSBlLnRvLCBsdmxbZS50b10gPSBsdmxbdl0gKyAxOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgd2hpbGUgKGxsIHAgPSBkZnMocywgdCwgTExPTkdfTUFYKSkgZmxvdyArPSBwOwogICAgICAgICAgICB9IHdoaWxlIChsdmxbdF0pOwogICAgICAgIHJldHVybiBmbG93OwogICAgfQogICAgYm9vbCBsZWZ0T2ZNaW5DdXQoaW50IGEpIHsgcmV0dXJuIGx2bFthXSAhPSAwOyB9Cn07Cgp2ZWN0b3I8aW50PiBjYW47CmJvb2wgdmlzWzUwNV07CmludCBuLCBrLCBtLCBwb3NzaWJsZVs1MDVdOwoKdm9pZCBkZnMoaW50IHUpIHsKICAgIGlmKHZpc1t1XSlyZXR1cm47CiAgICB2aXNbdV0gPSB0cnVlOwogICAgY2FuLnB1c2hfYmFjayh1KTsKCiAgICBmb3IgKGludCBpID0gMDsgaSA8IG07ICsraSkgewogICAgICAgIGlmKH5wb3NzaWJsZVtpXSkKICAgICAgICAgICAgZGZzKCh1ICsgaSkgJSBtKTsKICAgIH0KfQoKdmVjdG9yPGludD4gY3VyLCBwYXRoOwoKdm9pZCBnZXRQYXRoKGludCB1LCBpbnQgd2FudCl7CiAgICBpZih2aXNbdV0pcmV0dXJuOwogICAgdmlzW3VdID0gdHJ1ZTsKICAgIGlmKHUgPT0gd2FudCkgewogICAgICAgIHBhdGggPSBjdXI7CiAgICAgICAgcmV0dXJuOwogICAgfQoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbTsgKytpKSB7CiAgICAgICAgaWYofnBvc3NpYmxlW2ldKXsKICAgICAgICAgICAgY3VyW3Bvc3NpYmxlW2ldXSArKzsKICAgICAgICAgICAgZ2V0UGF0aCgodSArIGkpICUgbSwgd2FudCk7CiAgICAgICAgICAgIGN1cltwb3NzaWJsZVtpXV0gLS07CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIHNvbHZlKCkgewogICAgY2luID4+IG4gPj4gayA+PiBtOwogICAgTU9EID0gbTsKICAgIGNoYXIgY1tuXVtrXTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBrOyArK2opIHsKICAgICAgICAgICAgY2luID4+IGNbaV1bal07CiAgICAgICAgfQogICAgfQoKICAgIERpbmljIGRpbmljKG4gKyBtICsgMik7CiAgICBpbnQgcyA9IG4gKyBtLCBlID0gbiArIG0gKyAxOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpIHsKICAgICAgICBjYW4uY2xlYXIoKTsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IG07ICsraikgewogICAgICAgICAgICB2aXNbal0gPSBmYWxzZTsKICAgICAgICAgICAgcG9zc2libGVbal0gPSAtMTsKICAgICAgICB9CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBrOyArK2opIHsKICAgICAgICAgICAgcG9zc2libGVbcHcoY1tpXVtqXSAtICdhJyArIDEsIGogKyAxKV0gPSBqOwogICAgICAgIH0KCiAgICAgICAgZGZzKDApOwoKICAgICAgICBkaW5pYy5hZGRFZGdlKHMsIGksIDEpOwogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgY2FuLnNpemUoKTsgKytqKSB7CiAgICAgICAgICAgIGRpbmljLmFkZEVkZ2UoaSwgbiArIGNhbltqXSwgMSk7CiAgICAgICAgfQogICAgfQoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbTsgKytpKSB7CiAgICAgICAgZGluaWMuYWRkRWRnZShuICsgaSwgZSwgMSk7CiAgICB9CgogICAgaW50IG14ID0gZGluaWMuY2FsYyhzLCBlKTsKICAgIGlmKG14IDwgbil7CiAgICAgICAgY291dCA8PCAiTm9cbiI7CiAgICAgICAgcmV0dXJuOwogICAgfQoKICAgIGNvdXQgPDwgIlllc1xuIjsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CgogICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgbTsgKytqKSB7CiAgICAgICAgICAgIHZpc1tqXSA9IGZhbHNlOwogICAgICAgICAgICBwb3NzaWJsZVtqXSA9IC0xOwogICAgICAgIH0KICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IGs7ICsraikgewogICAgICAgICAgICBwb3NzaWJsZVtwdyhjW2ldW2pdIC0gJ2EnICsgMSwgaiArIDEpXSA9IGo7CiAgICAgICAgfQoKICAgICAgICBmb3IoYXV0byBtZCA6IGRpbmljLmFkaltpXSl7CiAgICAgICAgICAgIGlmKG1kLmMgPT0gMCl7CiAgICAgICAgICAgICAgICBwYXRoLmNsZWFyKCk7CiAgICAgICAgICAgICAgICBjdXIgPSB2ZWN0b3I8aW50PihrKTsKICAgICAgICAgICAgICAgIGdldFBhdGgoMCwgbWQudG8gLSBuKTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICBmb3IoYXV0byB4IDogcGF0aCl7CiAgICAgICAgICAgIGNvdXQgPDwgeCA8PCAnICc7CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgZW5kbDsKICAgIH0KfQoKc2lnbmVkIG1haW4oKSB7CiAgICBEcmFrb24oKTsKICAgIGZyZW9wZW4oImhhc2guaW4iLCAiciIsIHN0ZGluKTsKICAgIGludCB0ID0gMTsKICAgIGNpbiA+PiB0OwogICAgd2hpbGUgKHQtLSkgewogICAgICAgIHNvbHZlKCk7CiAgICB9Cn0=