// 写成一坨了,纪念一下自己辣的一坨
#include <iostream>
#include <string>
using namespace std;

void solve()
{
    string s;cin>>s;
    int cnt2 = 0,cnt3 = 0,sum = 0;
    for (char c:s)
    {
        // 0和1是不变的
        // 2有机会增加2,3有机会增加6
        sum+=c-'0';
        if (c == '3')
            cnt3++;
        if (c == '2')
            cnt2++;
    }
    sum%=9;if (sum == 0){cout<<"YES\n";return ;}
    if (sum %2)
    {
        int need2= (9-sum)/2;
        if (cnt2 >= need2)
        {cout<<"YES\n";return;}
        if (need2 == 4)
        {
            if (cnt2&&cnt3)
            {cout<<"YES\n";return;}
        }
        if (need2 ==3)
        {
            if (cnt3)
            {cout<<"YES\n";return;}
        }
    }
    else   
    {   
        // 凑出18来就行了
          int need2= (18-sum)/2;
        if (cnt2 >= need2)
        {cout<<"YES\n";return;}
        if (need2 == 5)
        {
            if (cnt3&&cnt2>=2)
            {cout<<"YES\n";return;}
        }
        else  
        {
            if (cnt3 >=2&&cnt2>=need2-6)
            {cout<<"YES\n";return;}
            if (cnt3&&cnt2>=need2-3)
            {cout<<"YES\n";return;}
        }
    }
    cout<<"NO\n";
}
int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);int t;cin>>t;
    while(t--)solve();return 0;
}
// 64 位输出请用 printf("%lld")