/*                               MMMMMM
                                    MMMMMMM
                                     MMMMMMMM     .
                                      MMMMMMMMM
                                      HMMMMMMMMMM
                                       MMMMMMMMMMMM  M
                                       MMMMMMMMMMMMM  M
                                        MMMMMMMMMMMMM  M
                                        MMMMMMMMMMMMM:
                                        oMMMMMMMMMMMMMM
              .MMMMMMMMMMMMMMo           MMMMMMMMMMMMMMM M
        MMMMMMMMMMMMMMMMMMMMMMMMMMM      MMMMMMMMMMMMMMMM
          MMMMMMMMMMMMMMMMMMMMMMMMMMMM.  oMMMMMMMMMMMMMMM.M
            MMMMMMMMMMMMMMMMMMMMMMMMMMMM  MMMMMMMMMMMMMMMM
              MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                oMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                  MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                    MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM:                     H
                     MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM                  .         MMM
                      MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM              M       MMMMMM
                       .MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM          M   MMMMMMMMMM
                MM.      MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM       M MMMMMMMMMMMM
                    MM    MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM    .MMMMMMMMMMMMMM
                      MM  MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                        MM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
               .MMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMM.MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                  HMMMMMMMMMMMMMMMMMMMMM.MMMMMMMMM.MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                     MMMMMMMMMMMMMMM MMM.oMMMMMMM..MMMMMMMMM:MMMMMMMMMMMMMMMMMMMMMMM
                       MMMMMMMMMMMMMM MM..MMMMMMM...MMMMMMM. MMMMMMMMMMMMMMMMMMMMM
                         MMMMMMMMMMMMMMM ..MMMMMM...MMMMMM ..MMMMMMMMMMMMMMMMMMM
                          MMMMMMM:M.MMM.M.. MMMMM M..MMMMM...MMMMMMMMMMMMMMMMMM  MMM
                            MMMM. .M..MM.M...MMMMMM..MMMMM.. MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM .
                             MMMM..M....M.....:MMM .MMMMMM..MMMMMMM...MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                              MMM.M.. ...M......MM.MMMMM.......MHM.M  .MMMMMMMMMMMMMMMMMMMMMMMMM
                         MMMMMMMM..MM. . MMM.....MMMMMM.M.....M ..MM..M MMMMMMMMMMMMMMMMMMM
                            .MMMMMHMM. ..MMMM. MMM............o..... . .MMMMMMMMMMMMMMM
                               MMM. M... .........................M..:.MMMMMMMMMMMM
                                 oMMM............ .................M.M.MMMMMMMMM
                                    .....MM........................ . MMMMMM
                                   M.....M.....................o.MM.MMMMMMMM.
                                    M........................M.. ...MMMMMMMMMMMMMo
                                      :....MMM..............MMM..oMMMMMMM
                                       M...MMM.............MMMMMMM
                                          .............:MMMMMMMM
                                          M..... MMM.....M
                                          M M.............
                                          ................M
                                       ooM.................MM  MoMMMMMoooM
                                  MMoooM......................MoooooooH..oMM
                              MHooooMoM.....................MMooooooM........M
                            oooooooMoooM......... o........MoooooooM............
                            Mooooooooooo.......M.........Moooooooo:..............M
                           MooMoooooooooM...M........:Mooooooooooo:..............M
                          M..oooooooooooo .........Mooooooooooooooo..............M
                         M...Mooo:oooooooo.M....ooooooooooooooooooo..M...........M
                          ...oooooMoooooooM..Mooooooooooooo:oooooooM.M...........M.
                         M...ooooooMoo:ooooMoooooooooooooHoooooooooH:M. ...........:
                         M..MoooooooMoooooooooooooooooo:ooooooMooooMoM..............M
                         M..ooooooooooMooooooooooooooHoooooooMooHooooM...............M
                         ...ooooooooooooooooooo:MooooooooooooooMoMoooM................
                        M...oooooooooooooooooooooooooooooooooooooMooMM................M
                        ...MooooooooooooooooooooooooooooooooooooooooMo ................
                        ...MooooooooooooooooooooooooooooooooooooooooM M................M
                       M...ooooooooooooooooooooooooooooooooooooooooM   ................M
                       ...MoooooooooooooooooooooooooooooooooooooooMM   .:...............
                       .....MooooooooooooooooooooooooooooooooooooMoo       .............M
                       M...... ooooooooooooooooooooooooooooooooooooM       M..............M
                       M........MooooMMM MM MM  MMMMMMMMMooooooooM         M...............M
                       .........HM     M:  MM :MMMMMM          M           M...............
                      M..........M     M   MoM M                           M................M
                      M.........:M  MoH  M M M MooooHoooMM.   M             M...............M
                      M..........Moooo MMooM    oooooMooooooooM              M..............H
                      M.........MooooM  Mooo  : ooooooMooooMoooM              M........ . .o.M
                      H..  .....ooooo   oooo  M MooooooooooooooM               M... MMMMMMMMMMM
                      MMMMMMMMMMooooM M oooo  .  ooooooMooooooooM              .MMMMMMMMMMMMMMM
                      MMMMMMMMMMooooH : ooooH    oooooooooooooooo               MMMMMMMMMMMMMMM
                      MMMMMMMMMMoooo    ooooM    Moooooooooooooooo              .MMMMMMMMMMMMMMM
                      MMMMMMMMMMoooo    ooooM    MooooooooooooooooM              MMMMMMMMMMMMMMM
                      MMMMMMMMMMoooM    ooooM     ooooooooooooooooo               MMMMMMMMMMM:M
                      MMMMMMMMMMoooM   MooooM     oooooooooooMoooooo               MH...........
                       . ......Mooo.   MooooM     oooooooooooooooooo              M............M
                      M.M......oooo    MooooM     Moooooooooooooooooo:           .........M.....
                      M.M.....Moooo    MooooM      ooooooooooooooooooM            .M............
                      .......MooooH    MooooM      oooooooooMoooooooooo          M..o...M..o....M
                      .o....HMooooM    MooooH      MooooooooMooooooooooM          .:M...M.......M
                     M..M.....MoooM    :oooo:    .MooooooooHooMoooooooooM         M M... ..oM.M
                      M...M.:.Mooo. MMMMooooo   oooooooooooMoooooooooooooM          ....M. M
                       M:M..o.Moooooooooooooo MooooooooooooooMooooooooooooM          .Mo
                              MooooooooooooooMooooooooooooMoMoooooooooooooo
                              Mooooooooooooooo:ooooooooooooooooooooooooooooo
                              ooooooooooooooooMooooooooooMoooooooooooooooooo
                              ooooooooooooooooMoooooooooooMooooooooooooooooHo
                              ooMooooooooooooooMoooooooooooooooooooooooooooMoM
                             MooMoooooooooooooo.ooooooooooooooooooooooooooo:oM
                             MoooooooooooooooooooooooooooooooooooooooooooooooM
                             MoooMooooooooooooooMooooooooooooooooooooooooooooo.
                             MoooMooooooooooooooMoooooooooooooooooooooooooMooooM
                             MooooooooooooooooooMoooooooooooooooooooooooooMoooooM
                             MooooMoooooooooooooMoooooooooooooooooooooooooMoHooooM
                             ooooooMooooooooooooooooooooooooooooooooooooooooMoMoooM
                            MooooooooooooooooooooMooooooooooooooooooooooooooMoooooH:
                            MoooooooMooooooooooooMoooooooooooooooooooooooooooooHoooM
                            MooooooooMoooooooooooMoooooooooooooooooooooooooMoooMooooM
                            Moooooooooooooooooooooooooooooooooooooooooooooo.oooMooooo
                            MoooooooooooooooooooooooooooooooooooooooooooooMoooooooooM
                             MooooooooooooooooooooMoooooooooooooooooooooooooooooooooM
                              MooooooooooooooooooooMHooooooooooooooooooooMoooo:ooooo
                               MMooooooooooooooooooMoMHoooooooooooooooooooooooMooooo
                                MMoooooooooooooooMMooo MMooooooooooooooooooooooooooM
                                MMMoooooooooooooMooooo  oooooooooooooooooooooMooooo
                                MooMMoooooooooMoooMMoM  ooooHooooooooooooooooMooooM
                                MooooMooooooMooooMoooM  MoooooMoooooooooooooMooooo
                                ooooooMMooooooooMooooM  MoooooooooMooooooooooooooM
                                HooooooMoooooooMooooM    HoooooooHooMooooooooooooo
                                 oooMoooooooooHoooM         MoooooooooMoooooooooM
                                  HooooooooooooHM             MooooooooMMoooooooM
                                   MMMMMMMMMMMMMM                Moooooo:MooooHMM
                                    MMMMMMM: ...                  MMMMMMMMMMMMMM
                                   M............M                  MMMMMMMMM ....
                                   M.MM..........                  M.............M
                                M ..............MM                 M..............
                             MMMMM............MMMM                 ..MMMMMMMM ....M
                           MMMMMMMMMMMMMMMMMMMMMMMM               MMMMMMMMMMMMM...M
                        .MMMMMMMMMMMMMMMMMMMMMMMMMM               MMMMMMMMMMMMMMMMMM
                        MMMMMMMMMMMMMMMMMMMMMMMMM                MMMMMMMMMMMMMMMMMMM
                        :MMMMMMMMMMMMMMMMMMH                     MMMMMMMMMMMMMMMMMMM
                                                                MMMMMMMMMMMMMMMMMM
                                                                 MMMMMMMMMMMMMMM
                                                                  HMMMMMM
                                                                                */
#include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for (ll i = 0; i < n; i++)
#define Fo(i, k, n) for (ll i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define pb push_back
#define F first
#define S second
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
const ll MOD = 1e9 + 7;

void solve(){
  // freopen("convention2.in","r",stdin);
  // freopen("convention2.out","w",stdout);
  string s;cin>>s;
  ll cnt2 = 0,cnt3 = 0;
  ll cnt = 0;
  for(auto x:s){
    if(x!='3'){cnt += x-'0';}
    if(x=='2'){cnt2++;}
    else if(x=='3'){cnt3++;}
  }
  ll temp = cnt%9;
  if(temp==0){
    yes;
  }
  else{
  temp = 9 - temp;
  map<ll,ll> m,m1;
  fo(i,cnt3+1){
    m[(3*i)%9]++;
  }
  fo(i,cnt2+1){
    m1[(2*i)%9]++;
  }
  bool check = 0;
  fo(i,cnt2+1){
    if(temp>=(2*i)%9){
      if(m[temp-(2*i)%9]){
        check = 1;
        break;
      }
    }
    else{
      if(m[9-temp+(2*i)%9]){
        check = 1;
        break;
      }
    }
  }
  cnt2 = cnt3;
  fo(i,cnt2+1){
    if(temp>=(3*i)%9){
      if(m1[temp-(3*i)%9]){
        check = 1;
        break;
      }
    }
    else{
      if(m1[9-temp+(3*i)%9]){
        check = 1;
        break;
      }
    }
  }
  if(check){yes;}
  else{no;}}
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    t = 1;
    cin >> t;
    while (t--)
    {
      solve();
    }
    return 0;
}

