#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MOD = 1e9 + 7;
void solve(){
string s;
cin >> s;
int n = s.size();
int two = 0, three = 0;
int sum = 0;
for(int i = 0; i < n; i++){
sum += s[i] - '0';
if(s[i] == '2')two++;
if(s[i] == '3')three++;
}
for(int i = 0; i <= two; i++){
for(int j = 0; j <= three; j++){
int s = 2 * i + 6 * j;
s %= 9;
if((sum + s) % 9 == 0){
cout << "YES\n";
return;
}
}
}
cout << "NO\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
for(int i = 1; i <= t; i++){
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1PRCA9IDFlOSArIDc7Cgp2b2lkIHNvbHZlKCl7CglzdHJpbmcgczsKCWNpbiA+PiBzOwoJaW50IG4gPSBzLnNpemUoKTsKCQoJaW50IHR3byA9IDAsIHRocmVlID0gMDsKCWludCBzdW0gPSAwOwoJZm9yKGludCBpID0gMDsgaSA8IG47IGkrKyl7CgkJc3VtICs9IHNbaV0gLSAnMCc7CgkJaWYoc1tpXSA9PSAnMicpdHdvKys7CgkJaWYoc1tpXSA9PSAnMycpdGhyZWUrKzsKCX0KCQoJZm9yKGludCBpID0gMDsgaSA8PSB0d287IGkrKyl7CgkJZm9yKGludCBqID0gMDsgaiA8PSB0aHJlZTsgaisrKXsKCQkJaW50IHMgPSAyICogaSArIDYgKiBqOwoJCQlzICU9IDk7CgkJCWlmKChzdW0gKyBzKSAlIDkgPT0gMCl7CgkJCQljb3V0IDw8ICJZRVNcbiI7CgkJCQlyZXR1cm47CgkJCX0KCQl9Cgl9Cgljb3V0IDw8ICJOT1xuIjsKCQp9CgppbnQgbWFpbigpewoJaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CgljaW4udGllKG51bGxwdHIpOwoJCglpbnQgdCA9IDE7CgljaW4gPj4gdDsKCQoJZm9yKGludCBpID0gMTsgaSA8PSB0OyBpKyspewoJCXNvbHZlKCk7Cgl9CglyZXR1cm4gMDsKfQ==