Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 156 ms):
import sys, os, math, random
from _collections_abc import range_iterator
from io import BytesIO, IOBase
import copy
from collections import defaultdict
import random



# sys.setrecursionlimit(10**6)
BUFSIZE = 4096
MOD = 10 ** 9 + 7
MODD = 998244353
inf = float('inf')


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

input = lambda: sys.stdin.readline().rstrip("\r\n")
i = lambda: input()
ii = lambda: int(input())
MI = lambda: map(str, input().split())
mi = lambda: map(int, input().split())
li = lambda: list(input().split())
lmi = lambda: list(map(int, input().split()))
zli = lambda: [0] + list(map(int, input().split()))


def find_factors(n):
    factors = []
    if n == 1:
        factors.append(1)
    for i in range(1, math.ceil(math.sqrt(n)) + 1):
        if n % i == 0:
            if i * i == n:
                factors.append(i)
            else:

                factors.append(i)
                factors.append(n // i)
    s = set(factors)
    factors = list(s)

    return (factors)





import bisect


def standard_binary_search(array, searchVal):
    return bisect.bisect_left(array, searchVal + 1) - 1







caca=0
tata=False
def solve():
    a=input()
    n=len(a)
    s=0
    twos=0
    threes=0
    lst=[2,4,6,8,1,3,5,7,0]
    for i in range(n):
        s+=int(a[i])
        if a[i]=='2':
            twos+=1

        if a[i]=='3':
            threes+=1


    if s%9==0:
        print('yes')
        return
    num=s%9
    i1=lst.index(num)
    for i in range(min(2,threes)):
        jj=(lst[i1]+6)%9
        
        i1=max(i1,lst.index(jj))
        if i1==8:
            print('yes')
            return
    for i in range(min(10,twos)):
        i1=lst.index((lst[i1]+2)%9)
        if i1==8:
            print('yes')
            return
    print('no')














l = ii()

for i in range(l):




    solve()

Solution 2 (runtime: 140 ms):
import sys
from heapq import heappush, heappop, heapify
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
from itertools import accumulate, groupby
from math import gcd, lcm, isqrt
import random
 
class DefaultDict:
    def __init__(self, default=None):
        self.default = default
        self.x = random.randrange(1, 1 << 31)
        self.dd = defaultdict(default)
    def __repr__(self):
        return "{"+", ".join(f"{k ^ self.x}: {v}" for k, v in self.dd.items())+"}"
    def __eq__(self, other):
        for k in set(self) | set(other):
            if self[k] != other[k]: return False
        return True
    def __or__(self, other):
        res = DefaultDict(self.default)
        for k, v in self.dd: res[k] = v
        for k, v in other.dd: res[k] = v
        return res
    def __len__(self):
        return len(self.dd)
    def __getitem__(self, item):
        return self.dd[item ^ self.x]
    def __setitem__(self, key, value):
        self.dd[key ^ self.x] = value
    def __delitem__(self, key):
        del self.dd[key ^ self.x]
    def __contains__(self, item):
        return item ^ self.x in self.dd
    def items(self):
        for k, v in self.dd.items(): yield (k ^ self.x, v)
    def keys(self):
        for k in self.dd: yield k ^ self.x
    def values(self):
        for v in self.dd.values(): yield v
    def __iter__(self):
        for k in self.dd: yield k ^ self.x
class CounterInt(DefaultDict):
    def __init__(self, aa=[]):
        super().__init__(int)
        for a in aa: self.dd[a ^ self.x] += 1
    def __add__(self, other):
        res = CounterInt()
        for k in set(self) | set(other):
            v = self[k]+other[k]
            if v > 0: res[k] = v
        return res
    def __sub__(self, other):
        res = CounterInt()
        for k in set(self) | set(other):
            v = self[k]-other[k]
            if v > 0: res[k] = v
        return res
    def __and__(self, other):
        res = CounterInt()
        for k in self:
            v = min(self[k], other[k])
            if v > 0: res[k] = v
        return res
    def __or__(self, other):
        res = CounterInt()
        for k in set(self) | set(other):
            v = max(self[k], other[k])
            if v > 0: res[k] = v
        return res
class Set:
    def __init__(self, aa=[]):
        self.x = random.randrange(1, 1 << 31)
        self.st = set()
        for a in aa: self.st.add(a ^ self.x)
    def __repr__(self):
        return "{" + ", ".join(str(k ^ self.x) for k in self.st) + "}"
    def __len__(self):
        return len(self.st)
    def add(self, item):
        self.st.add(item ^ self.x)
    def discard(self, item):
        self.st.discard(item ^ self.x)
    def __contains__(self, item):
        return item ^ self.x in self.st
    def __iter__(self):
        for k in self.st: yield k ^ self.x
    def pop(self):
        return self.st.pop() ^ self.x
    def __or__(self, other):
        res = Set(self)
        for a in other: res.add(a)
        return res
    def __and__(self, other):
        res = Set()
        for a in self:
            if a in other: res.add(a)
        for a in other:
            if a in self: res.add(a)
        return res
 
def ceiling(x, y):
    return (x+y-1) // y
 
def sumtoN(n):
    return ((n)*(n+1)) // 2
 
def sieve(n):
    prim = [False] * (n + 1)
    ret = []
    for i in range(2, n + 1):
        if not prim[i]:
            ret.append(i)
            for j in range(i, n + 1, i):
                prim[j] = 1
 
    return ret


input = lambda: sys.stdin.readline().strip()
printerr = lambda *args, **kwargs: print("\u001B[31m", *args, "\u001B[0m", file=sys.stderr, **kwargs)

def main():
    s = [int(x) for x in list(input())]
    sm = sum(s) % 9
    twos = s.count(2)
    threes = s.count(3)

    for i in range(min(10, twos + 1)):
        for j in range(min(10, threes + 1)):
            if (sm + 2 * i + 6 * j) % 9 == 0:
                print("YES")
                return
    print("NO")
    
    

tcs = int(input())
for tc in range(tcs):
    main()