/*                               MMMMMM
                                    MMMMMMM
                                     MMMMMMMM     .
                                      MMMMMMMMM
                                      HMMMMMMMMMM
                                       MMMMMMMMMMMM  M
                                       MMMMMMMMMMMMM  M
                                        MMMMMMMMMMMMM  M
                                        MMMMMMMMMMMMM:
                                        oMMMMMMMMMMMMMM
              .MMMMMMMMMMMMMMo           MMMMMMMMMMMMMMM M
        MMMMMMMMMMMMMMMMMMMMMMMMMMM      MMMMMMMMMMMMMMMM
          MMMMMMMMMMMMMMMMMMMMMMMMMMMM.  oMMMMMMMMMMMMMMM.M
            MMMMMMMMMMMMMMMMMMMMMMMMMMMM  MMMMMMMMMMMMMMMM
              MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                oMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                  MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                    MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM:                     H
                     MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM                  .         MMM
                      MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM              M       MMMMMM
                       .MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM          M   MMMMMMMMMM
                MM.      MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM       M MMMMMMMMMMMM
                    MM    MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM    .MMMMMMMMMMMMMM
                      MM  MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                        MM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
               .MMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMM.MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                  HMMMMMMMMMMMMMMMMMMMMM.MMMMMMMMM.MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                     MMMMMMMMMMMMMMM MMM.oMMMMMMM..MMMMMMMMM:MMMMMMMMMMMMMMMMMMMMMMM
                       MMMMMMMMMMMMMM MM..MMMMMMM...MMMMMMM. MMMMMMMMMMMMMMMMMMMMM
                         MMMMMMMMMMMMMMM ..MMMMMM...MMMMMM ..MMMMMMMMMMMMMMMMMMM
                          MMMMMMM:M.MMM.M.. MMMMM M..MMMMM...MMMMMMMMMMMMMMMMMM  MMM
                            MMMM. .M..MM.M...MMMMMM..MMMMM.. MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM .
                             MMMM..M....M.....:MMM .MMMMMM..MMMMMMM...MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
                              MMM.M.. ...M......MM.MMMMM.......MHM.M  .MMMMMMMMMMMMMMMMMMMMMMMMM
                         MMMMMMMM..MM. . MMM.....MMMMMM.M.....M ..MM..M MMMMMMMMMMMMMMMMMMM
                            .MMMMMHMM. ..MMMM. MMM............o..... . .MMMMMMMMMMMMMMM
                               MMM. M... .........................M..:.MMMMMMMMMMMM
                                 oMMM............ .................M.M.MMMMMMMMM
                                    .....MM........................ . MMMMMM
                                   M.....M.....................o.MM.MMMMMMMM.
                                    M........................M.. ...MMMMMMMMMMMMMo
                                      :....MMM..............MMM..oMMMMMMM
                                       M...MMM.............MMMMMMM
                                          .............:MMMMMMMM
                                          M..... MMM.....M
                                          M M.............
                                          ................M
                                       ooM.................MM  MoMMMMMoooM
                                  MMoooM......................MoooooooH..oMM
                              MHooooMoM.....................MMooooooM........M
                            oooooooMoooM......... o........MoooooooM............
                            Mooooooooooo.......M.........Moooooooo:..............M
                           MooMoooooooooM...M........:Mooooooooooo:..............M
                          M..oooooooooooo .........Mooooooooooooooo..............M
                         M...Mooo:oooooooo.M....ooooooooooooooooooo..M...........M
                          ...oooooMoooooooM..Mooooooooooooo:oooooooM.M...........M.
                         M...ooooooMoo:ooooMoooooooooooooHoooooooooH:M. ...........:
                         M..MoooooooMoooooooooooooooooo:ooooooMooooMoM..............M
                         M..ooooooooooMooooooooooooooHoooooooMooHooooM...............M
                         ...ooooooooooooooooooo:MooooooooooooooMoMoooM................
                        M...oooooooooooooooooooooooooooooooooooooMooMM................M
                        ...MooooooooooooooooooooooooooooooooooooooooMo ................
                        ...MooooooooooooooooooooooooooooooooooooooooM M................M
                       M...ooooooooooooooooooooooooooooooooooooooooM   ................M
                       ...MoooooooooooooooooooooooooooooooooooooooMM   .:...............
                       .....MooooooooooooooooooooooooooooooooooooMoo       .............M
                       M...... ooooooooooooooooooooooooooooooooooooM       M..............M
                       M........MooooMMM MM MM  MMMMMMMMMooooooooM         M...............M
                       .........HM     M:  MM :MMMMMM          M           M...............
                      M..........M     M   MoM M                           M................M
                      M.........:M  MoH  M M M MooooHoooMM.   M             M...............M
                      M..........Moooo MMooM    oooooMooooooooM              M..............H
                      M.........MooooM  Mooo  : ooooooMooooMoooM              M........ . .o.M
                      H..  .....ooooo   oooo  M MooooooooooooooM               M... MMMMMMMMMMM
                      MMMMMMMMMMooooM M oooo  .  ooooooMooooooooM              .MMMMMMMMMMMMMMM
                      MMMMMMMMMMooooH : ooooH    oooooooooooooooo               MMMMMMMMMMMMMMM
                      MMMMMMMMMMoooo    ooooM    Moooooooooooooooo              .MMMMMMMMMMMMMMM
                      MMMMMMMMMMoooo    ooooM    MooooooooooooooooM              MMMMMMMMMMMMMMM
                      MMMMMMMMMMoooM    ooooM     ooooooooooooooooo               MMMMMMMMMMM:M
                      MMMMMMMMMMoooM   MooooM     oooooooooooMoooooo               MH...........
                       . ......Mooo.   MooooM     oooooooooooooooooo              M............M
                      M.M......oooo    MooooM     Moooooooooooooooooo:           .........M.....
                      M.M.....Moooo    MooooM      ooooooooooooooooooM            .M............
                      .......MooooH    MooooM      oooooooooMoooooooooo          M..o...M..o....M
                      .o....HMooooM    MooooH      MooooooooMooooooooooM          .:M...M.......M
                     M..M.....MoooM    :oooo:    .MooooooooHooMoooooooooM         M M... ..oM.M
                      M...M.:.Mooo. MMMMooooo   oooooooooooMoooooooooooooM          ....M. M
                       M:M..o.Moooooooooooooo MooooooooooooooMooooooooooooM          .Mo
                              MooooooooooooooMooooooooooooMoMoooooooooooooo
                              Mooooooooooooooo:ooooooooooooooooooooooooooooo
                              ooooooooooooooooMooooooooooMoooooooooooooooooo
                              ooooooooooooooooMoooooooooooMooooooooooooooooHo
                              ooMooooooooooooooMoooooooooooooooooooooooooooMoM
                             MooMoooooooooooooo.ooooooooooooooooooooooooooo:oM
                             MoooooooooooooooooooooooooooooooooooooooooooooooM
                             MoooMooooooooooooooMooooooooooooooooooooooooooooo.
                             MoooMooooooooooooooMoooooooooooooooooooooooooMooooM
                             MooooooooooooooooooMoooooooooooooooooooooooooMoooooM
                             MooooMoooooooooooooMoooooooooooooooooooooooooMoHooooM
                             ooooooMooooooooooooooooooooooooooooooooooooooooMoMoooM
                            MooooooooooooooooooooMooooooooooooooooooooooooooMoooooH:
                            MoooooooMooooooooooooMoooooooooooooooooooooooooooooHoooM
                            MooooooooMoooooooooooMoooooooooooooooooooooooooMoooMooooM
                            Moooooooooooooooooooooooooooooooooooooooooooooo.oooMooooo
                            MoooooooooooooooooooooooooooooooooooooooooooooMoooooooooM
                             MooooooooooooooooooooMoooooooooooooooooooooooooooooooooM
                              MooooooooooooooooooooMHooooooooooooooooooooMoooo:ooooo
                               MMooooooooooooooooooMoMHoooooooooooooooooooooooMooooo
                                MMoooooooooooooooMMooo MMooooooooooooooooooooooooooM
                                MMMoooooooooooooMooooo  oooooooooooooooooooooMooooo
                                MooMMoooooooooMoooMMoM  ooooHooooooooooooooooMooooM
                                MooooMooooooMooooMoooM  MoooooMoooooooooooooMooooo
                                ooooooMMooooooooMooooM  MoooooooooMooooooooooooooM
                                HooooooMoooooooMooooM    HoooooooHooMooooooooooooo
                                 oooMoooooooooHoooM         MoooooooooMoooooooooM
                                  HooooooooooooHM             MooooooooMMoooooooM
                                   MMMMMMMMMMMMMM                Moooooo:MooooHMM
                                    MMMMMMM: ...                  MMMMMMMMMMMMMM
                                   M............M                  MMMMMMMMM ....
                                   M.MM..........                  M.............M
                                M ..............MM                 M..............
                             MMMMM............MMMM                 ..MMMMMMMM ....M
                           MMMMMMMMMMMMMMMMMMMMMMMM               MMMMMMMMMMMMM...M
                        .MMMMMMMMMMMMMMMMMMMMMMMMMM               MMMMMMMMMMMMMMMMMM
                        MMMMMMMMMMMMMMMMMMMMMMMMM                MMMMMMMMMMMMMMMMMMM
                        :MMMMMMMMMMMMMMMMMMH                     MMMMMMMMMMMMMMMMMMM
                                                                MMMMMMMMMMMMMMMMMM
                                                                 MMMMMMMMMMMMMMM
                                                                  HMMMMMM
                                                                                */
#include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for (ll i = 0; i < n; i++)
#define Fo(i, k, n) for (ll i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define pb push_back
#define F first
#define S second
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
const ll MOD = 1e9 + 7;

