#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt;
cin >> tt;
while (tt--) {
string s;
cin >> s;
vector<bool> dp(9);
dp[0] = true;
for (char c : s) {
vector<bool> new_dp(9);
for (int x = 0; x < 9; x++) {
if (dp[x]) {
int d = int(c - '0');
while (d <= 9) {
new_dp[(x + d) % 9] = true;
if (d <= 1) {
break;
}
d = d * d;
}
}
}
for(int i: new_dp){
cout << i << " ";
}cout << endl;
swap(dp, new_dp);
}
cout << (dp[0] ? "YES" : "NO") << '\n';
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAojaWZkZWYgTE9DQUwKI2luY2x1ZGUgImFsZ28vZGVidWcuaCIKI2Vsc2UKI2RlZmluZSBkZWJ1ZyguLi4pIDQyCiNlbmRpZgogCmludCBtYWluKCkgewogIGlvczo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsKICBjaW4udGllKG51bGxwdHIpOwogIGludCB0dDsKICBjaW4gPj4gdHQ7CiAgd2hpbGUgKHR0LS0pIHsKICAgIHN0cmluZyBzOwogICAgY2luID4+IHM7CiAgICB2ZWN0b3I8Ym9vbD4gZHAoOSk7CiAgICBkcFswXSA9IHRydWU7CiAgICBmb3IgKGNoYXIgYyA6IHMpIHsKICAgICAgdmVjdG9yPGJvb2w+IG5ld19kcCg5KTsKICAgICAgZm9yIChpbnQgeCA9IDA7IHggPCA5OyB4KyspIHsKICAgICAgICBpZiAoZHBbeF0pIHsKICAgICAgICAgIGludCBkID0gaW50KGMgLSAnMCcpOwogICAgICAgICAgd2hpbGUgKGQgPD0gOSkgewogICAgICAgICAgICBuZXdfZHBbKHggKyBkKSAlIDldID0gdHJ1ZTsKICAgICAgICAgICAgaWYgKGQgPD0gMSkgewogICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGQgPSBkICogZDsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgZm9yKGludCBpOiBuZXdfZHApewogICAgICAJY291dCA8PCBpIDw8ICIgIjsKICAgICAgfWNvdXQgPDwgZW5kbDsgCiAgICAgIHN3YXAoZHAsIG5ld19kcCk7CiAgICB9CiAgICBjb3V0IDw8IChkcFswXSA/ICJZRVMiIDogIk5PIikgPDwgJ1xuJzsKICB9CiAgcmV0dXJuIDA7Cn0=