Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 124 ms):
#                             * بسم الله الرحمن الرحيم *
#     ﴾ رَبِّ اشْرَحْ لِي صَدْرِي * وَيَسِّرْ لِي أَمْرِي * وَاحْلُلْ عُقْدَةً مِّن لِّسَانِي * يَفْقَهُوا قَوْلِي ﴿
#  
t=int(input())
for i in range(t):
    n=input()
    intn=int(n)
    result="NO"
    count_2 = n.count('2') 
    count_3 = n.count('3')
    if intn%9==0:
        result="YES"
    else:
        rest=intn%9
        if rest in [1,3,5,7]:
            if rest==7:
                if count_2>=1:
                    result="YES"
            elif rest==5:
                if count_2>=2:
                    result="YES"
            elif rest==3:
                if count_2>=3 or count_3>=1:
                    result="YES"
            elif rest==1:
                if (count_2>=1 and count_3>=1) or count_2>=4:
                    result="YES"
        elif rest in [2,4,6,8]:
            if rest==8:
                if (count_2>=2 and count_3>=1) or count_2>=5:
                    result="YES"
            elif rest==6:
                if (count_3>=2) or (count_2>=3 and count_3>=1) or count_2>=6:
                    result="YES"
            elif rest==4:
                if (count_2>=4 and count_3>=1) or (count_2>=1 and count_3>=2) or count_2>=7:
                    result="YES"
            elif rest==2:
                if (count_2>=5 and count_3>=1) or (count_2>=2 and count_3>=2) or count_2>=8:
                    result="YES"
                
    print(result)

Solution 2 (runtime: 249 ms):
# ======= author: kuanhungchen.com | created: 05/02/23 08:33:06 CST ======= #
from sys            import stdin,  stderr, stdout, setrecursionlimit
from bisect         import bisect_left, bisect_right
from collections    import defaultdict, deque, Counter
from itertools      import accumulate, combinations, permutations, product
from functools      import lru_cache, cmp_to_key, reduce
from heapq          import heapify, heappush, heappop, heappushpop, heapreplace
# from pypyjit        import set_param
# set_param("max_unroll_recursion=-1")
# setrecursionlimit(300005)
orda  = 97
ordA  = 65
INF   = 1 << 60
MOD   = 1000000007
MOD2  = 998244353
input = lambda: stdin.readline().rstrip("\r\n")
linen = lambda cf_: "[L" + str(cf_.f_back.f_lineno) + "] "
dbg   = lambda *A, **M: stderr.write("\033[91m" + \
        M.get("sep", " ").join(map(str, A)) + M.get("end", "\n") + "\033[0m")
# ============================ START OF MY CODE ============================ #

def solve(_tc):
    s = input()
    n = len(s)
    dp = [set() for _ in range(n)]
    x = int(s[0])
    while x <= 9:
        dp[0].add(x % 9)
        if x == 0 or x == 1: break
        x *= x
    for i in range(1, n):
        x = int(s[i])
        xs = []
        while x <= 9:
            xs.append(x)
            if x == 0 or x == 1: break
            x *= x
        st = set()
        for d in dp[i - 1]:
            for x in xs:
                st.add((d + x) % 9)
        dp[i] = st
    if 0 in dp[-1]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    _tcs = int(input())
    for _tc in range(1, vars().get("_tcs", 1) + 1):
        dbg("=== Case {} ===".format(str(_tc).rjust(2)))
        solve(_tc)