void solve(ll t,ll t1){
  // freopen("convention2.in","r",stdin);
  // freopen("convention2.out","w",stdout);
  string s;cin>>s;
  ll cnt2 = 0,cnt3 = 0;
  ll cnt = 0;
  for(auto x:s){
    if(x!='3'){cnt += x-'0';}
    if(x=='2'){cnt2++;}
    else if(x=='3'){cnt3++;}
  }
  ll temp = cnt%9;
  if(temp==0){
    yes;
  }
  else{
  temp = 9 - temp;
  map<ll,ll> m,m1;
  fo(i,cnt3+1){
    m[(3*i)%9]++;
  }
  fo(i,cnt2+1){
    m1[(2*i)%9]++;
  }
  bool check = 0;
  fo(i,cnt2+1){
    if(temp>=(2*i)%9){
      if(m[temp-(2*i)%9]){
        check = 1;
        break;
      }
    }
    else{
      if(m[9+temp-(2*i)%9]){
        check = 1;
        break;
      }
    }
  }
  cnt2 = cnt3;
  fo(i,cnt2+1){
    if(temp>=(3*i)%9){
      if(m1[temp-(3*i)%9]){
        check = 1;
        break;
      }
    }
    else{
      if(m1[9-temp+(3*i)%9]){
        check = 1;
        break;
      }
    }
  }
  if(check){yes;}
  else{no;}}
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll t;
    t = 1;
    cin >> t;
    ll t1 = t;
    while (t--)
    {
      solve(t,t1);
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

int main()

{

    int t;

    cin >> t;

    while (t--)

    {

       long long n,x=0,sum=0,c2=0,c3=0,num=0,diff=0,i,f=0,j,temp=0;

       string s;

       cin>>s;

       for(i=0;i<s.length();i++){

        sum+=s[i]-'0';

        if(s[i]=='2'){

            c2++;

        }

        else if(s[i]=='3'){

            c3++;

        }

       }

       if(sum%9==0){

           cout<<"YES"<<endl;

           continue;

       }

       else if(c3==s.length()){

           cout<<"YES"<<endl;

           continue;

       }

       else{

          x=sum/9;

          num=(x+1)*9;

          diff=abs(num-sum);

        for(i=0;i<=c2;i++){

            temp=0;

            for(j=0;j<=c3;j++){

                temp=i*2+j*6+sum;

                if(temp%9==0){

                    f=1;

                    break;

                 }

             }

        }

        if(f==0){

            cout<<"NO"<<endl;

        }

        else{

            cout<<"YES"<<endl;

        }

    }

  }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int t;

    cin >> t;

    while (t--)

    {

       long long n,x=0,sum=0,c2=0,c3=0,num=0,diff=0,i,f=0,j,temp=0;

       string s;

       cin>>s;

       for(i=0;i<s.length();i++){

        sum+=s[i]-'0';

        if(s[i]=='2'){

            c2++;

        }

        else if(s[i]=='3'){

            c3++;

        }

       }

       if(sum%9==0){

           cout<<"YES"<<endl;

           continue;

       }

       else if(c3==s.length()){

           cout<<"YES"<<endl;

           continue;

       }

       else{

          x=sum/9;

          num=(x+1)*9;

          diff=abs(num-sum);

        for(i=0;i<=c2;i++){

            temp=0;

            if(i*2>99){

                break;

            }

            else{

            for(j=0;j<=c3;j++){

                temp=i*2+j*6;

                if(temp>99){

                    break;

                }

                else if((temp+sum)%9==0){

                    f=1;

                    break;

                 }

             }

             if(f==1){

                 break;

             }

            }

        }

        if(f==0){

            cout<<"NO"<<endl;

        }

        else{

            cout<<"YES"<<endl;

        }

    }

  }

    return 0;

}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    while (t--)
    {
        int n;cin>>n;
        int n_2=0,n_3=0;
        string s=to_string(n);
        // cout<<s;
        int sum=0;
        // int x=stoi(s[1]);
        for(int i=0;i<s.size();i++){
            sum+=s[i]-'0';
            if(s[i]=='2') n_2++;
            if(s[i]=='3') n_3++;
        }
        // cout<<sum;
        // cout<<n_2<<" "<<n_3;
        int flag=0;
        for(int i=0;i<=min(n_3,2);i++){
            for(int j=0;j<=min(n_2,8);j++){
                if((sum+6*i + 2*j)%9 == 0){
                    flag=1;
                }
            }
        }
        if(flag==1) cout<<"Yes"<<endl;
        if(flag==0) cout<<"No"<<endl;
    }

}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    while (t--)
    {
        string n;cin>>n;
        int n_2=0,n_3=0;
        // string s=to_string(n);
        // cout<<s<<endl;
        int sum=0;
        // int x=stoi(s[1]);
        // cout<<s.size();
        for(int i=0;i<n.size();i++){
            sum+=n[i]-'0';
            if(n[i]=='2') n_2++;
            if(n[i]=='3') n_3++;
        }
        // cout<<n_2<<" "<<n_3<<endl;
        if(n_2 == 0 && n_3 == 0){
            if(sum%9==0) cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
        else{
            int flag=0;
            for(int i=0;i<=min(n_3,2);i++){
                for(int j=0;j<=min(n_2,8);j++){
                    if((sum+ 6*i + 2*j)%9 == 0){
                        flag=1;
                    }
                }
            }
            if(flag==1) cout<<"Yes"<<endl;
            if(flag==0) cout<<"No"<<endl;
        }
    }

}

Answer: 2


#include<iostream>
#include<vector>
#include<string>
#include<cmath>
using namespace std;
int calculateX(int m, int n) {

    int k = static_cast<int>(ceil(static_cast<double>(m) / n));

    int x = (k * n) - m;

    return x;
}
int main() {
    int t;
    cin>>t;
    vector<string>v;
    while(t--){
        long long int number;
        int sum = 0;

    cin >> number;
    int m=0,n=0;
    while (number > 0) {
        if(number%10==2){
            m++;
        }
        if(number%10==3){
            n++;
        }
        sum += number % 10;
        number /= 10;
    }

    int remi=sum%9;
    if(m==0 && n==0){
        if(remi==0){
            v.push_back("YES");
        }
        else{
            v.push_back("NO");
        }

    }
    else{
        int rem=calculateX(remi,9);
        int check=0;
        for(int i=0;i<=m;i++){
            for(int j=0;j<=n;j++){
                if((sum+2*i+6*j) % 9 ==0){
                    check=1;
                    break;
                }

            }
        }
        if(check){
            v.push_back("YES");
        }
        else{
            v.push_back("NO");
        }

    }

    }

    for(auto i:v){
        cout<<i<<endl;
    }
    return 0;
}

#include <iostream>
#include <vector>
#include <string>
using namespace std;

void solve() {
    string input;
    cin >> input;

    vector<int> s;
    for (char c : input) {
        s.push_back(c - '0'); // Convert char to int and store in vector
    }

    int sm = 0;
    int twos = 0, threes = 0;

    for (int digit : s) {
        sm += digit;
        if (digit == 2) twos++;
        if (digit == 3) threes++;
    }

    for (int i = 0; i <= min(10, twos); i++) {
        for (int j = 0; j <= min(10, threes); j++) {
            if ((sm + i * 2 + j * 6) % 9 == 0) {
                cout << "YES" << endl;
                return;
            }
        }
    }

    cout << "NO" << endl;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 2


#include <iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<stack>
#include <string>
#include<math.h>
#include<iomanip>
using namespace std;
void solve(){
    long long sum=0;
    long long two=0,three=0;
    string s;
    cin>>s;
    int len=s.length();
    for(int i=0;i<len;i++){
        sum+=s[i]-'0';
        if(s[i]=='2')two++;
        if(s[i]=='3')three++;
    }
    int mod=sum%9;
    if(mod==0){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==1&&((two>=4)||(three>=1&&two>=1))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==2&&two>=1&&three>=1){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==3&&three>=1){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==4&&((two>=7)||(three>=1&&two>=4)||(three>=2&&two>=1))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==5&&two>=2){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==6&&((two>=6)||(three>=1&&two>=3)||(three>=2))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==7&&two>=1){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==8&&((two>=5)||(three>=1&&two>=2))){
        cout<<"YES"<<"\n";
        return;
    }

    cout<<"NO\n";
}
int main() {
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

#include <iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<stack>
#include <string>
#include<math.h>
#include<iomanip>
using namespace std;
void solve(){
    long long sum=0;
    long long two=0,three=0;
    string s;
    cin>>s;
    int len=s.length();
    for(int i=0;i<len;i++){
        sum+=s[i]-'0';
        if(s[i]=='2')two++;
        if(s[i]=='3')three++;
    }
    int mod=sum%9;
    if(mod==0){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==1&&((two>=4)||(three>=1&&two>=1))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==2&&(two>=8||(three>=1&&two>=5)||(three>=2&&two>=2))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==3&&(three>=1||two>=3)){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==4&&((two>=7)||(three>=1&&two>=4)||(three>=2&&two>=1))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==5&&two>=2){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==6&&((two>=6)||(three>=1&&two>=3)||(three>=2))){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==7&&two>=1){
        cout<<"YES"<<"\n";
        return;
    }
    if(mod==8&&((two>=5)||(three>=1&&two>=2))){
        cout<<"YES"<<"\n";
        return;
    }

    cout<<"NO\n";
}
int main() {
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

using namespace std;

#define ll long long

#define dbg cout

#define yes cout <<"YES"<<endl

#define no cout <<"NO"<<endl

#define in(a) for(int j=0;j<a.size();j++) cin>>a[j];

#define all(v) v.begin(),v.end()

#define out(a) for(int i=0;i<a.size();i++){cout<<a[i]<<" ";}cout<<endl;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

string to_binary(ll n) {

    if (n == 0) return "0";

    string r;

    while (n != 0) {

        r = (n % 2 == 0 ? "0" : "1") + r;

        n /= 2;

    }

    return r;

}

ll to_decimal(string s) {

    reverse(s.begin(), s.end());

    ll ct = 0;

    for (ll i = 0; i < s.size(); i++) {

        if (s[i] == '1')

            ct += (1ll << i);

    }

    return ct;

}

vector<bool> SieveOfEratosthenes(ll n) {

    vector<bool> prime(n + 1, 1);

    for (ll p = 2; p * p <= n; p++) {

        if (prime[p] == true) {

            for (ll i = p * p; i <= n; i += p)

                prime[i] = false;

        }

    }

    prime[1] = prime[0] = 0;

    return prime;

}

long long binpow(long long a, long long b) {

    long long res = 1;

    while (b > 0) {

        if (b & 1)

            res = res * a;

        a = a * a;

        b >>= 1;

    }

    return res;

}

ll binpow_modulo(ll a, ll b, ll mod) {

    ll res = 1;

    while (b > 0) {

        if (b % 2 == 1)

            res = (res * a) % mod;

        a = (a * a) % mod;

        b = b / 2;

    }

    return res;

}

ll fact(ll n, ll mod) {

    ll ans = 1;

    for (ll i = 2; i <= n; i++)

        ans = ((ans % mod) * (i % mod) + mod) % mod;

    ans %= mod;

    return ans;

}

ll ncr(ll n, ll r, ll mod) {

    if(n<r) return 0;

    ll ri = binpow_modulo(fact(r, mod), mod - 2, mod);

    ll nri = binpow_modulo(fact(n - r, mod), mod - 2, mod);

    ll ans = (((fact(n, mod) % mod) * (ri % mod)) % mod);

    ans = (((ans % mod) * (nri % mod)) % mod);

    ans %= mod;

    ans = (ans + mod) % mod;

    return ans;

}

ll inverse_modulo(ll num,ll mod){

return binpow_modulo(num,mod-2,mod);

}

ll lcm_of_array(vector<ll> arr) {

    // nlogn

    ll lcm = arr[0];

    for (ll i = 1; i < arr.size(); i++) {

        ll num1 = lcm;

        ll num2 = arr[i];

        ll gcd_val = __gcd(num1, num2);

        lcm = (lcm / gcd_val) * arr[i];

    }

    return lcm;

}

ll lcm(ll a, ll b) {

    return (a / (__gcd(a, b))) * b;

}

int fun(int ind, int mm, string &s,vector<vector<int>>&dp){

    if(ind==s.size()){

        if(mm==0) return 1;

        return 0;

    }

    if(dp[ind][mm]!=-1) return dp[ind][mm];

    int pick=0,nonpick=0;

    if(s[ind]=='2'){

        pick = fun(ind+1,(mm+2)%9,s,dp);

        nonpick = fun(ind+1,mm,s,dp);

    }

    else if(s[ind]=='3'){

         pick = fun(ind+1,(mm+6)%9,s,dp);

        nonpick = fun(ind+1,mm,s,dp);

    }

    else{

        nonpick = fun(ind+1,mm,s,dp);

    }

    return dp[ind][mm] = pick||nonpick;

}

int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);

    freopen("output.txt", "w", stdout);

#endif

ll TESTCASES;

cin >> TESTCASES;

while (TESTCASES--) {

string s;

cin >>s;

int n = s.size();

vector<vector<int>>dp(n+1,vector<int>(10,-1));

int mm =0;

for(int i=0;i<s.size();i++){

    int dd = s[i]-'0';

    mm= (mm+dd)%9;

}

int f = fun(0,mm,s,dp);

if(f) yes;

else no;

}

}

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

using namespace std;

#define ll long long

#define dbg cout

#define yes cout <<"YES"<<endl

#define no cout <<"NO"<<endl

#define in(a) for(int j=0;j<a.size();j++) cin>>a[j];

#define all(v) v.begin(),v.end()

#define out(a) for(int i=0;i<a.size();i++){cout<<a[i]<<" ";}cout<<endl;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

string to_binary(ll n) {

    if (n == 0) return "0";

    string r;

    while (n != 0) {

        r = (n % 2 == 0 ? "0" : "1") + r;

        n /= 2;

    }

    return r;

}

ll to_decimal(string s) {

    reverse(s.begin(), s.end());

    ll ct = 0;

    for (ll i = 0; i < s.size(); i++) {

        if (s[i] == '1')

            ct += (1ll << i);

    }

    return ct;

}

vector<bool> SieveOfEratosthenes(ll n) {

    vector<bool> prime(n + 1, 1);

    for (ll p = 2; p * p <= n; p++) {

        if (prime[p] == true) {

            for (ll i = p * p; i <= n; i += p)

                prime[i] = false;

        }

    }

    prime[1] = prime[0] = 0;

    return prime;

}

long long binpow(long long a, long long b) {

    long long res = 1;

    while (b > 0) {

        if (b & 1)

            res = res * a;

        a = a * a;

        b >>= 1;

    }

    return res;

}

ll binpow_modulo(ll a, ll b, ll mod) {

    ll res = 1;

    while (b > 0) {

        if (b % 2 == 1)

            res = (res * a) % mod;

        a = (a * a) % mod;

        b = b / 2;

    }

    return res;

}

ll fact(ll n, ll mod) {

    ll ans = 1;

    for (ll i = 2; i <= n; i++)

        ans = ((ans % mod) * (i % mod) + mod) % mod;

    ans %= mod;

    return ans;

}

ll ncr(ll n, ll r, ll mod) {

    if(n<r) return 0;

    ll ri = binpow_modulo(fact(r, mod), mod - 2, mod);

    ll nri = binpow_modulo(fact(n - r, mod), mod - 2, mod);

    ll ans = (((fact(n, mod) % mod) * (ri % mod)) % mod);

    ans = (((ans % mod) * (nri % mod)) % mod);

    ans %= mod;

    ans = (ans + mod) % mod;

    return ans;

}

ll inverse_modulo(ll num,ll mod){

return binpow_modulo(num,mod-2,mod);

}

ll lcm_of_array(vector<ll> arr) {

    // nlogn

    ll lcm = arr[0];

    for (ll i = 1; i < arr.size(); i++) {

        ll num1 = lcm;

        ll num2 = arr[i];

        ll gcd_val = __gcd(num1, num2);

        lcm = (lcm / gcd_val) * arr[i];

    }

    return lcm;

}

ll lcm(ll a, ll b) {

    return (a / (__gcd(a, b))) * b;

}

int fun(int ind, int mm, string s,vector<vector<int>>&dp){

    if(ind==s.size()){

        if(mm==0) return 1;

        return 0;

    }

    if(dp[ind][mm]!=-1) return dp[ind][mm];

    int pick=0,nonpick=0;

    if(s[ind]=='2'){

        pick = fun(ind+1,(mm+2)%9,s,dp);

        nonpick = fun(ind+1,mm,s,dp);

    }

    else if(s[ind]=='3'){

         pick = fun(ind+1,(mm+6)%9,s,dp);

        nonpick = fun(ind+1,mm,s,dp);

    }

    else{

        nonpick = fun(ind+1,mm,s,dp);

    }

    return dp[ind][mm] = pick||nonpick;

}

int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);

#ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);

    freopen("output.txt", "w", stdout);

#endif

ll TESTCASES;

cin >> TESTCASES;

while (TESTCASES--) {

string s;

cin >>s;

int n = s.size();

vector<vector<int>>dp(n+1,vector<int>(10,-1));

int mm =0;

for(int i=0;i<s.size();i++){

    int dd = s[i]-'0';

    mm= (mm+dd)%9;

}

int f = fun(0,mm,s,dp);

if(f) yes;

else no;

}

}

