Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 124 ms):
#  /\_/\  
# ( o.o ) 
#  > ^ <
#   ...
# Online Judge Please PASS Me :)

import sys
from collections import defaultdict

def slowestIO():
    input = sys.stdin.read
    data = input().splitlines()
    return data

def definitelyNotMain():
    data = slowestIO()
    index = 0
    test_cases = int(data[index])
    index += 1
    results = []

    for _ in range(test_cases):
        num = data[index]
        index += 1
        n = len(num)
        
        rem = sum([int(ch) for ch in num]) % 9
        target = 9 - rem
        if rem % 2 == 0:
            target = 18 - rem
        
        ans = "YES"
        twos, threes = 0, 0
        for ch in num:
            twos += ch == "2"
            threes += ch == "3"
            
        cur = 0
        check2s = twos
        while twos > 0:
            twos -= 1
            cur += 2
            if cur > target:
                cur -= 2
                break
            
        while cur < target and threes > 0:
            threes -= 1
            cur += 6
            
        diff = cur - target
                
        if not(diff == 2 and check2s > 0) and not(diff == 4 and check2s > 1) and (rem > 0 and cur != target):
            ans = "NO"
                
        results.append(str(ans))

    sys.stdout.write('\n'.join(results) + '\n')

if __name__ == "__main__":
    definitelyNotMain()


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()