#include <bits/stdc++.h>
using namespace std;
#define Samurai ios_base::sync_with_stdio(false), cout.tie(NULL), cin.tie(NULL);
string s; vector<vector<int>>dp;
bool rec(int i=0, int sum = 0){
if(i == s.size()) return (sum%9 == 0);
int &ret = dp[i][sum];
if(~ret) return ret;
ret = 0;
int x = s[i] - '0';
ret |= rec(i+1,(sum+x)%9);
if(x*x < 10) ret |= rec(i+1, (sum+x*x)%9);
return ret;
}
int main(){ Samurai
int _t = 1;
cin >> _t;
for (int i = 1; i <= _t; i++){
cin >> s;
dp = vector<vector<int>>(s.size()+5,vector<int>(9,-1));
cout << (rec()?"YES":"NO") << '\n';
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgU2FtdXJhaSBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKSwgY291dC50aWUoTlVMTCksIGNpbi50aWUoTlVMTCk7CnN0cmluZyBzOyB2ZWN0b3I8dmVjdG9yPGludD4+ZHA7CmJvb2wgcmVjKGludCBpPTAsIGludCBzdW0gPSAwKXsKICAgIGlmKGkgPT0gcy5zaXplKCkpIHJldHVybiAoc3VtJTkgPT0gMCk7CiAgICBpbnQgJnJldCA9IGRwW2ldW3N1bV07CiAgICBpZih+cmV0KSByZXR1cm4gcmV0OwogICAgcmV0ID0gMDsKICAgIGludCB4ID0gc1tpXSAtICcwJzsKICAgIHJldCB8PSByZWMoaSsxLChzdW0reCklOSk7CiAgICBpZih4KnggPCAxMCkgcmV0IHw9IHJlYyhpKzEsIChzdW0reCp4KSU5KTsKICAgIHJldHVybiByZXQ7Cn0KaW50IG1haW4oKXsgU2FtdXJhaQogICAgaW50IF90ID0gMTsKICAgIGNpbiA+PiBfdDsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IF90OyBpKyspewogICAgICAgIGNpbiA+PiBzOwogICAgICAgIGRwID0gdmVjdG9yPHZlY3RvcjxpbnQ+PihzLnNpemUoKSs1LHZlY3RvcjxpbnQ+KDksLTEpKTsKICAgICAgICBjb3V0IDw8IChyZWMoKT8iWUVTIjoiTk8iKSA8PCAnXG4nOwogICAgfQogICAgcmV0dXJuIDA7Cn0KCg==