Answer: 1


#include <fstream>
#include <iostream>
using namespace std;

int main() {
    istream& fin = cin;
    ostream& fout = cout;

    int t;
    fin >> t;
    int two, three;
    long long sum;
    string number;
    int temp;
    int offset = int('0');
    for (int i=0; i<t; i++) {
        two = three = 0;
        sum = 0;
        fin >> number;
        for (char ch: number) {
            if (ch == '\n') break;
            temp = int(ch) - offset;
            if (temp == 2) two++;
            if (temp == 3) three++;
            sum += temp;
        }
        bool possible = sum % 9 == 0;
        sum %= 9;
        // fout << (sum % 9) << " " << two << " " << three << " " << endl;
        for (int j=0; j<=two; j++) {
            if (possible) break;
            if (j == 9) {possible = true; break;}
            for (int k=0; k<=three; k++) {
                if (possible) break;
                if (k == 9) {possible = true; break;}
                if ((sum + 2*j + 6*k) % 9 == 0) possible = true;
            }
        }
        fout << (possible ? "YES" : "NO") << "\n";
    }
}

#include <fstream>
#include <iostream>
using namespace std;

int main() {
    istream& fin = cin;
    ostream& fout = cout;

    int t;
    fin >> t;
    int two, three;
    long long sum;
    string number;
    int temp;
    int offset = int('0');
    for (int i=0; i<t; i++) {
        two = three = 0;
        sum = 0;
        fin >> number;
        for (char ch: number) {
            if (ch == '\n') break;
            temp = int(ch) - offset;
            if (temp == 2) two++;
            if (temp == 3) three++;
            sum += temp;
        }
        bool possible = sum % 9 == 0;
        sum %= 9;
        // fout << (sum % 9) << " " << two << " " << three << " " << endl;
        for (int j=0; j<=two; j++) {
            if (possible) break;
            if (j == 9) break;
            for (int k=0; k<=three; k++) {
                if (possible) break;
                if (k == 9) break;
                if ((sum + 2*j + 6*k) % 9 == 0) possible = true;
            }
        }
        fout << (possible ? "YES" : "NO") << "\n";
    }
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;

#define int long long

void solve(){
    string n;
    cin>>n;

    int sum = 0;
    int find2 = 0;
    int find3 = 0;

    for(auto it:n){
        sum += (it-'0');
        if((it-'0') == 2) find2++;
        if((it-'0') == 3) find3++;
    }

    for(int i=0;i<=find2;i++){
        for(int j=0;j<=find3;j++){
            if((sum + 6*j + 2*i)%9 == 0){
                cout<<"YES\n";
                return;
            }
        }
    }
        cout<<"NO\n";

}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--){
       solve();
    }
}

#include<bits/stdc++.h>
using namespace std;

#define int long long

int get_sum(int n){
    int sum = 0;
    while(n>0){
        sum += (n%10);
        n=n/10;
    }
    return sum;
}

int get_two(int n){
    int a = 0;
    while(n>0){
        if(n%10 == 2) a++;
        n=n/10;
    }
    return a;
}

int get_three(int n){
    int a = 0;
    while(n>0){
        if(n%10 == 3) a++;
        n=n/10;
    }
    return a;
}

void solve(){
    int n;
    cin>>n;
    int sum = get_sum(n);
    int left = (((sum+8)/9)*9)  - sum;

    // cout<<"LEFT IS: "<<left<<endl;

    int find2 = get_two(n);
    int find3 = get_three(n);

    if(left == 0){
        cout<<"YES\n";
        return;
    }else if(left == 1){
        if(find2>=5 || (find3>=1 && find2>=2)){
            cout<<"YES\n";
            return;
        }
    }
    else if(left == 2 && find2>=1){
        cout<<"YES\n";
        return;
    }
    else if(left == 3){
        if(find3>=2 || (find2>=6) || (find3>=1 && find2>=3)){
            cout<<"YES\n";
            return;
        }
    }
    else if(left == 4 &&  find2>=2){
        cout<<"YES\n";
        return;

    }else if(left == 5){
        //make 14
        if(find2>=7 || (find3>=2 && find2>=1) || (find3>=1 && find2>=4)){
            cout<<"YES\n";
            return;
        }
    }
    else if(left == 6 && (find2>=3 || find3>=1)   ){
        cout<<"YES\n";
        return;

    }
    else if(left == 7){
        if(find2>=8 || (find3>=2 && find2>=2) || (find2>=1 && find2>=5)){
            cout<<"YES\n";
            return;
        }
    }

    else if(left == 8 && ( find2>=4  || (find3>=1 && find2>=1)) ){
        cout<<"YES\n";
        return;

    }
        cout<<"NO\n";

}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--){
       solve();
    }
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define endl "\n"

void slove()
{
    string s;
    int a = 0,b = 0;
    int sum = 0;
    cin >> s;
    for(int i = 0;i < s.size();i++)
    {
        sum += s[i] - '0';
        if(s[i] == '2') a++;
        else if(s[i] == '3') b++;
    }

    for(int i = 0; i <= min(5,a);i++)
    {
        for(int j = 0; j <= min(5,b); j++)
        {
            int k = i*2 + 6*j;
            if((sum + k)%9 == 0)
            {
                cout << "YES" << endl;
                return ;
            }
        }
    }
    cout << "NO" << endl;
}

signed main()
{
    int T = 1;
    cin >> T;
    while(T--) slove();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

void slove()
{
    string s;
    int a = 0,b = 0;
    int sum = 0;
    cin >> s;
    for(int i = 0;i < s.size();i++)
    {
        sum += s[i] - '0';
        if(s[i] == '2') a++;
        else if(s[i] == '3') b++;
    }

    for(int i = 0; i <= min(8ll,a);i++)
    {
        for(int j = 0; j <= min(8ll,b); j++)
        {
            int k = i*2 + 6*j;
            if((sum + k)%9 == 0)
            {
                cout << "YES" << endl;
                return ;
            }
        }
    }
    cout << "NO" << endl;
}

signed main()
{
    int T = 1;
    cin >> T;
    while(T--) slove();
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;

void accelerate()
{
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

void solve() {
    string n;
    cin >> n;
    int sum = 0;
    int two = 0;
    int three = 0;
    for (auto c : n) {
        int d = c - '0';
        sum = (sum + d) % 9;
        if (d == 2) two++;
        else if (d == 3) three++;
    }
    int dp[9] = {0};
    dp[sum] = 1;
    for (int i = 0; i < two || i < 8; i++) {
        sum = (sum + 2) % 9;
        dp[sum] = 1;
    }
    if (dp[0] || (dp[3] && three >= 1) || (dp[6] && three >= 2)) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
}

signed main() {
    // accelerate();
    int tc = 1;
    cin >> tc;
    while (tc--) {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

void accelerate()
{
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}

void solve() {
    string n;
    cin >> n;
    int sum = 0;
    int two = 0;
    int three = 0;
    for (auto c : n) {
        int d = c - '0';
        sum = (sum + d) % 9;
        if (d == 2) two++;
        else if (d == 3) three++;
    }
    int dp[9] = {0};
    dp[sum] = 1;
    for (int i = 0; i < two && i < 8; i++) {
        sum = (sum + 2) % 9;
        dp[sum] = 1;
    }
    if (dp[0] || (dp[3] && three >= 1) || (dp[6] && three >= 2)) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
}

signed main() {
    // accelerate();
    int tc = 1;
    cin >> tc;
    while (tc--) {
        solve();
    }
    return 0;
}

Answer: 2


Answer: 2


#include <bits/stdc++.h>
using namespace std;

#define int long long
#define vi vector<int>
#define vs vector<string>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define mii map<int, int>
#define umii unordered_map<int, int>

#define ff first
#define ss second
#define rep(i, n) for(int i=0; i<n; i++)
#define rrep(i, n) for(int i=n-1; i>=0; i--)
#define pb push_back
#define aint(x) x.begin(),x.end()

#define pyes cout << "YES\n"
#define pno cout << "NO\n"
#define pm1 cout << -1 << endl
#define p0 cout << 0 << endl
#define p1 cout << 1 << endl
#define out(x) cout<<x<<endl
#define nl cout<<endl
const int oo = 1e18;
const int mod = 1e9 + 7;

void solve() {
	string s;	cin >> s;
	int n = s.size(), sum = 0;

	int cnt2 = 0, cnt3 = 0;
	rep(i, n) {
		sum += s[i] - '0';
		if (s[i] - '0' == 2)	cnt2++;
		if (s[i] - '0' == 3)	cnt3++;
	}

	sum %= 9;
	int flag = 0;

	for (int i = 0; i <= min(cnt2, 1LL * 20); i++) {
		for (int j = 0; j <= min(cnt3, 1LL * 20); j++) {
			int temp = sum;
			temp += i * 2 + j * 6;
			if (temp % 9 == 0) {
				cout << "YES\n";
				return;
			}
		}
	}
	for (int i = 0; i < min(cnt2, 1LL * 20); i++) {
		int temp = sum;
		if ((temp + i * 2) % 9 == 0) {
			pyes;
			return;
		}
	}
	for (int i = 0; i < min(cnt3, 1LL * 20); i++) {
		int temp = sum;
		if ((temp + i * 3) % 9 == 0) {
			pyes;
			return;
		}
	}
	pno;
}

signed main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int t = 1;
	cin >> t;
	// out(t);
	while (t--)	solve();
}

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define vi vector<int>
#define vs vector<string>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define mii map<int, int>
#define umii unordered_map<int, int>

#define ff first
#define ss second
#define rep(i, n) for(int i=0; i<n; i++)
#define rrep(i, n) for(int i=n-1; i>=0; i--)
#define pb push_back
#define aint(x) x.begin(),x.end()

#define pyes cout << "YES\n"
#define pno cout << "NO\n"
#define pm1 cout << -1 << endl
#define p0 cout << 0 << endl
#define p1 cout << 1 << endl
#define out(x) cout<<x<<endl
#define nl cout<<endl
const int oo = 1e18;
const int mod = 1e9 + 7;

void solve() {
	string s;	cin >> s;
	int n = s.size(), sum = 0;

	int cnt2 = 0, cnt3 = 0;
	rep(i, n) {
		sum += s[i] - '0';
		if (s[i] - '0' == 2)	cnt2++;
		if (s[i] - '0' == 3)	cnt3++;
	}

	sum %= 9;
	int flag = 0;

	for (int i = 0; i <= min(cnt2, 1int * 20); i++) {
		for (int j = 0; j <= min(cnt3, 1int * 20); j++) {
			int temp = sum;
			temp += i * 2 + j * 6;
			if (temp % 9 == 0) {
				cout << "YES\n";
				return;
			}
		}
	}
	for (int i = 0; i < min(cnt2, 1int * 20); i++) {
		int temp = sum;
		if ((temp + i * 2) % 9 == 0) {
			pyes;
			return;
		}
	}
	for (int i = 0; i < min(cnt3, 1int * 20); i++) {
		int temp = sum;
		if ((temp + i * 3) % 9 == 0) {
			pyes;
			return;
		}
	}
	pno;
}

signed main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int t = 1;
	cin >> t;
	// out(t);
	while (t--)	solve();
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
const int maxn = 100005, MOD = 1e9 + 7;

void fast_io(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

// bool prime[1001];

// void SieveOfEratosthenes(int n)
// {
//     memset(prime, true, sizeof(prime));
//     for (int p = 2; p * p <= n; p++) {
//         if (prime[p] == true) {
//             for (int i = p * p; i <= n; i += p)prime[i] = false;
//         }
//     }
// }

void solve(){
    string n; cin >> n;
    int twos = 0, threes = 0, md = 0;
    int x = n.size();
    bool ans = 0;
    for(int i = 0; i<x ; ++i){
        int temp = (int)(n[i]) - 48;
        md += temp;
        md = (md % 9);
        // cout << temp << " ";
        if(temp == 2) twos++;
        if(temp == 3) threes++;

    }
    twos = (twos%9);
    threes = (threes%3);
    for(int i = 0; i<=twos; ++i){
        for(int j = 0; j<=threes; ++j){
            int temp = (2*i) + (6*j);
            if((md+temp) % 9 == 0) {
                ans = 1;
                break;
            }
        }
        if(ans) break;
    }
    if(ans) cout << "YES";
    else cout << "NO";

}

signed main() {
    fast_io();
    int t = 1;
    cin >> t;
    while(t--){
        solve();
        cout << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
const int maxn = 100005, MOD = 1e9 + 7;

void fast_io(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}

// bool prime[1001];

// void SieveOfEratosthenes(int n)
// {
//     memset(prime, true, sizeof(prime));
//     for (int p = 2; p * p <= n; p++) {
//         if (prime[p] == true) {
//             for (int i = p * p; i <= n; i += p)prime[i] = false;
//         }
//     }
// }

void solve(){
    string n; cin >> n;
    int twos = 0, threes = 0, md = 0;
    int x = n.size();
    bool ans = 0;
    for(int i = 0; i<x ; ++i){
        int temp = (int)(n[i]) - 48;
        md += temp;
        md = (md % 9);
        // cout << temp << " ";
        if(temp == 2) twos++;
        if(temp == 3) threes++;

    }
    twos = min((int)8, twos);
    threes = min((int)2, threes);
    for(int i = 0; i<=twos; ++i){
        for(int j = 0; j<=threes; ++j){
            int temp = (2*i) + (6*j);
            if((md+temp) % 9 == 0) {
                ans = 1;
                break;
            }
        }
        if(ans) break;
    }
    if(ans) cout << "YES";
    else cout << "NO";

}

signed main() {
    fast_io();
    int t = 1;
    cin >> t;
    while(t--){
        solve();
        cout << endl;
    }
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int t;
    cin >> t;
    while (t--){
        string num;
        cin >> num;
        int l = num.size();
        int qs = 0;
        int cnt2 = 0, cnt3 = 0;
        for (int i = 0; i < l; i++){
            if (num[i]-'0' == 2){
                cnt2++;
            }
            else if (num[i]-'0' == 3){
                cnt3++;
            }
            qs += num[i]-'0';
        }
        bool ch = false;
        int temp;
        for (int i = 0; i <= cnt2; i++){
            for (int j = 0; j <= cnt3; j++){
                temp = qs;
                qs += (2*i)+(6*j);
                if (qs%9==0) ch = true;
                if (ch) break;
                qs = temp;
            }
            if (ch) break;
        }
        if (ch) cout << "YES\n";
        else cout << "NO\n";
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int t;
    cin >> t;
    while (t--){
        string num;
        cin >> num;
        int l = num.size();
        int qs = 0;
        int cnt2 = 0, cnt3 = 0;
        for (int i = 0; i < l; i++){
            if (num[i]-'0' == 2){
                cnt2++;
            }
            else if (num[i]-'0' == 3){
                cnt3++;
            }
            qs += num[i]-'0';
        }
        bool ch = false;
        qs%=9;
        if (qs == 0) ch = true;
        if (qs == 7 && cnt2 >= 1) ch = true;
        if (qs == 6 && (cnt2 >= 6 || cnt3 >= 2 || (cnt3 >= 1 && cnt2 >= 3))) ch = true;
        if (qs == 5 && cnt2 >= 2) ch = true;
        if (qs == 3 && (cnt2 >= 3 || cnt3 >= 1)) ch = true;
        if (qs == 2 && cnt2 >= 8 || (cnt3 >= 1 && cnt2 >= 5) || (cnt3 >= 2 && cnt2 >= 2)) ch = true;
        if (qs == 1 && (cnt2 >= 4 || (cnt2 >= 1 && cnt3 >= 1))) ch = true;
        if (ch) cout << "YES\n";
        else cout << "NO\n";
    }

    return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string num;
        cin >> num;

        int twos_count = 0, threes_count = 0, sum = 0;

        // Process digits as a string to avoid overflow
        for (char digit : num)
        {
            int k = digit - '0';
            if (k == 2)
                twos_count++;
            else if (k == 3)
                threes_count++;
            sum += k;
        }

        if (sum % 9 == 0)
        {
            cout << "YES" << endl;
            continue;
        }

        // Limit effective counts based on problem constraints
        twos_count = min(twos_count, 8);
        threes_count = min(threes_count, 5);

        bool marker = false;
        int rem = sum % 9;

        for (int i = 0; i <= threes_count; i++)
        {
            for (int j = 0; j <= twos_count; j++)
            {
                if ((rem + i * 6 + j * 2) % 9 == 0)
                {
                    marker = true;
                    break;
                }
            }
            if (marker)
                break;
        }

        if (marker)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string num;
        cin >> num;

        int twos_count = 0, threes_count = 0, sum = 0;

        // Process digits as a string to avoid overflow
        for (char digit : num)
        {
            int k = digit - '0';
            if (k == 2)
                twos_count++;
            else if (k == 3)
                threes_count++;
            sum += k;
        }

        if (sum % 9 == 0)
        {
            cout << "YES" << endl;
            continue;
        }

        // Limit effective counts based on problem constraints
        twos_count = min(twos_count, 5);
        threes_count = min(threes_count, 3);

        bool marker = false;
        int rem = sum % 9;

        for (int i = 0; i <= threes_count; i++)
        {
            for (int j = 0; j <= twos_count; j++)
            {
                if ((rem + i * 6 + j * 2) % 9 == 0)
                {
                    marker = true;
                    break;
                }
            }
            if (marker)
                break;
        }

        if (marker)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }

    return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

#define int long long

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define BUFF ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)

const int INF = 0x3F3F3F3F;

signed main()
{
    BUFF;
    int t;
    string num;
    cin >> t;
    while(t--) {
        cin >> num;

        int soma = 0;
        int num2 = 0, num3 = 0;
        for(int i = 0; i < num.size(); i++) {
            soma += num[i] - '0';
            soma % 9;
            if(num[i] == '2') num2++;
            if(num[i] == '3') num3++;
        }

        if(soma % 9 == 0 || num3 == num.size()) cout << "YES" << endl;
        else {
            while(soma % 2 == 0 && num3) {
                soma = (soma+6)%9;
                num3--;
            }

            while(soma % 2 == 0 && num2) {
                soma = (soma+2)%9;
                num2--;
            }
            if((soma == 1 && num2 >= 4) || (soma == 3 && num2 >= 3) || (soma == 5 && num2 >= 2) || (soma == 7 && num2 >= 1)) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define int long long

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;

#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define BUFF ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)

const int INF = 0x3F3F3F3F;

bool possible(int num, int num2, int num3) {
    if(num == 9 || num == 18)  return true;
    if(num > 18) return false;

    if(num2 && num3) {
        if(possible(num+2, num2-1, num3)) return true;
        if(possible(num+6, num2, num3-1)) return true;
    } else if(num3) {
        if(possible(num+6, num2, num3-1)) return true;
    } else if(num2) {
        if(possible(num+2, num2-1, num3)) return true;
    }

    return false;
}

signed main()
{
    BUFF;
    int t;
    string num;
    cin >> t;
    while(t--) {
        cin >> num;

        int soma = 0;
        int num2 = 0, num3 = 0;
        for(int i = 0; i < num.size(); i++) {
            soma += num[i] - '0';
            soma % 9;
            if(num[i] == '2') num2++;
            if(num[i] == '3') num3++;
        }

        if(soma % 9 == 0 || num3 == num.size()) cout << "YES" << endl;
        else {
            if(possible(soma%9, num2, num3)) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
    return 0;
}

Answer: 2


def is_divi9(num):
    s = sum(num)  # مجموع اولیه ارقام
    if s % 9 == 0:  # بررسی اولیه
        return "YES"

    # شمارش تعداد ارقام 2 و 3
    count_2 = num.count(2)
    count_3 = num.count(3)

    # بررسی تمام مقادیر ممکن از تغییر
    for c2 in range(count_2 + 1):  # تعداد تغییر 2ها از 0 تا count_2
        for c3 in range(count_3 + 1):  # تعداد تغییر 3ها از 0 تا count_3
            new_s = s + c2 * 2 + c3 * 6
            if new_s % 9 == 0:
                return "YES"

    return "NO"

# ورودی گرفتن
t = int(input())
for _ in range(t):
    num = [int(digit) for digit in input()]  # تبدیل عدد به لیست ارقام
    print(is_divi9(num))

from itertools import product

def is_divi9(num):
    s = sum(num)  # مجموع اولیه ارقام
    if s % 9 == 0:  # بررسی اولیه
        return "YES"

    # پیدا کردن ایندکس ارقام 2 و 3
    indices = [i for i in range(len(num)) if num[i] in (2, 3)]
    # تولید تمام ترکیب‌های ممکن از تغییر
    for changes in product((0, 1), repeat=len(indices)):  # 0: تغییر نکن، 1: تغییر کن
        new_s = s  # شروع از مجموع اولیه
        for idx, change in zip(indices, changes):
            if change == 1:  # اگر قرار است این رقم تغییر کند
                new_s += num[idx] ** 2 - num[idx]
        if new_s % 9 == 0:  # بررسی بخش‌پذیری مجموع جدید
            return "YES"

    return "NO"  # اگر هیچ حالتی موفق نبود

# ورودی گرفتن
t = int(input())
for _ in range(t):
    num = [int(digit) for digit in input()]  # تبدیل عدد به لیست ارقام
    print(is_divi9(num))

Answer: 1


#include <ostream>

#include <cstdio>

#include <algorithm>

#include<list>

#include<queue>

#include <iostream>

#include <cstring>

#include <math.h>

#include <cmath>

#include <string>

using namespace std;

int t,number=1,total,num2,num3;

char num[1005][100005];

int main(){

    scanf("%d",&t);

    while (t--)

    {

        total = 0;

        num2 = 0;

        num3 = 0;

        scanf("%s",num[number]);

        for (int i = 0; i < strlen(num[number]); i++)

        {

            total += (num[number][i] - '0');

            if (num[number][i] == '2')

            {

                num2 ++;

            }

            if (num[number][i] == '3')

            {

                num3 ++;

            }

        }

        if (total%9 == 0)

        {

            printf("YES\n");

            continue;

        }

        if ((total%9)%2 == 0)

        {

            total = 18-(total%9);

        }else{

            total = 9-(total%9);

        }

        int temp = num2+num3;

       for (int i = 1; i <= temp+1; i++)

       {

        if (total == 0)

        {

            printf("YES\n");

            break;

        }

         if (total>=6 && num3>0)

         {

            total -= 6;

            num3--;

         }

         else if (total>=2 && num2>0)

         {

            total -= 2;

            num2--;

       }

    }

    if (total == 0)

    {

        continue;

    }

    printf("NO\n");

        number++;

}

    return 0;

}

#include <ostream>

#include <cstdio>

#include <algorithm>

#include<list>

#include<queue>

#include <iostream>

#include <cstring>

#include <math.h>

#include <cmath>

#include <string>

using namespace std;

int t,total,num2,num3;

char num[100005];

int main(){

    scanf("%d",&t);

    while (t--)

    {

        total = 0;

        num2 = 0;

        num3 = 0;

        scanf("%s",num);

        for (int i = 0; i < strlen(num); i++)

        {

            total += (num[i] - '0');

            if (num[i] == '2')

            {

                num2 ++;

            }

            if (num[i] == '3')

            {

                num3 ++;

            }

        }

        if (total%9 == 0)

        {

            printf("YES\n");

            continue;

        }

        if ((total%9)%2 == 0)

        {

            total = 18-(total%9);

        }else{

            total = 9-(total%9);

        }

        int temp = num2+num3;

       for (int i = 1; i <= temp+1; i++)

       {

        if (total == 0)

        {

            printf("YES\n");

            break;

        }

         if (total>=6 && num3>0)

         {

            total -= 6;

            num3--;

         }

         else if (total>=2 && num2>0)

         {

            total -= 2;

            num2--;

       }

    }

    if (total == 0)

    {

        continue;

    }

    printf("NO\n");

}

    return 0;

}

Answer: 2


#include <bits/stdc++.h>
using namespace std;

int main() {
    int tt;
    cin >> tt;
    while(tt--) {
        string n;
        cin >> n;
        int sum = 0,two = 0,three = 0;
        for(int i = 0;i < n.length();i++) {
            sum += n[i] - '0';
            if(n[i] == '2') two++;
            if(n[i] == '3') three++;
        }
        int remainder = sum%9;
        int t = 0;
        two = min(two,9);
        three = min(three,9);
        for(int i = 0;i <= two;i++) {
            for(int j = 0;j <= three;j++) {
                if((remainder + (i*2 + j*6)) % 9 == 0) {
                    t = 1;
                    break;
                }
            }
        }
        if(t == 1) cout << "yes" << endl;
        else cout << "no" << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;

int main() {
    int tt;
    cin >> tt;
    while(tt--) {
        long long n;
        cin >> n;
        int sum = 0,two = 0,three = 0;
        while(n > 0) {
            sum += n%10;
            if(n%10 == 2) two++;
            if(n%10 == 3) three++;
            n = n/10;
        }
        int remainder = sum%9;
        int t = 0;
        if(remainder == 0) t = 1;
        two = min(two,9);
        three = min(three,9);
        for(int i = 0;i <= two;i++) {
            for(int j = 0;j <= three;j++) {
                if((remainder + (i*2 + j*6)) % 9 == 0) {
                    t = 1;
                    break;
                }
            }
        }
        if(t == 1) cout << "yes" << endl;
        else cout << "no" << endl;
    }
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int t;
int c;
int x,y;
int sum;
bool f=0;
int main(){
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		c=s.size();
		sum=0;
		f=0;
		x=0,y=0;
		for(int i=0;i<c;i++){
			sum+=int(s[i]-'0');
			if(int(s[i]-'0')==2){
				x++;
			}
			if(int(s[i]-'0')==3){
				y++;
			}
		}
		sum%=9;
		if(sum==0){
			cout<<"Yes"<<endl;
			continue;
		}
		for(int i=0;i<=x;i++){
			for(int j=0;j<=y;j++){
				if((sum+2*i+6*j)%9==0){
					f=1;
					break;
				}
			}
			if(f==1)break;
		}
		if(f==0){
			cout<<"No"<<endl;
		}else{
			cout<<"Yes"<<endl;
		}
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int t;
int a[N];
bool f=0;
int c;
void dfs(int i,int sum){
	if(i>c){//出口
		if(sum%9==0){
			f=1;
		}
		return;
	}
	//不变
	dfs(i+1,sum+a[i]);
	//能否变
	if(2<=a[i]&&a[i]<=3){//可以变
		a[i]*=a[i];
		dfs(i+1,sum+a[i]);
	}
	return;
}
int main(){
	cin>>t;
	while(t--){
		for(int i=1;i<=N;i++){
			a[i]=0;
		}
		string s;
		cin>>s;
		c=s.size();
		for(int i=0;i<c;i++){
			a[i+1]=int(s[i]-'0');
			if(a[i+1]>=2&&a[i+1]<=3){
			}
		}
		f=0;
		dfs(1,0);
		if(f==0){
			cout<<"No"<<endl;
		}else{
			cout<<"Yes"<<endl;
		}
	}
	return 0;
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;

int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int sum=0;
        int cnt2=0;
        int cnt3=0;
        for(int i=0; i<s.size(); i++){
            sum+=s[i]-'0';
            if(s[i]=='2')
                cnt2++;
            if(s[i]=='3')
                cnt3++;
        }
        int required=9-sum%9;
        if(required==9){
            cout<<"Yes"<<endl;
            continue;
        }
        int temp=-1;
        for(int i=cnt2; i>=0; i--){
            if((required-2*i)%6==0){
                temp=(required-2*i)/6;
                break;
            }
        }
        if(temp==-1){
            required+=9;
            for(int i=cnt2; i>=0; i--){
                if((required-2*i)%6==0){
                    temp=(required-2*i)/6;
                    break;
            }
        }
        }
        if(temp!=-1 && temp<=cnt3)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
}

#include<bits/stdc++.h>
using namespace std;

int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int sum=0;
        int cnt2=0;
        int cnt3=0;
        for(int i=0; i<s.size(); i++){
            sum+=s[i]-'0';
            if(s[i]=='2')
                cnt2++;
            if(s[i]=='3')
                cnt3++;
        }
        bool flag=false;
        for(int i=0; i<=cnt2; i++){
            for(int j=0; j<=cnt3; j++){
                int newNum=sum+2*i+6*j;
                if(newNum%9==0){
                    flag=true;
                    break;
                }
            }
            if(flag)
                break;
        }
        if(flag)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }
}

Answer: 2


no = int(input())
for i in range(no):
    l = list(map(int, list(input())))
    c2 = l.count(2)
    c3 = l.count(3)
    sumo = sum(l)
    ans = "NO"
    for two in range(c2+1):
        for three in range(c3+1):
            x = sumo + 2*two + 6*three
            if x%9==0:
                ans = "YES"
                break
        if ans == "YES":
            break
    print(ans)

no = int(input())
for i in range(no):
    l = list(map(int, input().split('')))
    c2 = l.count(2)
    c3 = l.count(3)
    x = 0
    ans = "NO"
    for two in range(c2):
        for three in range(c3):
            x += 2*two + 6*three
            if x%9==0:
                ans = "YES"
                break
        if ans == "YES":
            break
    print(ans)

Answer: 1


#include <iostream>

#include <vector>

#include <algorithm>

#include <cstdint>

#include <map>

#include <string>

#include <set>

#ifdef ONLINE_JUDGE

#define default_file(...)

#define dbg(...)

#else

#include "../local/debug.h"

#endif

void run_program(){

     std::string s;

     std::cin >> s;

     int sum = 0, cnt_two = 0, cnt_three = 0;

     for(auto& c : s)

          sum += int(c - '0'),

          cnt_two += (c == '2'),

          cnt_three += (c == '3');

     for(int two = 0; two < std::min(10, cnt_two + 1); ++two)

          for(int three = 0; three < std::min(10, cnt_three + 1); ++three)

               if((sum + two * 2 + three * 6) % 9 == 0)

                    return void(std::cout << "YES\n");

     std::cout << "NO\n";

}

int main() {

     default_file("");

     std::ios::sync_with_stdio(0); std::cin.tie(0);

     int t;

     std::cin >> t;

     while(t--)

     run_program();

     return 0;

}

#include <iostream>

#include <vector>

#include <algorithm>

#include <cstdint>

#include <map>

#include <string>

#include <set>

#ifdef ONLINE_JUDGE

#define default_file(...)

#define dbg(...)

#else

#include "../local/debug.h"

#endif

void run_program(){

     std::string s;

     std::cin >> s;

     int sum = 0, cnt_two = 0, cnt_three = 0;

     for(auto& c : s)

          sum += int(c - '0'),

          cnt_two += (c == '2'),

          cnt_three += (c == '3');

     int mod = sum % 9;

     if(mod == 0 || (mod % 2 == 1 && cnt_two >= (9 - mod) / 2)

                 || (mod == 3 && (cnt_three >= 1 || cnt_two >= 3))

                 || (mod == 6 && (cnt_three >= 2 || cnt_two >= 6))

                 || ((mod == 4 || mod == 2) && cnt_three >= 1 && cnt_two >= (6 - mod) / 2)

                 || (mod == 1 && cnt_three >= 2 && cnt_two >= 1)

                 || (mod == 5 && cnt_three >= 1 && cnt_two >= 2)

                 || (mod == 7 && cnt_three >= 1 && cnt_two >= 4)

                 || (mod == 8 && cnt_two >= 4))

          std::cout << "YES\n";

     else

          std::cout << "NO\n";

} // 7

// 6

int main() {

     default_file("");

     std::ios::sync_with_stdio(0); std::cin.tie(0);

     int t;

     std::cin >> t;

     while(t--)

     run_program();

     return 0;

}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main(){
    int t;
    cin>>t;
    while(t--){
        string n;
        cin>>n;
        int sum=0;
        int count2=0,count3=0;
        for(int i=0;i<n.length();i++){
            sum+=(n[i]-'0');
            if(n[i]=='3')count3++;
            else if(n[i]=='2')count2++;
        }
        if(sum%9==0){
            cout<<"YES\n";
            continue;
        }
        int rem=sum%9;
        if(count3==n.length()){
            cout<<"YES\n";
            continue;
        }
         if(count2/2 >=rem){
             cout<<"YES\n";
         }else if(rem%2){
             if((9-rem)/2<=count2){
                 cout<<"YES\n";
             }else{
                 cout<<"NO\n";
             }
             }else{cout<<"NO\n";
         }
    }
}

#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main(){
    int t;
    cin>>t;
    while(t--){
        string n;
        cin>>n;
        int sum=0;
        int count2=0,count3=0;
        for(int i=0;i<n.length();i++){
            sum+=(n[i]-'0');
            if(n[i]=='3')count3++;
            else if(n[i]=='2')count2++;
        }
        int rem=sum%9;
        if(rem==0){
            cout<<"YES\n";
            continue;
        }
        bool ans=false;
        for(int i=0;i<=min((int)10,count2);i++){
            for(int j=0;j<=min((int)10,count3);j++){
                 if((rem+ (i*2) + (j*6))%9==0){
                     ans=true;break;
                 }
            }
        }
        if(ans){
            cout<<"YES\n";
        }else{
            cout<<"NO\n";
        }
    }
}

Answer: 2


#include <iostream>
using namespace std;

void solve(){
     string s;
        cin>>s;

        int two=0,three=0,sum=0;
           bool flag=0;
        for(auto i:s){
            if(i=='2'){
                two++;
            }
            else if(i=='3'){
                three++;
            }
            sum+=(i-'0');
            sum=sum%9;

        }
            for(int j=0;j<=three;j++){
                for(int i=0;i<=two;i++){
                    long tsum=sum;
                    tsum+=i*2+j*6;

                    if(tsum%9==0){
                        cout<<"YES"<<endl;
                        return;
                    }
                }
            }

                cout<<"NO"<<endl;
            return;
}

int main()
{

    int t;
    cin>>t;
    while(t--){
        solve();

    }

}

int main()
{

    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;

        int two=0,three=0,sum=0;
           bool flag=0;
        for(auto i:s){
            if(i=='2'){
                two++;
            }
            else if(i=='3'){
                three++;
            }
            sum+=(i-'0');
            sum=sum%9;

            for(int j=0;j<=three;j++){
                for(int i=0;i<=two;i++){
                    long tsum=sum;
                    tsum+=i*2+j*6;

                    if(tsum%9==0){
                        flag=1;
                       break;
                    }
                }
            }

        }
        if(flag==1){
                cout<<"YES"<<endl;
            }
            else{
                cout<<"NO"<<endl;
            }
    }

}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

#define RANGE(i, a, b)                                                         \
    for (auto i = a - (a > b); (i < b) ^ (a > b); i += 1 - 2 * (a > b))
#define ll long long
/*
 1 -> 1 No change
 2 -> 4: +2 // only this can be applied twice, in that case: +5
 3 -> 9: +6
 So we can change x 2s to 4, and y 3s to 6s. The new remainder of the number
 becomes 2*x + 6*y == 0 [9]. We must find a solution for (x, y), with the upper
 bound being the actual number of occurence of 2s and 3s.
 Les restes possible d'un multiplication par 2 mod 9:
 1*2 = 2 [9]
 2*2 = 4 [9]
 3*2 = 6 [9]
 4*2 = 8 [9]
 5*2 = 0 [9]
 Les restes possible d'une multiplication par 6 mod 9:
 1*6 = 6 [9]
 2*6 = 3 [9]
 3*6 = 0 [9]
 Tableau des additions possibles:
 ---- 0 2 4 6 8
    0 0 2 4 6 7
    3 3 5 7 0 2
    6 6 8 1 3 5

*/

void solve() {
    ll n;
    cin >> n;
    ll count2 = 0, count3 = 0;
    ll rem = 0;
    for (; n > 0; n /= 10) {
        rem += n % 10;
        if (n % 10 == 2)
            count2++;
        else if (n % 10 == 3)
            count3++;
    }
    rem %= 9;
    // cout << count2 << " " << count3 << " " << rem << endl;
    if (count2 >= 5 && count3 >= 3) {
        cout << "Yes" << endl;
        return;
    }
    for (int n2 = 0; n2 <= min(count2, 10ll); n2++)
        for (int n3 = 0; n3 <= min(count3, 10ll); n3++)
            if ((n2 * 2 + n3 * 6 + rem) % 9 == 0) {
                cout << "Yes" << endl;
                return;
            }
    cout << "No" << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define RANGE(i, a, b)                                                         \
    for (auto i = a - (a > b); (i < b) ^ (a > b); i += 1 - 2 * (a > b))
#define ll long long
/*
 1 -> 1 No change
 2 -> 4: +2 // only this can be applied twice, in that case: +5
 3 -> 9: +6
 So we can change x 2s to 4, and y 3s to 6s. The new remainder of the number
 becomes 2*x + 6*y == 0 [9]. We must find a solution for (x, y), with the upper
 bound being the actual number of occurence of 2s and 3s.
 Les restes possible d'un multiplication par 2 mod 9:
 1*2 = 2 [9]
 2*2 = 4 [9]
 3*2 = 6 [9]
 4*2 = 8 [9]
 5*2 = 0 [9]
 Les restes possible d'une multiplication par 6 mod 9:
 1*6 = 6 [9]
 2*6 = 3 [9]
 3*6 = 0 [9]
 Tableau des additions possibles:
 ---- 0 2 4 6 8
    0 0 2 4 6 7
    3 3 5 7 0 2
    6 6 8 1 3 5

*/

void solve() {
    string n;
    cin >> n;
    ll count2 = 0, count3 = 0;
    ll rem = 0;
    for (auto d : n) {
        int c = d - '0';
        rem += c % 10;
        rem %= 9;
        if (c % 10 == 2)
            count2++;
        else if (c % 10 == 3)
            count3++;
    }
    for (int n2 = 0; n2 <= min(count2, 10ll); n2++)
        for (int n3 = 0; n3 <= min(count3, 10ll); n3++)
            if (((n2 * 2) % 9 + (n3 * 6) % 9 + rem) % 9 == 0) {
                cout << "Yes" << endl;
                return;
            }
    cout << "No" << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
    int q;
    cin>>q;
    while(q--){
        string n;
        cin>>n;
        int sum=0;
        int x2=0;
        int x3=0;
        for(int i=0;i<n.size();i++){
            int num = n[i] - '0';
            if(num==2){
                x2++;
            }
            if(num==3){
                x3++;
            }
            sum+=num;

        }
        bool flag=true;
        if(sum%9==0){
            cout<<"YES"<<endl;
        }
        else if(x3==n.size()){
            cout<<"YES"<<endl;
        }

        else{
            for(int i=1;i<=x2;i++){
                for(int j=0;j<=x3;j++){
                    int x=sum+2*i+6*j;
                    if((x)%9==0){
                        cout<<"YES"<<endl;
                        flag=false;
                        break;
                    }
                }
                if(flag==false){
                    break;
                }
            }
            if(flag==true){
                cout<<"NO"<<endl;
            }
        }

    }
}

#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
    int q;
    cin>>q;
    while(q--){
        string n;
        cin>>n;
        int sum=0;
        int x2=0;
        int x3=0;
        for(int i=0;i<n.size();i++){
            int num = n[i] - '0';
            if(num==2){
                x2++;
            }
            if(num==3){
                x3++;
            }
            sum+=num;

        }
        bool flag=true;
        if(sum%9==0){
            cout<<"YES"<<endl;
        }

        else{
            if(flag==true){
                for(int j=0;j<=x3;j++){
                    int x=sum+6*j;
                    if((x)%9==0){
                        cout<<"YES"<<endl;
                        flag=false;
                        break;
                    }
                }
            }
            if(flag==true){
                for(int i=1;i<=x2;i++){

                    int x=sum+2*i;
                    if((x)%9==0){
                        cout<<"YES"<<endl;
                        flag=false;
                        break;
                    }
                }
            }
            if(flag==true){
                 for(int i=1;i<=x2;i++){
                for(int j=0;j<=x3;j++){
                    int x=sum+2*i+6*j;
                    if((x)%9==0){
                        cout<<"YES"<<endl;
                        flag=false;
                        break;
                    }
                }
                if(flag==false){
                    break;
                }
            }
            }

            if(flag==true){
                cout<<"NO"<<endl;
            }
        }

    }
}

Answer: 2


import java.io.*;
import java.util.*;

public class TLE {
  public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
  static FastReader in = new FastReader();

  public static void main(String[] args) throws Exception {
    int testCases = in.nextInt();
    while (testCases-- > 0) {
      solve();
    }
    out.close();
  }

  private static void solve() {
    long sum = 0;
    String s = in.next();
    int two = 0, three = 0;
    for (char c : s.toCharArray()) {
      sum += c - '0';
      if (c == '2') {
        two++;
      } else if (c == '3') {
        three++;
      }
    }
    for (int t = 0; t <= two; t++) {
      for (int th = 0; th <= three; th++) {
        if ((sum + 2 * t + th * 6) % 9 == 0) {
          System.out.println("YES");
          return;
        }
      }
    }
    System.out.println("NO");

  }

  static class FastReader {
    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
      br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
      while (st == null || !st.hasMoreElements()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(next());
    }

    long nextLong() {
      return Long.parseLong(next());
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    String nextLine() {
      String str = "";
      try {
        str = br.readLine();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }
  }
}

import java.io.*;
import java.util.*;

public class TLE {
  public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
  static FastReader in = new FastReader();

  public static void main(String[] args) throws Exception {
    int testCases = in.nextInt();
    while (testCases-- > 0) {
      solve();
    }
    out.close();
  }

  private static void solve() {
    long sum = 0;
    String s = in.next();
    int two = 0, three = 0;
    for (char c : s.toCharArray()) {
      sum += c - '0';
      if (c == '2') {
        two++;
      } else if (c == '3') {
        three++;
      }
    }
    if (sum % 9 == 0) {
      System.out.println("YES");
    } else {
      long modN = sum % 9;
      long mod = 9 - modN;
      if (mod == 2 && two >= 1) {
        System.out.println("YES");
      } else if (mod == 4 && two >= 2) {
        System.out.println("YES");
      } else if (mod == 3 && three >= 2) {
        System.out.println("YES");
      } else if (mod == 6 && (two >= 3 || three >= 2)) {
        System.out.println("YES");
      } else if (mod == 8 && (three >= 2 && two >= 1) || (two >= 4)) {
        System.out.println("YES");
      } else if (mod == 5 && two >= 1 && three >= 1) {
        System.out.println("YES");
      } else if (mod == 7 && two >= 2 && three >= 1) {
        System.out.println("YES");
      } else {
        System.out.println("NO");
      }
    }

  }

  static class FastReader {
    BufferedReader br;
    StringTokenizer st;

    public FastReader() {
      br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
      while (st == null || !st.hasMoreElements()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(next());
    }

    long nextLong() {
      return Long.parseLong(next());
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    String nextLine() {
      String str = "";
      try {
        str = br.readLine();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }
  }
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> takearray(int n)
{
    vector<int> a;
    for(int i=0;i<n;i++)
    {
        int x;
        cin >> x;
        a.push_back(x);
    }
    return a;
}
// bool cmp(auto a, auto b)
// {
//     return a.second < b.second;
// }
int getmax(vector<int> a)
{
    int x = LLONG_MIN;
    for(int i=0;i<a.size();i++)
    {
        if(x<a[i]) x = a[i];
    }
    return x;
}
void printvec(vector<int> a){for(int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;}
//bool help(pair<int,int> p1,pair<int,int> p2)
// {
//     if(p1.first > p2.first) return 1;
//     if(p1.first<p2.first) return -1;
//     return 0;
// }
// // pair<int, int> getElement(int n, int l)
// // {
// //     int i = static_cast<int>(std::floor((-1 + std::sqrt(1 + 8 * l)) / 2));
// //     int indexInRow = l - (i * (i + 1)) / 2;
// //     int j = i + indexInRow + 1;
// //     return {i + 1, j};
// // }
// int funcc(vector<int> &a)
// {
//     int n = a.size();
//     multiset<int> ms;
//         for(int i=0;i<n-1;i=i+2)
//         {
//             ms.insert(a[i+1]-a[i]);
//         }
//         auto it = ms.rbegin();
//         return *it;
// }

void solve()
{
    string s;
    cin >> s;
    int sum = 0;
    int freq_o=0,freq_tw=0,freq_th=0;
    while(s.length()>0)
    {
        int pos = s.length();
        if(s[pos-1]=='2') freq_tw++;
        else if(s[pos-1]=='3') freq_th++;
        sum+=(s[pos-1]-'0');
        sum%=9;
        s=s.substr(0,s.length()-1);
    }
    for(int i=0;i<=min(freq_tw,8ll);i++)
        {
            for(int j=0;j<=min(freq_th,8ll);j++)
                {
                    if((2ll*i+6ll*j+sum)%9ll==0)
                    {
                        cout << "Yes" << endl;
                        return;
                    }
                }
        }
    cout << "No" << endl;
    return;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> takearray(int n)
{
    vector<int> a;
    for(int i=0;i<n;i++)
    {
        int x;
        cin >> x;
        a.push_back(x);
    }
    return a;
}
// bool cmp(auto a, auto b)
// {
//     return a.second < b.second;
// }
int getmax(vector<int> a)
{
    int x = LLONG_MIN;
    for(int i=0;i<a.size();i++)
    {
        if(x<a[i]) x = a[i];
    }
    return x;
}
void printvec(vector<int> a){for(int i=0;i<a.size();i++) cout << a[i] << " "; cout << endl;}
//bool help(pair<int,int> p1,pair<int,int> p2)
// {
//     if(p1.first > p2.first) return 1;
//     if(p1.first<p2.first) return -1;
//     return 0;
// }
// // pair<int, int> getElement(int n, int l)
// // {
// //     int i = static_cast<int>(std::floor((-1 + std::sqrt(1 + 8 * l)) / 2));
// //     int indexInRow = l - (i * (i + 1)) / 2;
// //     int j = i + indexInRow + 1;
// //     return {i + 1, j};
// // }
// int funcc(vector<int> &a)
// {
//     int n = a.size();
//     multiset<int> ms;
//         for(int i=0;i<n-1;i=i+2)
//         {
//             ms.insert(a[i+1]-a[i]);
//         }
//         auto it = ms.rbegin();
//         return *it;
// }

void solve()
{
    int n;
    cin >> n;
    int sum = 0;
    int x = n;
    int freq_o=0,freq_tw=0,freq_th=0;
    while(x>0)
    {
        if(x%10==2) freq_tw++;
        else if(x%10==3) freq_th++;
        sum+=x%10;
        x=x/10;
    }
    if(sum%9 == 0)
    {
        cout << "Yes" << endl;
        return;
    }
    else
    {
        sum = sum%9;
        if((sum == 7ll)&&(freq_tw))
        {
            cout << "Yes" << endl;
            return;
        }
        else if((sum==3ll) && (freq_th))
        {
            cout << "Yes" << endl;
            return;
        }
        else if((sum == 1ll) && freq_th && freq_tw)
        {
            cout << "Yes" << endl;
            return;
        }
        else
        {
            for(int i=0;i<=min(freq_tw,8ll);i++)
            {
                for(int j=0;j<=min(freq_th,8ll);j++)
                {
                    if((2*i+6*j+sum)%9==0)
                    {
                        cout << "Yes" << endl;
                        return;
                    }
                }
            }
        }
    }
    cout << "No" << endl;
    return;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

// Function to check if a number represented by a string is divisible by 9
bool isDivisibleBy9(const string &s)
{
    int sum = 0;
    for (char c : s)
    {
        sum += (c - '0');
    }
    return (sum % 9 == 0);
}

// Function to determine if a number can become divisible by 9
string uninteresting_numbers(const string &s)
{
    int n = s.length();
    // DP table: dp[i][sum] indicates if it's possible to achieve the `sum`
    // using digits up to index `i`.
    vector<vector<bool>> dp(n + 1, vector<bool>(9, false));

    // Base case: sum 0 is achievable with no digits used.
    dp[0][0] = true;

    // Iterate over each digit of the number
    for (int i = 0; i < n; i++)
    {
        int digit = s[i] - '0';
        for (int sum = 0; sum < 9; sum++)
        {
            // If `sum` is achievable using the first `i` digits
            if (dp[i][sum])
            {
                // Case 1: Do not modify this digit
                dp[i + 1][(sum + digit) % 9] = true;

                // Case 2: Replace the digit with its square (if <= 3)
                if (digit <= 3)
                {
                    int squared = digit * digit;
                    dp[i + 1][(sum + squared) % 9] = true;
                }
            }
        }
    }

    // Check if any sum divisible by 9 is achievable using all digits
    for (int sum = 0; sum < 9; sum++)
    {
        if (dp[n][sum] && sum == 0)
        {
            return "YES";
        }
    }
    return "NO";
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string number;
        cin >> number;
        cout << uninteresting_numbers(number) << endl;
    }
    return 0;
}

// https://codeforces.com/problemset/problem/2050/C
// uninteresting numbers
#include <bits/stdc++.h>
using namespace std;
bool test(string s)
{
    int sum = 0;
    for (int i = 0; i < s.length(); i++)
    {
        sum += s[i] - '0';
    }
    if (sum % 9 == 0)
        return true;
    return false;
}
void solution(string s, bool &counter, vector<bool> &visited)
{
    if (counter)
        return;
    counter = test(s);
    for (int i = 0; i < s.length(); i++)
    {
        if (!visited[i])
        {
            int no = s[i] - '0';
            visited[i] = true;
            if (no <= 3)
            {
                string copy = s;
                copy[i] = '0' + no * no;
                solution(copy, counter, visited);
            }
        }
    }
}
string uninteresting_numbers(string n)
{
    bool counter = false;
    vector<bool> visited(n.length(), false);
    solution(n, counter, visited);
    if (counter)
    {
        return "YES";
    }
    return "NO";
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string number;
        cin >> number;
        cout << uninteresting_numbers(number) << endl;
    }
    return 0;
}

Answer: 1


#include<bits/stdc++.h>

using namespace std;

#define int long long

signed main() {

    int t;

    cin>>t;

    while(t--){

        string s;

        cin>>s;

        int n = s.size();

        int sum = 0;

        int two,three;

        two = three = 0;

        for(int i=0;i<n;i++){

            sum+=s[i]-'0';

            if(s[i]-'0' == 2) two++;

            if(s[i]-'0' == 3) three++;

        }

        int flag=0;

        int a =10;

        two = min(a,two);

        three = min(a,three);

        for(int i=0;i<=two;i++){

            for(int j=0;j<=three;j++){

                if((sum+i*2+j*6)%9==0){

                    flag++;

                    break;

                }

            }

        }

        if(flag) cout<<"YES\n";

        else{

            cout<<"NO\n";

        }

        }

    return 0;

}

#include<bits/stdc++.h>

using namespace std;

#define int long long

signed main() {

    int t;

    cin>>t;

    while(t--){

        string s;

        cin>>s;

        int n = s.size();

        int sum = 0;

        int two,three;

        two = three = 0;

        for(int i=0;i<n;i++){

            sum+=s[i]-'0';

            if(s[i]-'0' == 2) two++;

            if(s[i]-'0' == 3) three++;

        }

        if(sum%9==0) cout<<"YES\n";

        else{

            int d = 9 - sum%9;

            if(d%2!=0) d+=9;

            // cout<<d<<" "<<two*2 + three*6<<endl;

            if(two*2 + three*6 >=d  ){

                d = d - two*2;

                if(d>=0 &&d%6!=0 )

                cout<<"NO\n";

                else if(d>=0 && d%6==0 && d/6 < three){

                    cout<<"NO\n";

                }

                else

                cout<<"YES\n";

            }

            else{

                cout<<"NO\n";

            }

        }

    }

    return 0;

}

Answer: 1


#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        ll n;
        cin >> n;

        vector<int> digits;
        int sum = 0;

        while (n > 0) {
            int temp = n % 10;
            digits.push_back(temp);
            sum += temp;
            n /= 10;
        }

        if (sum % 9 == 0) {
            cout << "YES" << endl;
            continue;
        }

        int count2 = 0, count3 = 0;
        for (int digit : digits) {
            if (digit == 2) count2++;
            if (digit == 3) count3++;
        }
        bool possible = false;
        for (int i = 0; i <= count2 && !possible; i++) {
            for (int j = 0; j <= count3 && !possible; j++) {
                int newSum = sum + i * 2 + j * 6;
                if (newSum % 9 == 0) {
                    possible = true;
                    break;
                }
            }
            if(possible){break;}
        }

        if (possible) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}

#include <bits/stdc++.h>
#define ll long long int
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        string n;
        cin >> n;
        vector<int> digits;
        int sum = 0;
        for (char c : n) {
            int digit = c - '0';
            digits.push_back(digit);
            sum += digit;
        }
        if (sum % 9 == 0) {
            cout << "YES" << endl;
            continue;
        }
        int count2 = 0, count3 = 0;
        for (int digit : digits) {
            if (digit == 2) count2++;
            if (digit == 3) count3++;
        }
        bool possible = false;
        for (int i = 0; i <= count2 && !possible; i++) {
            for (int j = 0; j <= count3 && !possible; j++) {
                int newSum = sum + i * 2 + j * 6;
                if (newSum % 9 == 0) {
                    possible = true;
                    break;
                }
            }
        }
        if (possible) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define pb push_back

#define F first

#define S second

#define pii pair<int,int>

#define vi vector<int>

#define vpii vector<pii>

#define loop(i,n) sloop(0,i,n)

#define sloop(s, i, n) for(ll i=(s);i<(n);i++)

#define rloop(i,n) rsloop(0,i,n)

#define rsloop(s,i,n) for(ll i=(n);i-->(s);)

#define all(v) (v).begin(),(v).end()

#ifdef DEBUG

#define DBG cout << __LINE__ << endl;

#else

#define DBG

#endif

//int xses[8] = {-1,1,0,0,-1,1,1,-1};

//int yses[8] = {0,0,-1,1,-1,1,-1,1};

//#define int ll

void solve()

{

	string s;

	cin >> s;

	int sum=0;

	int cnt2=0,cnt3=0;

	for(char c:s)

	{

		sum+=c-'0';

		if(c=='2')cnt2++;

		if(c=='3')cnt3++;

	}

	sum%=9;

	cnt2%=9;

	cnt3%=9;

	loop(i,cnt2+1)

	{

		loop(j,cnt3+1)

		{

			if((sum+6*j+2*i)%9==0){cout << "YES\n";return;}

		}

	}

	cout << "NO\n";

}

signed main()

{

	cin.tie(0);

	ios_base::sync_with_stdio(0);

	int t;

	cin >> t;

	while(t--)

		solve();

	return 0;

}

#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define pb push_back

#define F first

#define S second

#define pii pair<int,int>

#define vi vector<int>

#define vpii vector<pii>

#define loop(i,n) sloop(0,i,n)

#define sloop(s, i, n) for(ll i=(s);i<(n);i++)

#define rloop(i,n) rsloop(0,i,n)

#define rsloop(s,i,n) for(ll i=(n);i-->(s);)

#define all(v) (v).begin(),(v).end()

#ifdef DEBUG

#define DBG cout << __LINE__ << endl;

#else

#define DBG

#endif

//int xses[8] = {-1,1,0,0,-1,1,1,-1};

//int yses[8] = {0,0,-1,1,-1,1,-1,1};

//#define int ll

void solve()

{

	string s;

	cin >> s;

	int sum=0;

	int cnt2=0,cnt3=0;

	for(char c:s)

	{

		sum+=c-'0';

		if(c=='2')cnt2++;

		if(c=='3')cnt3++;

	}

	sum%=9;

	cnt2=min(cnt2,9);

	cnt3=min(cnt3,3);

	loop(i,cnt2+1)

	{

		loop(j,cnt3+1)

		{

			if((sum+6*j+2*i)%9==0){cout << "YES\n";return;}

		}

	}

	cout << "NO\n";

}

signed main()

{

	cin.tie(0);

	ios_base::sync_with_stdio(0);

	int t;

	cin >> t;

	while(t--)

		solve();

	return 0;

}

Answer: 2