You are given an integer N. Is there a permutation of digits of integer that's divisible by 8? A permutation of digits of integer N is defined as an integer formed by rearranging the digits of N. For example, if the number N = 123, then {123, 132, 213, 231, 312, 321} are the possible permutations.

Input Format 

The first line contains an integer T i.e. number of test cases. 

T lines follow, each containing the integer N. 

Output Format 

For each test case print YES if there exists one such re-arrangement of N such that it is divisible by 8 or NO if there isn't. 

Constraints 

1 <= T <= 45

0 <= N <= 10^{110}  

Note 

Re-arrangements of 10 are {10, 01} which boils down to {10, 1}.

Sample Input  

2
61
75

Sample Output  

YES
NO

Explanation 

Test case #00: 16 is permutation of 61 which is divisible by 8. 

Test case #01: None of permutation of 75, {57, 75}, are divisible by 8.

Source: hackerrank | Difficulty: MEDIUM
Tags: Number theory


Solutions:
import itertools, sys
T = int(sys.stdin.readline())
for _ in range(T):
	N = sys.stdin.readline().strip()
	if len(N) == 1:
		print('YES' if N == '8' or N == '0' else 'NO')
	elif len(N) == 2:
		print('YES' if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0 else 'NO')
	else:
		match = False
		for s in itertools.permutations(N, 3):
			n = int(''.join(s))
			if n % 8 == 0:
				print('YES')
				match = True
				break
		if not match:
			print('NO')


import sys

def digits(s):
	d = [int(c) for c in list(s)]
	d.sort()
	return d
e = 0
eights = []
while e < 1000:
	eights.append(digits('{:03.0f}'.format(e)))
	e += 8
n = int(input())
for t in range(n):
	x = digits(input())
	if len(x) == 1:
		x = x[0]
		print('YES' if x % 8 == 0 else 'NO')
	elif len(x) == 2:
		a = 10 * x[0] + x[1]
		b = 10 * x[1] + x[0]
		print('YES' if a % 8 == 0 or b % 8 == 0 else 'NO')
	else:
		result = 'NO'
		for e in eights:
			y = x[:]
			cool = False
			try:
				y.remove(e[0])
				y.remove(e[1])
				y.remove(e[2])
				result = 'YES'
				break
			except:
				pass
		print(result)


import itertools

def main():
	testcases = int(input())
	for _ in range(testcases):
		s = input()
		for p in itertools.permutations(s, 3) if len(s) > 3 else itertools.permutations(s):
			if int(''.join(p)) % 8 == 0:
				print('YES')
				break
		else:
			print('NO')
main()


from itertools import permutations
T = int(input())
N = [input() for _ in range(T)]
for num in N:
	mesh = []
	for n in range(10):
		mesh += [str(n)] * min(num.count(str(n)), 3)
	mesh = set(permutations(mesh, min(len(mesh), 3)))
	mesh = [int(''.join(n)) for n in mesh]
	print('YES' if any((n % 8 == 0 for n in mesh)) else 'NO')


import sys
import collections
table = []
tmp = 0
while tmp <= 1000:
	tmp_collect = collections.Counter(str(tmp))
	for ch in '0123456789':
		if ch not in tmp_collect:
			tmp_collect[ch] = 0
	table.append((tmp, len(str(tmp)), tmp_collect))
	tmp += 8
n = int(sys.stdin.readline())
for i in range(n):
	text = sys.stdin.readline()
	read_num = int(text)
	digits = len(str(read_num))
	read_count = collections.Counter(str(read_num))
	for record in table:
		num = record[0]
		counter = record[2]
		found = True
		if read_count['0'] < 3:
			for item in counter.items():
				if counter[item[0]] > read_count[item[0]]:
					found = False
					break
		if found == True:
			if digits == record[1] or read_count['0'] >= 3:
				break
			elif min(3, digits) - record[1] <= read_count['0'] - counter['0']:
				break
			else:
				found = False
	if found == False:
		print('NO')
	else:
		print('YES')


import sys
import copy

def f(words, dicts):
	tmp = copy.deepcopy(dicts)
	for letter in words:
		if tmp[letter] == 0:
			return 0
		else:
			tmp[letter] -= 1
	return 1
checklist_all = [['0', '8'], ['16', '24', '32', '40', '48', '56', '64', '72', '80', '88', '96'], ['000', '008', '016', '024', '032', '040', '048', '056', '064', '072', '080', '088', '096', '104', '112', '120', '128', '136', '144', '152', '160', '168', '176', '184', '192', '200', '208', '216', '224', '232', '240', '248', '256', '264', '272', '280', '288', '296', '304', '312', '320', '328', '336', '344', '352', '360', '368', '376', '384', '392', '400', '408', '416', '424', '432', '440', '448', '456', '464', '472', '480', '488', '496', '504', '512', '520', '528', '536', '544', '552', '560', '568', '576', '584', '592', '600', '608', '616', '624', '632', '640', '648', '656', '664', '672', '680', '688', '696', '704', '712', '720', '728', '736', '744', '752', '760', '768', '776', '784', '792', '800', '808', '816', '824', '832', '840', '848', '856', '864', '872', '880', '888', '896', '904', '912', '920', '928', '936', '944', '952', '960', '968', '976', '984', '992']]
T = int(sys.stdin.readline().rstrip('\n'))
for t in range(T):
	digit_count = {'0': 0, '1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9': 0}
	string_in = sys.stdin.readline().rstrip('\n')
	checklist = checklist_all[min(3, len(string_in)) - 1]
	for letter in string_in:
		digit_count[letter] += 1
	flag = 0
	for item in checklist:
		if f(item, digit_count):
			flag = 1
			break
	if flag:
		print('YES')
	else:
		print('NO')


def solve(s):
	mark = [0 for i in range(0, 10)]
	l = len(s)
	if l == 1:
		if int(s) % 8 == 0:
			return 1
		return 0
	if l == 2:
		a = int(s[0])
		b = int(s[1])
		if (10 * a + b) % 8 == 0 or (10 * b + a) % 8 == 0:
			return 1
		return 0
	for i in s:
		t = ord(i) - ord('0')
		mark[t] += 1
	for i in range(0, 10):
		mark[i] -= 1
		for j in range(0, 10):
			mark[j] -= 1
			for k in range(0, 10):
				mark[k] -= 1
				if mark[i] >= 0 and mark[j] >= 0 and (mark[k] >= 0):
					t = 100 * i + 10 * j + k
					if t % 8 == 0:
						return 1
				mark[k] += 1
			mark[j] += 1
		mark[i] += 1
	return 0
t = int(input())
for i in range(0, t):
	s = input()
	if solve(s) == 1:
		print('YES')
	else:
		print('NO')


l = ['000', '002', '004', '006', '008', '012', '014', '016', '023', '024', '025', '027', '028', '029', '034', '036', '044', '045', '046', '047', '048', '049', '056', '067', '068', '069', '088', '112', '123', '125', '126', '127', '128', '129', '136', '144', '146', '148', '166', '167', '168', '223', '224', '227', '234', '235', '236', '238', '239', '244', '246', '247', '248', '255', '256', '257', '258', '259', '267', '269', '278', '279', '288', '289', '299', '336', '344', '348', '356', '367', '368', '369', '445', '446', '447', '448', '449', '456', '458', '466', '468', '469', '478', '488', '489', '566', '567', '568', '669', '677', '678', '679', '688', '689', '888']
t = int(input())
while t:
	t -= 1
	s = input().strip()
	if len(s) <= 2:
		if int(s) % 8 == 0 or int(s[::-1]) % 8 == 0:
			ans = 'YES'
		else:
			ans = 'NO'
	else:
		ans = 'NO'
		for last in l:
			q = list(s)
			u = 0
			for j in last:
				if j in q:
					q.remove(j)
					u += 1
				else:
					break
			if u == 3:
				ans = 'YES'
				break
			else:
				continue
	print(ans)


from itertools import permutations

def fn(s):
	l = len(s)
	for i in range(0, l):
		for j in range(i + 1, l):
			for k in range(j + 1, l):
				n = s[i] + s[j] + s[k]
				perms = [''.join(p) for p in permutations(n)]
				for z in perms:
					if int(z) % 8 == 0:
						print('YES')
						return
	print('NO')
t = int(input())
for i in range(0, t):
	x = input()
	if len(x) == 1:
		if int(x) % 8 == 0:
			print('YES')
		else:
			print('NO')
	elif len(x) == 2:
		if int(x) % 8 == 0 or int(x[::-1]) % 8 == 0:
			print('YES')
		else:
			print('NO')
	else:
		fn(x)


import itertools
T = int(input())
for _ in range(T):
	n = input()
	r = min(4, len(n))
	for p in itertools.permutations(n, r):
		if int(''.join(p)) % 8 == 0:
			print('YES')
			break
	else:
		print('NO')


def digit_counts(s):
	counts = [0] * 10
	for c in s:
		counts[int(c)] += 1
	return tuple(counts)
all_eight_digit_counts = [digit_counts('{0:03}'.format(8 * x)) for x in range(125)]
for T in range(int(input())):
	N = input()
	if len(N) <= 2:
		if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0:
			print('YES')
		else:
			print('NO')
	else:
		N_digit_counts = digit_counts(N)
		for eight_digit_counts in all_eight_digit_counts:
			if all((N_digit_counts[i] >= eight_digit_counts[i] for i in range(10))):
				print('YES')
				break
		else:
			print('NO')


from itertools import permutations
for bita3 in range(int(input())):
	x = input()
	flag = False
	for i in permutations(x, 3):
		if int(''.join(i)) % 8 == 0:
			print('YES')
			flag = True
			break
	if len(x) < 3 and (not flag):
		if int(x) % 8 == 0 or int(x[::-1]) % 8 == 0:
			print('YES')
			flag = True
	if not flag:
		print('NO')


from bisect import bisect_left

def check(nn):
	if all([n % 8 for n in nn]):
		print('NO')
	else:
		print('YES')

def find(counted, n):
	remainder = n % 8
	d1 = 8 - remainder
	if counted[d1] > 0:
		return True
	if d1 != 1:
		return False
	if counted[d1 + 8] > 0:
		return True
	return False

def form(d1, d2):
	return d1 * 100 + d2 * 10
T = int(input())
for t in range(T):
	line = input()
	if len(line) == 1:
		check([int(line)])
		continue
	if len(line) == 2:
		check([int(line), int(line[1] + line[0])])
		continue
	counted = [0] * 10
	count = 0
	for ch in line:
		counted[int(ch)] += 1
	found = False
	for i in range(10):
		if counted[i] == 0:
			continue
		counted[i] -= 1
		for j in range(i, 10):
			if counted[j] < 1:
				continue
			counted[j] -= 1
			found = find(counted, form(i, j)) or find(counted, form(j, i))
			counted[j] += 1
			if found:
				break
		counted[i] += 1
		if found:
			break
	print('YES' if found else 'NO')


import itertools
t = int(input())
for i in range(0, t):
	isPerm = 'NO'
	n = int(input())
	length = len(str(n))
	a = [0 for k in range(0, length)]
	count = 1
	for j in range(0, length):
		factor = 10
		if n < 10:
			a[j] = n
		else:
			a[j] = n % factor
		n = int(n / 10)
	if length < 4:
		perm = list(itertools.permutations(a, length))
	else:
		perm = list(itertools.permutations(a, 3))
	for l in range(0, len(perm)):
		num = int(''.join(map(str, perm[l])))
		if num / 8 == int(num / 8):
			isPerm = 'YES'
			break
	print(isPerm)


endings = [['0', '0', '0'], ['0', '0', '8'], ['0', '1', '6'], ['0', '2', '4'], ['0', '3', '2'], ['0', '4', '0'], ['0', '4', '8'], ['0', '5', '6'], ['0', '6', '4'], ['0', '7', '2'], ['0', '8', '8'], ['0', '9', '6'], ['0', '1', '4'], ['1', '1', '2'], ['0', '1', '2'], ['1', '2', '8'], ['1', '3', '6'], ['1', '4', '4'], ['1', '5', '2'], ['1', '6', '8'], ['1', '7', '6'], ['1', '8', '4'], ['1', '9', '2'], ['2', '0', '0'], ['2', '0', '8'], ['2', '1', '6'], ['2', '2', '4'], ['2', '3', '2'], ['2', '2', '8'], ['2', '5', '6'], ['2', '6', '4'], ['2', '7', '2'], ['2', '8', '8'], ['2', '9', '6'], ['3', '0', '4'], ['3', '1', '2'], ['3', '2', '8'], ['3', '3', '6'], ['3', '4', '4'], ['3', '5', '2'], ['3', '6', '0'], ['3', '6', '8'], ['3', '7', '6'], ['3', '8', '4'], ['3', '9', '2'], ['4', '1', '6'], ['4', '2', '4'], ['4', '3', '2'], ['4', '4', '0'], ['4', '4', '8'], ['4', '9', '6'], ['5', '0', '4'], ['5', '1', '2'], ['5', '2', '0'], ['5', '2', '8'], ['5', '4', '4'], ['5', '5', '2'], ['5', '6', '8'], ['5', '7', '6'], ['5', '8', '4'], ['5', '9', '2'], ['6', '0', '0'], ['6', '0', '8'], ['6', '1', '6'], ['6', '2', '4'], ['6', '3', '2'], ['6', '4', '8'], ['6', '5', '6'], ['6', '6', '4'], ['6', '7', '2'], ['6', '8', '0'], ['6', '8', '8'], ['6', '9', '6'], ['7', '0', '4'], ['7', '1', '2'], ['7', '2', '8'], ['7', '3', '6'], ['7', '4', '4'], ['7', '5', '2'], ['7', '6', '0'], ['7', '6', '8'], ['7', '7', '6'], ['7', '8', '4'], ['7', '9', '2'], ['8', '1', '6'], ['8', '2', '4'], ['8', '3', '2'], ['8', '5', '6'], ['8', '6', '4'], ['8', '7', '2'], ['8', '8', '8'], ['8', '9', '6'], ['9', '0', '4'], ['9', '1', '2'], ['9', '2', '0'], ['9', '2', '8'], ['9', '3', '6'], ['9', '4', '4'], ['9', '5', '2'], ['9', '7', '2'], ['9', '8', '0'], ['9', '8', '8'], ['9', '9', '6']]
yes = 'YES'
no = 'NO'

def check(number):
	if len(number) >= 3:
		for option in endings:
			temp = number
			valid = True
			for x in option:
				valid = valid and x in temp
				if not valid:
					break
				temp = temp.replace(x, '', 1)
			if valid:
				return yes
		return no
	else:
		if permute(number):
			return yes
		return no

def permute(head, tail=''):
	if len(head) == 0:
		if int(tail) % 8 == 0:
			return True
	else:
		for i in range(len(head)):
			if permute(head[0:i] + head[i + 1:], head[i] + tail):
				return True
		return False
x = int(input().strip())
for y in range(x):
	print(check(input().strip()))


from itertools import permutations, combinations

def check_permutations(base):
	for p_var in permutations(base):
		if not int(''.join(p_var)) % 8:
			return True

def has_permutation8(number):
	if number < 100:
		if check_permutations(str(number)):
			return 'YES'
	for var in combinations(str(number), 3):
		if check_permutations(var):
			return 'YES'
	return 'NO'
N = int(input())
for i in range(N):
	print(has_permutation8(int(input())))


from timeit import itertools
n = int(input())
result = []
for i in range(0, n):
	num = input()
	if len(num) > 3:
		comb = [''.join(map(str, comb)) for comb in itertools.combinations(num, 3)]
	else:
		comb = [num]
	for j in range(0, len(comb)):
		permutations = lambda s: (''.join(j) for j in itertools.permutations(s))
		b = list(permutations(comb[j]))
		find = False
		for k in range(0, len(b)):
			if int(b[k]) % 8 == 0:
				result.append('YES')
				find = True
				break
		if find:
			break
		elif j == len(comb) - 1:
			result.append('NO')
for x in range(0, len(result)):
	print(result[x])


from itertools import repeat, permutations
suffixes = set(('{:0>3}'.format(x) for x in range(0, 999, 8)))
T = int(input())
for test in range(T):
	chdigits = input()
	length = len(chdigits)
	digitlist = []
	numbers = set((str(x) for x in range(0, 100, 8)))
	numbers.add('08')
	numbers.add('00')
	d = {}
	for chdigit in chdigits:
		digit = int(chdigit)
		if digit in d:
			d[digit] = min(d[digit] + 1, 3)
		else:
			d[digit] = 1
	digitlist = sum((list(repeat(k, v)) for (k, v) in d.items()), digitlist)
	if len(digitlist) <= 2:
		perms = set((t for t in permutations(digitlist, len(digitlist))))
		for p in perms:
			number = ''.join((str(d) for d in p))
			if number in numbers:
				print('YES')
				break
		else:
			print('NO')
	else:
		tuples = set((t for t in permutations(digitlist, 3)))
		for t in tuples:
			suffix = ''.join((str(d) for d in t))
			if suffix in suffixes:
				print('YES')
				break
		else:
			print('NO')


import collections
import itertools
import sys
import unittest

def div_by_eight(s):
	return divmod(int(s), 8)[1] == 0

def solve(s):
	d = collections.defaultdict(int)
	for c in s:
		d[c] = min(d[c] + 1, 3)
	available_characters = []
	for (k, v) in d.items():
		available_characters.extend([k] * v)
	for perm in itertools.permutations(available_characters, min(len(s), 3)):
		if div_by_eight(''.join(perm)):
			return True
	return False

def out(b):
	if b == True:
		return 'YES'
	else:
		return 'NO'

class TestDivByEight(unittest.TestCase):

	def test_solve(self):
		self.assertTrue(solve('61'))
		self.assertFalse(solve('75'))
N = int(sys.stdin.readline().strip())
for z in range(N):
	print(out(solve(sys.stdin.readline().strip())))


sol = [[(0, 3)], [(8, 3)], [(0, 1), (4, 2)], [(0, 1), (8, 2)], [(0, 1), (8, 2)], [(0, 1), (8, 2)], [(0, 2), (2, 1)], [(0, 2), (4, 1)], [(0, 2), (4, 1)], [(0, 2), (6, 1)], [(0, 2), (8, 1)], [(0, 2), (8, 1)], [(0, 2), (8, 1)], [(1, 1), (4, 2)], [(1, 1), (6, 2)], [(1, 2), (2, 1)], [(2, 1), (4, 2)], [(2, 1), (5, 2)], [(2, 1), (8, 2)], [(2, 1), (9, 2)], [(2, 2), (3, 1)], [(2, 2), (4, 1)], [(2, 2), (7, 1)], [(3, 1), (4, 2)], [(3, 2), (6, 1)], [(4, 1), (6, 2)], [(4, 1), (8, 2)], [(4, 1), (8, 2)], [(4, 2), (5, 1)], [(4, 2), (6, 1)], [(4, 2), (7, 1)], [(4, 2), (8, 1)], [(4, 2), (9, 1)], [(5, 1), (6, 2)], [(6, 1), (7, 2)], [(6, 1), (8, 2)], [(6, 2), (9, 1)], [(0, 1), (1, 1), (2, 1)], [(0, 1), (1, 1), (4, 1)], [(0, 1), (1, 1), (6, 1)], [(0, 1), (1, 1), (6, 1)], [(0, 1), (2, 1), (3, 1)], [(0, 1), (2, 1), (3, 1)], [(0, 1), (2, 1), (4, 1)], [(0, 1), (2, 1), (4, 1)], [(0, 1), (2, 1), (5, 1)], [(0, 1), (2, 1), (7, 1)], [(0, 1), (2, 1), (7, 1)], [(0, 1), (2, 1), (8, 1)], [(0, 1), (2, 1), (8, 1)], [(0, 1), (2, 1), (9, 1)], [(0, 1), (3, 1), (4, 1)], [(0, 1), (3, 1), (6, 1)], [(0, 1), (4, 1), (5, 1)], [(0, 1), (4, 1), (6, 1)], [(0, 1), (4, 1), (6, 1)], [(0, 1), (4, 1), (7, 1)], [(0, 1), (4, 1), (8, 1)], [(0, 1), (4, 1), (8, 1)], [(0, 1), (4, 1), (8, 1)], [(0, 1), (4, 1), (8, 1)], [(0, 1), (4, 1), (9, 1)], [(0, 1), (5, 1), (6, 1)], [(0, 1), (5, 1), (6, 1)], [(0, 1), (6, 1), (7, 1)], [(0, 1), (6, 1), (8, 1)], [(0, 1), (6, 1), (8, 1)], [(0, 1), (6, 1), (9, 1)], [(0, 1), (6, 1), (9, 1)], [(1, 1), (2, 1), (3, 1)], [(1, 1), (2, 1), (5, 1)], [(1, 1), (2, 1), (5, 1)], [(1, 1), (2, 1), (6, 1)], [(1, 1), (2, 1), (7, 1)], [(1, 1), (2, 1), (8, 1)], [(1, 1), (2, 1), (9, 1)], [(1, 1), (2, 1), (9, 1)], [(1, 1), (3, 1), (6, 1)], [(1, 1), (4, 1), (6, 1)], [(1, 1), (4, 1), (8, 1)], [(1, 1), (6, 1), (7, 1)], [(1, 1), (6, 1), (8, 1)], [(1, 1), (6, 1), (8, 1)], [(2, 1), (3, 1), (4, 1)], [(2, 1), (3, 1), (5, 1)], [(2, 1), (3, 1), (6, 1)], [(2, 1), (3, 1), (8, 1)], [(2, 1), (3, 1), (8, 1)], [(2, 1), (3, 1), (9, 1)], [(2, 1), (4, 1), (6, 1)], [(2, 1), (4, 1), (6, 1)], [(2, 1), (4, 1), (7, 1)], [(2, 1), (4, 1), (8, 1)], [(2, 1), (4, 1), (8, 1)], [(2, 1), (5, 1), (6, 1)], [(2, 1), (5, 1), (7, 1)], [(2, 1), (5, 1), (8, 1)], [(2, 1), (5, 1), (9, 1)], [(2, 1), (5, 1), (9, 1)], [(2, 1), (6, 1), (7, 1)], [(2, 1), (6, 1), (9, 1)], [(2, 1), (7, 1), (8, 1)], [(2, 1), (7, 1), (8, 1)], [(2, 1), (7, 1), (9, 1)], [(2, 1), (8, 1), (9, 1)], [(3, 1), (4, 1), (8, 1)], [(3, 1), (5, 1), (6, 1)], [(3, 1), (6, 1), (7, 1)], [(3, 1), (6, 1), (7, 1)], [(3, 1), (6, 1), (8, 1)], [(3, 1), (6, 1), (9, 1)], [(4, 1), (5, 1), (6, 1)], [(4, 1), (5, 1), (8, 1)], [(4, 1), (6, 1), (8, 1)], [(4, 1), (6, 1), (8, 1)], [(4, 1), (6, 1), (9, 1)], [(4, 1), (7, 1), (8, 1)], [(4, 1), (8, 1), (9, 1)], [(5, 1), (6, 1), (7, 1)], [(5, 1), (6, 1), (8, 1)], [(5, 1), (6, 1), (8, 1)], [(6, 1), (7, 1), (8, 1)], [(6, 1), (7, 1), (9, 1)], [(6, 1), (8, 1), (9, 1)], [(6, 1), (8, 1), (9, 1)]]

def test_other(a):
	arr = [0] * 10
	for e in a:
		arr[int(e)] += 1
	for elem in sol:
		if test_elem(elem, arr):
			return True
	return False

def test_elem(elem, arr):
	for (nr, count) in elem:
		if arr[nr] < count:
			return False
	return True

def test_1(elem):
	if elem == '8' or elem == '0':
		return True
	else:
		return False

def test_2(elem):
	if int(elem) % 8 == 0 or int(elem[::-1]) % 8 == 0:
		return True
	else:
		return False
for n in range(int(input())):
	elem = input()
	d = {1: test_1, 2: test_2}
	if d.get(len(elem), test_other)(elem):
		print('YES')
	else:
		print('NO')


two = ['08']
three = ['008']

def isin(wh, s):
	targ = list(s)
	cur = list(wh)
	for l in s:
		if l in cur:
			targ.remove(l)
			cur.remove(l)
		else:
			return False
	return True

def isinall(s, gr):
	for t in gr:
		if isin(s, t):
			return 'YES'
	return 'NO'
for n in range(2, 13):
	a = str(8 * n)
	two.append(a)
	three.append('0' + a)
for n in range(13, 125):
	three.append(str(8 * n))
for testcase in range(int(input())):
	a = input()
	if len(a) == 1:
		if a in '08':
			print('YES')
		else:
			print('NO')
	elif len(a) == 2:
		print(isinall(a, two))
	else:
		print(isinall(a, three))


for t in range(int(input())):
	inp = input()
	l = len(inp)
	if inp == '0' or inp == '8':
		print('YES')
	elif l == 2:
		if int(inp) % 8 == 0 or int(inp[::-1]) % 8 == 0:
			print('YES')
		else:
			print('NO')
	else:
		i = 0
		done = False
		while i < l and done == False:
			j = 0
			while j < l and done == False:
				k = 0
				while k < l and done == False:
					if i != j and j != k and (i != k) and (int(inp[i] + inp[j] + inp[k]) % 8 == 0):
						print('YES')
						done = True
						break
					k += 1
				j += 1
			i += 1
		if done == False:
			print('NO')


from itertools import permutations
from random import randint

def f(N):
	if len(N) <= 2:
		return int(N) % 8 == 0 or int(N[::-1]) % 8 == 0
	for i in range(len(N)):
		for j in range(i + 1, len(N)):
			for k in range(j + 1, len(N)):
				li = [N[i], N[j], N[k]]
				for e in permutations(li):
					if int(''.join(e)) % 8 == 0:
						return True
	return False

def h():
	for _ in range(int(input())):
		print('YES' if f(input()) else 'NO')
h()


def is_Div(num):
	l = len(num)
	flag = False
	if l == 1:
		flag = int(num) % 8 == 0
	elif l == 2:
		flag = int(num) % 8 == 0 or int(num[1] + num[0]) % 8 == 0
	else:
		for i in range(l):
			if flag:
				break
			for j in range(i + 1, l):
				if flag:
					break
				for k in range(j + 1, l):
					if flag:
						break
					x = num[i]
					y = num[j]
					z = num[k]
					nList = [int(x + y + z), int(x + z + y), int(y + x + z), int(y + z + x), int(z + x + y), int(z + y + x)]
					for nu in nList:
						if nu % 8 == 0:
							flag = True
							break
	if flag:
		print('YES')
	else:
		print('NO')
t = int(input())
for i in range(t):
	num = input()
	is_Div(num)


digits = set((tuple(sorted('%d' % i)) for i in range(0, 1000, 8)))
Digits = []
for i in range(0, 1000, 8):
	Digits.append({})
	for d in '%03d' % i:
		Digits[-1][d] = Digits[-1].get(d, 0) + 1

def solve(N):
	if len(N) < 3:
		return tuple(sorted(N)) in digits
	else:
		Nds = {}
		for d in N:
			Nds[d] = Nds.get(d, 0) + 1
		for ds in Digits:
			valid = True
			for d in ds:
				if Nds.get(d, 0) < ds[d]:
					valid = False
					break
			if valid:
				return True
		return False
T = int(input())
for _ in range(T):
	if solve(input()):
		print('YES')
	else:
		print('NO')


def parse_sybmol_multiset(s):
	ms = {}
	for c in s:
		if c in ms:
			ms[c] += 1
		else:
			ms[c] = 1
	return ms

def is_submultiset(submultiset, multiset):
	for symbol in submultiset:
		if symbol in multiset and multiset[symbol] >= submultiset[symbol]:
			pass
		else:
			return False
	return True
divisible_by_8 = [[], [], []]
for i in range(0, 10, 8):
	s = str(i)
	divisible_by_8[0].append(parse_sybmol_multiset(s))
for i in range(0, 100, 8):
	s = str(i)
	while len(s) != 2:
		s = '0' + s
	divisible_by_8[1].append(parse_sybmol_multiset(s))
for i in range(0, 1000, 8):
	s = str(i)
	while len(s) != 3:
		s = '0' + s
	divisible_by_8[2].append(parse_sybmol_multiset(s))
for t in range(int(input())):
	ms = parse_sybmol_multiset(input())
	ind = min(len(ms), 3) - 1
	for div_8 in divisible_by_8[ind]:
		if is_submultiset(div_8, ms):
			print('YES')
			break
	else:
		print('NO')


import itertools

def count_input(x):
	c = [0 for _ in range(10)]
	for i in range(len(x)):
		if c[x[i]] < 3:
			c[x[i]] += 1
		else:
			None
	return c

def even_check(c):
	if c[0] == c[2] == c[4] == c[6] == c[8] == 0:
		return False
	else:
		return True

def div_check(a):
	s = [8 * i for i in range(0, 124)]
	perms = set(itertools.permutations(a, min(3, len(a))))
	for perm in perms:
		x = int(''.join((str(i) for i in perm)))
		if x % 8 == 0:
			return 'YES'
	return 'NO'
for _ in range(int(input())):
	a = list(map(int, input()))
	print(div_check(a))


def get2combinations(list_n):
	s = []
	N = len(list_n)
	for i in range(N):
		for j in range(N):
			if i != j:
				s.append(int(list_n[i] + list_n[j]))
	return s

def get3combinations(list_n):
	s = []
	N = len(list_n)
	for i in range(N):
		for j in range(N):
			for k in range(N):
				if i != j and i != k and (j != k):
					s.append(int(list_n[i] + list_n[j] + list_n[k]))
	return s
T = int(input())
for _ in range(T):
	num = list(input())
	found = False
	combinations = []
	if len(num) == 2:
		combinations = get2combinations(num)
	elif len(num) >= 3:
		combinations = get3combinations(num)
	else:
		combinations.append(int(num[0]))
	for i in combinations:
		if i % 8 == 0:
			print('YES')
			found = True
			break
	if not found:
		print('NO')


T = int(input())
for num in range(T):
	value = input()
	flag = False
	if len(value) == 1:
		if int(value) % 8 == 0:
			flag = True
		else:
			flag = False
	elif len(value) == 2:
		if int(value) % 8 == 0:
			flag = True
		elif int(value[1] + value[0]) % 8 == 0:
			flag = True
		else:
			flag = False
	else:
		for i in range(len(value)):
			for j in range(len(value)):
				for k in range(len(value)):
					if i != j and j != k and (k != i):
						number = int(value[i] + value[j] + value[k])
						if number % 8 == 0:
							flag = True
							break
				if flag == True:
					break
			if flag == True:
				break
	if flag == False:
		print('NO')
	if flag == True:
		print('YES')


import itertools, sys
T = int(sys.stdin.readline())
for _ in range(T):
	N = sys.stdin.readline().strip()
	if len(N) == 1:
		print('YES' if N == '8' or N == '0' else 'NO')
	elif len(N) == 2:
		print('YES' if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0 else 'NO')
	else:
		for s in itertools.permutations(N, 3):
			if int(''.join(s)) % 8 == 0:
				print('YES')
				break
		else:
			print('NO')


from itertools import permutations as perms

def f(N):
	for s in perms(N, 3):
		if int(''.join(s)) % 8 == 0:
			return 'YES'
	return 'NO'
for T in range(int(input())):
	N = input()
	if len(N) == 1:
		print('YES' if N == '8' or N == '0' else 'NO')
	elif len(N) == 2:
		print('YES' if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0 else 'NO')
	else:
		print(f(N))


for x in range(int(input())):
	a = [0] * 10
	n = input()
	flag = 0
	m = int(n)
	if m < 100:
		for x in n:
			a[ord(x) - 48] += 1
			if m == 0 or m == 8:
				flag = 1
				break
			if a[2] > 0:
				if a[4] > 0 or a[3] > 0 or a[7] > 0:
					flag = 1
					break
			if a[0] > 0:
				if a[4] > 0 or a[8] > 0:
					flag = 1
					break
			if a[6] > 0:
				if a[4] > 0 or a[9] > 0 or a[5] > 0 or (a[1] > 0):
					flag = 1
					break
			if a[8] > 0:
				if a[8] > 1 or a[0] > 0 or a[4] > 0:
					flag = 1
					break
	else:
		for x in n:
			a[ord(x) - 48] += 1
		c = 1000
		while c < 2000:
			d = str(c)
			b = [0] * 10
			b[ord(d[1]) - 48] += 1
			b[ord(d[2]) - 48] += 1
			b[ord(d[3]) - 48] += 1
			count = 0
			for x in range(10):
				if a[x] >= b[x]:
					count += 1
			if count == 10:
				flag = 1
				break
			c += 8
	if flag == 1:
		print('YES')
	else:
		print('NO')


t = int(input())
while t:
	t -= 1
	num = input()
	length = len(num)
	if length == 1:
		if int(num) % 8 == 0:
			print('YES')
		else:
			print('NO')
	elif length == 2:
		if int(num) % 8 == 0:
			print('YES')
		elif (int(num[1]) * 10 + int(num[0])) % 8 == 0:
			print('YES')
		else:
			print('NO')
	else:
		flag = 0
		for i in range(length):
			for j in range(i + 1, length):
				for k in range(j + 1, length):
					a = int(num[i])
					b = int(num[j])
					c = int(num[k])
					if (a * 100 + b * 10 + c) % 8 == 0:
						print('YES')
						flag = 1
						break
					elif (a * 100 + c * 10 + b) % 8 == 0:
						print('YES')
						flag = 1
						break
					elif (c * 100 + a * 10 + b) % 8 == 0:
						print('YES')
						flag = 1
						break
					elif (c * 100 + b * 10 + a) % 8 == 0:
						print('YES')
						flag = 1
						break
					elif (b * 100 + c * 10 + a) % 8 == 0:
						print('YES')
						flag = 1
						break
					elif (b * 100 + a * 10 + c) % 8 == 0:
						print('YES')
						flag = 1
						break
					else:
						flag = 0
				if flag:
					break
			if flag:
				break
		if flag == 0:
			print('NO')


from collections import Counter
numcases = int(input())
divisibles = [x for x in range(100, 1000) if x % 8 == 0]
lookup = []
for num in divisibles:
	cntr = Counter()
	while num:
		cntr[num % 10] += 1
		num = num // 10
	lookup.append(cntr)
for _ in range(numcases):
	digits = [int(x) for x in list(input())]
	if len(digits) < 3:
		sumDigits = sum(digits)
		flag = False
		for i in digits:
			rest = 2 * (sumDigits - i)
			if (i + rest) % 8 == 0:
				print('YES')
				flag = True
				break
		if not flag:
			print('NO')
	else:
		digitCount = Counter()
		for i in digits:
			digitCount[i] += 1
		flag = False
		for entry in lookup:
			solution = True
			for key in entry.keys():
				solution = solution and entry[key] <= digitCount[key]
			if solution:
				print('YES')
				flag = True
				break
		if not flag:
			print('NO')


from itertools import permutations
t = int(input())
res = []
for i in range(t):
	n = int(input())
	if n == 0:
		res.append('YES')
		continue
	l = []
	while n > 0:
		l.append(str(n % 10))
		n = n // 10
	flag = 0
	for j in permutations(l):
		if int(''.join(j)) % 8 == 0:
			flag = 1
			break
	if flag == 1:
		res.append('YES')
	else:
		res.append('NO')
for i in range(t):
	print(res[i])


import itertools
for _ in range(int(input())):
	N = int(input())
	found = 0
	if len(str(N)) < 3:
		if N % 8 == 0 or int(str(N)[::-1]) % 8 == 0:
			print('YES')
			continue
		else:
			print('NO')
			continue
	combo = itertools.permutations(str(N), 3)
	for item in combo:
		if int(''.join(item)) % 8 == 0:
			print('YES')
			found = 1
			break
	if not found:
		print('NO')


t = int(input())
for T in range(t):
	a = str(input())
	l = len(a)
	f = 0
	if l == 1:
		if a == '0' or a == '8':
			f += 1
	if l == 2:
		if (int(a[0]) * 10 + int(a[1])) % 8 == 0 or (int(a[1]) * 10 + int(a[0])) % 8 == 0:
			f += 1
	if l > 2:
		for i in range(l):
			for j in range(l):
				for k in range(l):
					if i != j and i != k and (j != k) and ((100 * int(a[i]) + 10 * int(a[j]) + int(a[k])) % 8 == 0):
						f += 1
	if f == 0:
		print('NO')
	else:
		print('YES')


T = int(input())
for i in range(T):
	n = list(str(input()))
	if len(n) == 1:
		if n[0] == '0' or n[0] == '8':
			print('YES')
		else:
			print('NO')
	elif len(n) == 2:
		c1 = int(''.join(n))
		c2 = int(''.join(reversed(n)))
		if c1 % 8 == 0 or c2 % 8 == 0:
			print('YES')
		else:
			print('NO')
	else:
		nn = sorted(n)
		n.clear()
		n = nn
		d = False
		for i in range(len(nn) - 2):
			if i < len(nn) - 3:
				if n[i] == n[i + 3]:
					continue
			for j in range(i + 1, len(nn) - 1):
				for k in range(j + 1, len(nn)):
					if k < len(nn) - 1:
						if n[i] == n[k] and n[k + 1] == n[k]:
							break
					c1 = int(''.join(n[i] + n[j] + n[k]))
					if c1 == 0:
						break
					c2 = int(''.join(n[i] + n[k] + n[j]))
					c3 = int(''.join(n[j] + n[i] + n[k]))
					c4 = int(''.join(n[j] + n[k] + n[i]))
					c5 = int(''.join(n[k] + n[j] + n[i]))
					c6 = int(''.join(n[k] + n[i] + n[j]))
					if c1 % 8 == 0 or c2 % 8 == 0 or c3 % 8 == 0 or (c4 % 8 == 0) or (c5 % 8 == 0) or (c6 % 8 == 0):
						d = True
				if d:
					break
			if d:
				break
		if d:
			print('YES')
		if not d:
			print('NO')


def to_sorted_str(i):
	return ''.join(sorted(str(i)))
multiples = set()
for i in range(0, 1000, 8):
	multiples.add(to_sorted_str(i))

def manasa_loves_maths(N):
	l = len(N)
	if l <= 3:
		tmp = to_sorted_str(N)
		return 'YES' if tmp in multiples else 'NO'
	N = sorted(N)
	for i in range(l):
		for j in range(i + 1, l):
			for k in range(j + 1, l):
				tmp = N[i] + N[j] + N[k]
				if tmp in multiples:
					return 'YES'
	return 'NO'
T = int(input())
for _ in range(T):
	N = input()
	print(manasa_loves_maths(N))


for _ in range(int(input())):
	n = input().strip()
	size = len(n)
	if size == 1:
		if n == '0' or n == '8':
			print('YES')
		else:
			print('NO')
		continue
	elif size == 2:
		if int(n) % 8 == 0 or int(''.join(reversed(n))) % 8 == 0:
			print('YES')
		else:
			print('NO')
		continue
	for i in range(size):
		for j in range(size):
			if i == j:
				continue
			for k in range(size):
				if k == i or k == j:
					continue
				num = int(n[i] + n[j] + n[k])
				if num % 8 == 0:
					print('YES')
					break
			else:
				continue
			break
		else:
			continue
		break
	else:
		print('NO')


import itertools, sys
T = int(sys.stdin.readline())
for _ in range(T):
	N = sys.stdin.readline().strip()
	if len(N) == 1:
		print('YES' if N == '8' or N == '0' else 'NO')
	elif len(N) == 2:
		print('YES' if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0 else 'NO')
	else:
		for s in itertools.permutations(N, 3):
			if int(''.join(s)) % 8 == 0:
				print('YES')
				break
		else:
			print('NO')


import itertools
T = int(input())
cases = []
for i in range(T):
	N = input()
	if len(N) > 3:
		for p in itertools.permutations(N, 3):
			if (int(p[0] + '00') + int(p[1] + '0') + int(p[2])) % 8 == 0:
				cases.append(True)
				break
		else:
			cases.append(False)
	else:
		for p in itertools.permutations(N):
			sum = 0
			for i in range(len(p)):
				sum += int(p[i] + '0' * i)
			if int(sum) % 8 == 0:
				cases.append(True)
				break
		else:
			cases.append(False)
for case in cases:
	if case:
		print('YES')
	else:
		print('NO')


import itertools, sys
T = int(sys.stdin.readline())
for _ in range(T):
	N = sys.stdin.readline().strip()
	if len(N) == 1:
		print('YES' if N == '8' or N == '0' else 'NO')
	elif len(N) == 2:
		print('YES' if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0 else 'NO')
	else:
		for s in itertools.permutations(N, 3):
			if int(''.join(s)) % 8 == 0:
				print('YES')
				break
		else:
			print('NO')


t = int(input())
for i in range(t):
	lis = list(input())
	lis = list(map(int, lis))
	if len(lis) == 1:
		if lis[0] % 8 == 0:
			print('YES')
		else:
			print('NO')
	elif len(lis) == 2:
		if (lis[0] * 10 + lis[1]) % 8 == 0 or (lis[1] * 10 + lis[0]) % 8 == 0:
			print('YES')
		else:
			print('NO')
	else:
		found = False
		for a in range(len(lis)):
			if found:
				break
			for b in range(len(lis)):
				if found:
					break
				if a == b:
					continue
				for c in range(len(lis)):
					if c == a or c == b:
						continue
					if (lis[a] * 100 + lis[b] * 10 + lis[c]) % 8 == 0:
						found = True
						break
		if found:
			print('YES')
		else:
			print('NO')


import itertools, sys
T = int(sys.stdin.readline())
for _ in range(T):
	N = sys.stdin.readline().strip()
	if len(N) == 1:
		print('YES' if N == '8' or N == '0' else 'NO')
	elif len(N) == 2:
		print('YES' if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0 else 'NO')
	else:
		for s in itertools.permutations(N, 3):
			if int(''.join(s)) % 8 == 0:
				print('YES')
				break
		else:
			print('NO')


def lovesmath2(string):
	if len(string) == 1:
		for i in range(0, len(string)):
			if int(string[i]) % 8 == 0:
				return 'YES'
			possible = False
	elif len(string) == 2:
		for i in range(0, len(string)):
			for j in range(0, len(string)):
				if i != j and int(string[i] + string[j]) % 8 == 0:
					return 'YES'
				possible = False
	else:
		for i in range(0, len(string)):
			for j in range(0, len(string)):
				for k in range(0, len(string)):
					if (i != j and j != k and (i != k)) and int((string[i] + string[j] + string[k]).lstrip('0').strip('')) % 8 == 0:
						return 'YES'
					possible = False
	if not possible:
		return 'NO'

def lovesmath(string):
	print(lovesmath2(string))
T = int(input())
for i in range(0, T):
	string = str(input())
	lovesmath(string)


import itertools, sys
T = int(sys.stdin.readline())
for _ in range(T):
	N = sys.stdin.readline().strip()
	if len(N) == 1:
		print('YES' if N == '8' or N == '0' else 'NO')
	elif len(N) == 2:
		print('YES' if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0 else 'NO')
	else:
		for s in itertools.permutations(N, 3):
			if int(''.join(s)) % 8 == 0:
				print('YES')
				break
		else:
			print('NO')


def find(l):
	n = []
	for i in range(10):
		n.append(l.count(i))
	if len(l) == 1:
		if l[0] % 8 == 0:
			print('YES')
			return
		else:
			print('NO')
			return
	for i in range(10):
		(a, b) = (-1, -1)
		num = 0
		if n[i] > 0:
			num = i * 100
			a = i
			n[i] = n[i] - 1
		else:
			continue
		for j in range(10):
			if n[j] > 0:
				num = num + j * 10
				b = j
				n[j] = n[j] - 1
				if len(l) == 2:
					if num / 10 % 8 == 0:
						print('YES')
						return
					else:
						n[b] += 1
						continue
			else:
				continue
			for k in range(10):
				if n[k] > 0:
					num = num + k
					if num % 8 == 0:
						print('YES')
						return
					else:
						num = num - k
				else:
					continue
			num = i * 100
			n[b] += 1
		n[a] += 1
	print('NO')

def main():
	for i in range(int(input())):
		l = list(map(int, list(input())))
		find(l)
main()


def toVect(x, z):
	sig = [0 for _ in range(10)]
	for c in x:
		sig[int(c)] += 1
	sig[0] += max(0, z - len(x))
	return sig

def belongs(a, b, c):
	ok = 0
	for i in range(10):
		if b[i] >= a[i]:
			ok += a[i]
	return ok == c
ko3 = list(map(lambda x: toVect(x, 3), set(map(str, range(0, 1000, 8)))))
ko2 = list(map(lambda x: toVect(x, 2), set(map(str, range(16, 100, 8)))))
t = int(input())
for _ in range(t):
	x = input()
	if len(x) == 1:
		if int(x) % 8 == 0:
			print('YES')
		else:
			print('NO')
		continue
	if len(x) == 2:
		ko = ko2
		c = 2
	else:
		ko = ko3
		c = 3
	sig = toVect(x, c)
	found = False
	for ok in ko:
		if belongs(ok, sig, c):
			found = True
			break
	if found:
		print('YES')
	else:
		print('NO')


import itertools

def div_by_eight(s):
	length = len(s)
	if length <= 3:
		return any((int(''.join(l)) % 8 == 0 for l in itertools.permutations(s)))
	for k in itertools.combinations(s, 3):
		if any((int(''.join(l)) % 8 == 0 for l in itertools.permutations(k))):
			return True
	return False
n = int(input())
for _ in range(n):
	k = input()
	if div_by_eight(k):
		print('YES')
	else:
		print('NO')


def countDigits(n):
	ar = [0] * 10
	for c in str(n):
		ar[int(c)] += 1
	return ar

def multiplesOf8():
	ar = [n * 8 for n in range(100 // 8 + 1, 1000 // 8)]
	return ar

def isPermutationMultipleOf8(n, multiples):
	digitsOfn = countDigits(n)
	if len(str(n)) < 3:
		if n % 8 == 0 or int(str(n)[::-1]) % 8 == 0:
			return 'YES'
		else:
			return 'NO'
	for m in multiples:
		digitsOfm = countDigits(m)
		isMultiple = True
		for i in range(10):
			if digitsOfm[i] > digitsOfn[i]:
				isMultiple = False
		if isMultiple:
			return 'YES'
	return 'NO'

def main():
	t = int(input())
	multiples8 = multiplesOf8()
	for _ in range(t):
		n = int(input())
		print(isPermutationMultipleOf8(n, multiples8))
main()


for _ in range(int(input())):
	string = input()
	limit = 10 ** min(len(string), 3)
	while len(string) < 3:
		string += '0'
	numDigits = [0] * 10
	for c in string:
		numDigits[int(c)] += 1
	found = False
	for multiple in range(0, limit, 8):
		necessary = [0] * 10
		tmp = multiple
		for i in range(3):
			necessary[tmp % 10] += 1
			tmp //= 10
		works = True
		for i in range(10):
			works = works and numDigits[i] >= necessary[i]
			if not works:
				break
		if works:
			print('YES')
			found = True
			break
	if not found:
		print('NO')


T = int(input())
while T > 0:
	N = int(input())
	M = N
	if N == 8:
		print('YES')
	elif N < 100:
		a = N % 10
		b = int(N / 10)
		if (10 * a + b) % 8 == 0 or (10 * b + a) % 8 == 0:
			print('YES')
		else:
			print('NO')
	else:
		temp_array = list()
		while int(N / 10) != 0:
			temp_array.append(N % 10)
			N = int(N / 10)
		temp_array.append(N)
		var = 0
		N = M
		for i in range(len(temp_array)):
			for j in range(len(temp_array)):
				for k in range(len(temp_array)):
					if k != j and k != i and (i != j):
						temp_var = 100 * temp_array[i] + 10 * temp_array[j] + temp_array[k]
						if temp_var % 8 == 0:
							print('YES')
							var = 1
							break
				if var == 1:
					break
			if var == 1:
				break
		if var == 0:
			print('NO')
	T = T - 1


import itertools

def perms(num):
	if len(num) == 1:
		return [num]
	if len(num) == 2:
		return [num[0] + num[1], num[1] + num[0]]
	ress = []
	res = ''
	for i in range(len(num)):
		for j in range(len(num)):
			for k in range(len(num)):
				if i != j and i != k and (j != k):
					ress.append(res + num[i] + num[j] + num[k])
			res = ''
	return ress
length = int(input())
nums = []
for i in range(length):
	nums.append(input())
found = False
for num in nums:
	for obj in perms(num):
		obj = int(''.join(obj))
		if obj % 8 == 0:
			found = True
			break
	print('YES' if found else 'NO')
	found = False


def three_digits(number):
	result = []
	for _ in range(3):
		result.append(number % 10)
		number //= 10
	return result
multiples_list = [three_digits(8 * i) for i in range(125)]

def find_match(number):
	digits = [int(ch) for ch in number]
	for multiple in multiples_list:
		digits_copy = list(digits)
		matches = 0
		for digit in multiple:
			if digit in digits_copy:
				digits_copy.remove(digit)
				matches += 1
				if matches == 3:
					return True
	return False
num_cases = int(input())
for _ in range(num_cases):
	number = input()
	if len(number) == 1:
		print('YES' if number == '8' or number == '0' else 'NO')
	elif len(number) == 2:
		print('YES' if int(number) % 8 == 0 or int(number[::-1]) % 8 == 0 else 'NO')
	else:
		print('YES' if find_match(number) else 'NO')


import itertools, sys
T = int(sys.stdin.readline())
for _ in range(T):
	N = sys.stdin.readline().strip()
	if len(N) == 1:
		print('YES' if N == '8' or N == '0' else 'NO')
	elif len(N) == 2:
		print('YES' if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0 else 'NO')
	else:
		for s in itertools.permutations(N, 3):
			if int(''.join(s)) % 8 == 0:
				print('YES')
				break
		else:
			print('NO')


import itertools

def funcCalculatedivisibles(x):
	var = len(x) if len(x) <= 3 else 3
	for e in itertools.permutations(x, var):
		num = int(''.join(e))
		if num % 8 == 0:
			return True
	return False

def funcInputAndCheck():
	numberOfInput = int(input())
	counter = 1
	while counter <= numberOfInput:
		num = input()
		if funcCalculatedivisibles(num):
			print('YES')
		else:
			print('NO')
		counter = counter + 1
funcInputAndCheck()


N = int(input().strip())
arr = [input().strip() for _ in range(N)]
from itertools import permutations

def main(arr):
	for n in arr:
		check_num(n)

def check_num(anum):
	last_three = anum[-3:]
	for p in permutations(anum, min(3, len(anum))):
		if int(''.join(p)) % 8 == 0:
			print('YES')
			return True
	print('NO')
	return False
main(arr)


epatterns = ['00', '08', '16', '24', '32', '40', '48', '56', '64', '72', '80', '88', '96']
opatterns = ['04', '12', '20', '28', '36', '44', '52', '60', '68', '76', '84', '92']

def pmatch(pattern, S):
	positions = []
	for c in pattern:
		i = 0
		p = S.find(c, i)
		while p in positions:
			i = p + 1
			p = S.find(c, i)
		if p == -1:
			return (False, '')
		else:
			positions.append(p)
	newstr = ''
	for i in range(len(S)):
		if i not in positions:
			newstr += S[i]
	return (True, newstr)

def check(S):
	if S == '8' or S == '0':
		return True
	for pattern in epatterns:
		(contains, rest) = pmatch(pattern, S)
		if contains and rest == '' or any([rest.find(c) != -1 for c in ['0', '2', '4', '6', '8']]):
			return True
	for pattern in opatterns:
		(contains, rest) = pmatch(pattern, S)
		if contains and any([rest.find(c) != -1 for c in ['1', '3', '5', '7', '9']]):
			return True
	return False
for _ in range(int(input().strip())):
	answer = 'YES' if check(input().strip()) else 'NO'
	print(answer)


import itertools as it
T = int(input())

def proc(N):
	for perm in it.permutations(N, min(len(N), 3)):
		if int(''.join(perm)) % 8 == 0:
			return 'YES'
	return 'NO'
for i in range(T):
	N = input()
	print(proc(N))


def strIntersection1(s1, s2):
	out = ''
	s2_tmp = s2[0:]
	for c in s1:
		if c in s2_tmp:
			out += c
			ind1 = s2_tmp.find(c)
			s2_tmp = s2_tmp[:ind1] + s2_tmp[ind1 + 1:]
	return out
nTst = int(input())
inp = []
for i1 in range(nTst):
	inp.append(input())
div8 = []
for i1 in range(125):
	x1 = str(i1 * 8)
	len_x = len(x1)
	if len_x < 3:
		x1 = (3 - len_x) * '0' + x1
	div8.append(x1)
for i1 in range(nTst):
	nn = inp[i1]
	found = False
	len_nn = len(nn)
	if len_nn == 1:
		if (nn == '0') | (nn == '8'):
			found = True
	elif len_nn == 2:
		n1 = 2 * int(nn[0]) + int(nn[1])
		n2 = 2 * int(nn[1]) + int(nn[0])
		if (n1 % 8 == 0) | (n2 % 8 == 0):
			found = True
	else:
		for num in div8:
			intersect = strIntersection1(num, nn)
			if len(intersect) == 3:
				found = True
				break
	if found:
		print('YES')
	else:
		print('NO')


import itertools, sys
T = int(sys.stdin.readline())
for _ in range(T):
	N = sys.stdin.readline().strip()
	if len(N) == 1:
		print('YES' if N == '8' or N == '0' else 'NO')
	elif len(N) == 2:
		print('YES' if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0 else 'NO')
	else:
		for s in itertools.permutations(N, 3):
			if int(''.join(s)) % 8 == 0:
				print('YES')
				break
		else:
			print('NO')


def Check_Divisibility(N):
	ans1 = int(N)
	ans2 = int((ord(N[0]) - 48) * 100 + (ord(N[1]) - 48) + (ord(N[2]) - 48) * 10)
	ans3 = int(ord(N[0]) - 48 + (ord(N[1]) - 48) * 100 + (ord(N[2]) - 48) * 10)
	ans4 = int((ord(N[0]) - 48) * 10 + (ord(N[1]) - 48) * 100 + (ord(N[2]) - 48))
	ans5 = int((ord(N[0]) - 48) * 10 + (ord(N[1]) - 48) + (ord(N[2]) - 48) * 100)
	ans6 = int(ord(N[0]) - 48 + (ord(N[1]) - 48) * 10 + (ord(N[2]) - 48) * 100)
	if (ans1 % 8 == 0 or ans2 % 8 == 0) or (ans3 % 8 == 0 or ans3 % 8 == 0) or (ans5 % 8 == 0 or ans6 % 8 == 0):
		return int(1)
	return int(0)

def generate_combinations(ans, cur_ind, cur_len, max_len, s, siz):
	if cur_len == max_len:
		if max_len == 2:
			temp = ans
			x = int(0)
			while temp > 0:
				x = x * 10 + temp % 10
				temp //= 10
			return x % 8 == 0 or ans % 8 == 0
		return Check_Divisibility(str(ans))
	if cur_ind < siz:
		for i in range(cur_ind + 1, siz):
			ans = ans * 10 + (ord(s[i]) - 48)
			y = generate_combinations(ans, i, cur_len + 1, max_len, s, siz)
			if y == 1:
				return 1
			else:
				ans = ans // 10
	return 0

def solve():
	s = input()
	l = len(s)
	ans = 'NO'
	if l == 1:
		x = ord(s[0]) - 48
		if x % 8 == 0:
			ans = 'YES'
	if l == 2:
		y = generate_combinations(int(0), int(-1), int(0), int(2), s, l)
		if y == 1:
			ans = 'YES'
	y = generate_combinations(int(0), int(-1), int(0), int(3), s, l)
	if y == 1:
		ans = 'YES'
	print(ans)

def main():
	t = int(input())
	while t > 0:
		solve()
		t -= 1
main()


import sys, itertools
stop = False

def divisible_permutations(n):
	p = itertools.permutations(n)
	return any([int(''.join(x)) % 8 == 0 for x in p])

def comb(res, rest, k):
	global stop
	if stop:
		return
	if k == 0:
		p = itertools.permutations(res)
		if divisible_permutations(res):
			print('YES')
			stop = True
	else:
		for i in range(len(rest)):
			comb(res + rest[i], rest[i + 1:], k - 1)
n = int(input())
for _ in range(n):
	stop = False
	s = input()
	if len(s) <= 3:
		if divisible_permutations(s):
			print('YES')
		else:
			print('NO')
	else:
		comb('', s, 3)
		if not stop:
			print('NO')


def cmp_digs(digs_one, digs_two):
	return all([digs_one[i] <= digs_two[i] for i in range(len(digs_one))])

def count_digs(num_str):
	tmp = [0] * 10
	for c in num_str:
		tmp[ord(c) - ord('0')] += 1
	return tmp
needed_digits = []
for i in range(1000, 2000):
	if i % 8 == 0:
		needed_digits.append(count_digs(str(i)[-3:]))
t = int(input())
for _ in range(t):
	number = input()
	if len(number) < 3:
		if int(number) % 8 == 0 or int(''.join(list(reversed(number)))) % 8 == 0:
			print('YES')
		else:
			print('NO')
	else:
		in_digits = count_digs(number)
		if any([cmp_digs(tail, in_digits) for tail in needed_digits]):
			print('YES')
		else:
			print('NO')


from collections import Counter
from itertools import permutations
t = int(input())
for _ in range(t):
	s = input()
	digits = Counter(s)
	new_s = []
	for (digit, count) in digits.items():
		new_s.extend([digit for _ in range(min(count, 3))])
	final_s = ''.join(new_s)
	correct = False
	size = min(3, len(final_s))
	for permutation in permutations(final_s, size):
		triple = int(''.join(list(permutation)))
		if triple % 8 == 0:
			correct = True
			break
	print('YES' if correct else 'NO')


eights3 = [str(8 * i).zfill(3) for i in range(125)]
eights2 = [str(8 * i).zfill(2) for i in range(125)]

def f(n):
	if n == '8' or n == '0':
		return 'YES'
	if len(n) >= 3:
		for i in eights3:
			m = i
			for char in n:
				if len(i) == 1 and i[0] == char:
					i = ''
				elif len(i) == 2:
					if i[0] == char:
						i = i[1]
					elif i[1] == char:
						i = i[0]
				elif len(i) == 3:
					if i[0] == char:
						i = i[1:]
					elif i[1] == char:
						i = i[0] + i[2]
					elif i[2] == char:
						i = i[:2]
			if len(i) == 0:
				return 'YES'
	elif n in eights2 or n[::-1] in eights2:
		return 'YES'
	return 'NO'
for i in range(int(input())):
	print(f(input()))


t = int(input())
for test in range(t):
	num = input()
	l = len(num)
	k = 0
	if num == '8' or num == '0':
		k = -1
	if l == 2:
		a = int(num[0])
		b = int(num[1])
		if (10 * a + b) % 8 == 0 or (10 * b + a) % 8 == 0:
			k = -1
	for i in range(l):
		for j in range(i + 1, l):
			for h in range(j + 1, l):
				a = int(num[i])
				b = int(num[j])
				c = int(num[h])
				if (100 * a + 10 * b + c) % 8 == 0 or (100 * a + 10 * c + b) % 8 == 0 or (100 * b + 10 * a + c) % 8 == 0 or ((100 * b + 10 * c + a) % 8 == 0) or ((100 * c + 10 * a + b) % 8 == 0) or ((100 * c + 10 * b + a) % 8 == 0):
					k = -1
					break
			if k == -1:
				break
		if k == -1:
			break
	if k == -1:
		print('YES')
	else:
		print('NO')


from itertools import permutations as _permut
T = int(input())
for _ in range(T):
	S = 'NO'
	N = input()
	R = 3 if len(N) > 3 else len(N)
	for p in _permut(N, R):
		d = ''.join(p)
		if int(d) % 8 == 0:
			S = 'YES'
			break
	print(S)


ar = []
for i in range(0, 1001, 8):
	G = {}
	for k in range(10):
		G[k] = 0
	if len(str(i)) == 1:
		G[0] = 2
	elif len(str(i)) == 2:
		G[0] = 1
	for s in range(len(str(i))):
		G[int(str(i)[s])] += 1
	ar.append(G)
small = []
for i in range(0, 1001, 8):
	G = {}
	for k in range(10):
		G[k] = 0
	for s in range(len(str(i))):
		G[int(str(i)[s])] += 1
	small.append(G)
for _ in range(int(input())):
	n = int(input())
	s = str(n)
	d = {}
	for i in range(10):
		d[i] = 0
	for x in range(len(s)):
		d[int(s[x])] += 1
	if len(s) <= 3:
		if d in small:
			print('YES')
		else:
			print('NO')
	else:
		check = False
		for g in ar:
			if all((d[z] >= g[z] for z in range(10))):
				check = True
			if check:
				break
		if check:
			print('YES')
		else:
			print('NO')


import re

def computePermutationsOfMultiplesOf8LessThan1000():
	format3 = '{:03}'
	formatRegex = '|.*{}.*{}.*{}.*'

	def addPermutationsOf(n, s):
		n_ = format3.format(n)
		s.add(formatRegex.format(n_[1], n_[2], n_[0]))
		s.add(formatRegex.format(n_[1], n_[0], n_[2]))
		s.add(formatRegex.format(n_[2], n_[1], n_[0]))
		s.add(formatRegex.format(n_[2], n_[0], n_[1]))
		s.add(formatRegex.format(n_[0], n_[1], n_[2]))
		s.add(formatRegex.format(n_[0], n_[2], n_[1]))
	setOfElementaryRegexes = set()
	for i in range(125):
		addPermutationsOf(8 * i, setOfElementaryRegexes)
	regex = ''
	for s in setOfElementaryRegexes:
		regex += s
	return regex[1:]
regex = re.compile(computePermutationsOfMultiplesOf8LessThan1000())

def test(N):
	if len(N) <= 2:
		return int(N) % 8 == 0 or int(N[::-1]) % 8 == 0
	else:
		return regex.match(N) is not None
T = int(input())
for i in range(T):
	N = input()
	print('YES' if test(N) else 'NO')


from itertools import permutations
number = int(input().strip())
for i in range(number):
	string = input().strip()
	is_divisible = False
	if len(string) > 3:
		per_num = 3
	else:
		per_num = len(string)
	for num_string in permutations(string, per_num):
		number = 0
		if len(num_string) <= 3:
			for i in range(len(num_string)):
				number += int(num_string[i]) * 10 ** (len(num_string) - 1 - i)
		else:
			number = int(num_string[-1]) + 10 * int(num_string[-2]) + 100 * int(num_string[-3])
		if number % 8 == 0:
			print('YES')
			is_divisible = True
			break
	if not is_divisible:
		print('NO')


def isub(a, b):
	for l in a:
		if l not in b:
			return False
		b = b.replace(l, '', 1)
	return True
divs = {'000'}
for i in range(104, 1000, 8):
	divs.add(str(i))
T = int(input())
for t in range(T):
	N = input()
	ln = len(N)
	if ln == 1:
		print('YES' if int(N) % 8 == 0 else 'NO')
	elif ln == 2:
		if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0:
			print('YES')
		else:
			print('NO')
	else:
		for i in divs:
			if isub(i, N):
				print('YES')
				break
		else:
			print('NO')


def isDiv(digits):
	if len(digits) == 1:
		if digits[0] % 8 == 0:
			return 'YES'
	elif len(digits) == 2:
		if int(str(digits[0]) + str(digits[1])) % 8 == 0 or int(str(digits[1]) + str(digits[0])) % 8 == 0:
			return 'YES'
	else:
		for i in range(len(digits)):
			for j in range(len(digits)):
				for k in range(len(digits)):
					if i != j and i != k and (j != k):
						num = int(str(digits[i]) + str(digits[j]) + str(digits[k]))
						if num % 8 == 0:
							return 'YES'
	return 'NO'
t = int(input().strip())
for _ in range(t):
	n = input().strip()
	digits = list(map(int, list(n)))
	print(isDiv(digits))


st = set()
for i in range(100, 1000):
	if i % 8 == 0:
		i = str(i)
		st1 = (i[0], i[1], i[2])
		st.add(st1)
for i in range(10, 100):
	if i % 8 == 0:
		i = str(i)
		st1 = ('0', i[0], i[1])
		st.add(st1)
for i in range(0, 10):
	if i % 8 == 0:
		i = str(i)
		st1 = ('0', '0', i[0])
		st.add(st1)

def f():
	n = input()
	if len(n) == 0:
		print('YES')
	if len(n) == 1:
		if int(n) % 8 == 0:
			print('YES')
		else:
			print('NO')
	elif len(n) == 2:
		if int(n) % 8 == 0:
			print('YES')
		elif int(n[1] + n[0]) % 8 == 0:
			print('YES')
		else:
			print('NO')
	else:
		for i in st:
			if (n.count(i[0]) >= i.count(i[0]) and n.count(i[1]) >= i.count(i[1])) and n.count(i[2]) >= i.count(i[2]):
				print('YES')
				break
		else:
			print('NO')
t = int(input())
for i in range(t):
	f()


from itertools import permutations
for _ in range(int(input())):
	n = input()
	r = False
	if n.count('0') > 2:
		r = True
	elif len(n) == 1:
		r = int(n) % 8 == 0
	elif len(n) == 2:
		for c in permutations(n, 2):
			n = int(c[0]) * 10 + int(c[1])
			if n % 8 == 0:
				r = True
				break
	else:
		for c in permutations(n, 3):
			if int(c[2]) % 2 == 0:
				n = int(c[0]) * 100 + int(c[1]) * 10 + int(c[2])
				if n % 8 == 0:
					r = True
					break
	if r:
		print('YES')
	else:
		print('NO')


from itertools import permutations as p

def foo(n):
	l = 0
	if len(n) < 3:
		l = p(n)
	else:
		l = p(n, 3)
	for i in l:
		if int(''.join(i)) % 8 == 0:
			return 'YES'
	return 'NO'
for _ in range(int(input())):
	print(foo(input()))


import sys

def div8cache():
	rval = []
	for i in range(0, 1000, 8):
		cc = [0] * 10
		s = str(i)
		for c in s:
			d = int(c)
			cc[d] += 1
		rval.append(list(cc))
		for i in range(3 - len(s)):
			cc[0] += 1
			rval.append(list(cc))
	return rval

def solve(s, cache):

	def get_digits(cc):
		return sum(cc)
	cc = [0] * 10
	for c in s:
		d = int(c)
		cc[d] += 1
	print('s=%s' % s, file=sys.stderr)
	for ch in cache:
		match = True
		for (d, c) in enumerate(ch):
			if c > cc[d]:
				match = False
				break
		if match:
			(dch, dcc) = (get_digits(ch), get_digits(cc))
			if dcc > 2 and dch > 2 or (dcc < 3 and dch == dcc):
				print('match: ch=%s\n       cc=%s' % (ch, cc), file=sys.stderr)
				return True
	return False
cache = div8cache()
t = int(input())
for _ in range(t):
	s = input()
	if solve(s, cache):
		print('YES')
	else:
		print('NO')


def check(inp):
	if len(inp) == 1:
		if inp[0] in [0, 8]:
			print('YES')
			return 1
	if len(inp) == 2:
		if (10 * inp[0] + inp[1]) % 8 == 0 or (inp[0] + 10 * inp[1]) % 8 == 0:
			print('YES')
			return 1
	even = [0, 2, 4, 6, 8]
	odd = [1, 3, 5, 7, 9]
	num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
	for a1 in even:
		cache1inp = list(inp)
		if a1 not in cache1inp:
			continue
		cache1inp.remove(a1)
		for a2 in num:
			cache2inp = list(cache1inp)
			val2digit = 10 * a2 + a1
			if val2digit % 4 != 0:
				continue
			if a2 not in cache2inp:
				continue
			cache2inp.remove(a2)
			if val2digit % 8 == 0:
				a3ar = even
			else:
				a3ar = odd
			for a3 in a3ar:
				if a3 not in cache2inp:
					continue
				else:
					print('YES')
					return 1
	print('NO')
	return 0
t = input()
for it in range(int(t)):
	i = []
	for c in input():
		i.append(int(c))
	check(i)


import itertools
t = int(input())

def permutations(vals):
	return itertools.permutations(vals)

def reducenumber(n):
	nint = [int(k) for k in n]
	tmp = []
	for i in range(10):
		digitcount = min(nint.count(i), 3)
		tmp.extend([i] * digitcount)
	return tmp
for testnbr in range(t):
	n = input()
	n = reducenumber(n)
	founddivisor = False
	for p in permutations(n):
		if len(p) >= 3:
			val = int(str(p[len(p) - 3]) + str(p[len(p) - 2]) + str(p[len(p) - 1]))
		elif len(p) == 2:
			val = int(str(p[len(p) - 2]) + str(p[len(p) - 1]))
		else:
			val = int(str(p[len(p) - 1]))
		if val % 8 == 0:
			founddivisor = True
			break
	if founddivisor:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	n = input()
	d = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9': 0, '0': 0}
	if len(n) <= 2:
		temp = int(n)
		if 0 == temp % 8 or 0 == (temp % 10 * 10 + temp // 10) % 8:
			print('YES')
		else:
			print('NO')
		t -= 1
		continue
	lstEvenPrefix = ['08', '16', '24', '32', '40', '48', '56', '64', '72', '80', '96']
	lstOddPrefix = ['04', '12', '20', '28', '36', '52', '60', '68', '76', '84', '92']
	flag = False
	for ele in n:
		d[ele] += 1
	if d['8'] >= 3 or d['0'] >= 3:
		flag = True
	elif d['4'] >= 2 and (d['1'] > 0 or d['3'] > 0 or d['5'] > 0 or (d['7'] > 0) or (d['9'] > 0)):
		flag = True
	elif d['8'] >= 2 and (d['2'] > 0 or d['4'] > 0 or d['6'] > 0):
		flag = True
	elif d['0'] >= 2 and (d['2'] > 0 or d['4'] > 0 or d['6'] > 0 or (d['8'] > 0)):
		flag = True
	else:
		for ele in lstOddPrefix:
			if d[ele[0]] > 0 and d[ele[1]] > 0:
				d[ele[0]] -= 1
				d[ele[1]] -= 1
				if d['1'] > 0 or d['3'] > 0 or d['5'] > 0 or (d['7'] > 0) or (d['9'] > 0):
					flag = True
					break
		if not flag:
			for ele in lstEvenPrefix:
				if d[ele[0]] > 0 and d[ele[1]] > 0:
					d[ele[0]] -= 1
					d[ele[1]] -= 1
					if d['2'] > 0 or d['4'] > 0 or d['6'] > 0 or (d['8'] > 0):
						flag = True
						break
	if flag:
		print('YES')
	else:
		print('NO')
	t -= 1


import sys
import itertools as it

def divisible_by_8(digits):
	leftover = int(''.join([str(d) for d in digits])[-3:])
	return leftover % 8 == 0

def solve(str):
	digits = [int(c) for c in str]
	length = len(digits)
	perms = it.permutations(digits, min(length, 3))
	return any([divisible_by_8(p) for p in perms])
n = int(sys.stdin.readline())
for _ in range(n):
	s = sys.stdin.readline().strip()
	result = solve(s)
	print('YES' if result else 'NO')


from collections import Counter
ok1 = [Counter(str(i)) for i in range(0, 10, 8)]
ok2 = [Counter(str(i)) for i in range(16, 100, 8)]
ok3 = [Counter(str(i)) for i in range(104, 1000, 8)]
for _ in range(int(input())):
	N = input()
	if len(N) == 1:
		ok = ok1
	elif len(N) == 2:
		ok = ok2
	else:
		ok = ok3
	N = Counter(N)
	for c in ok:
		if all((N[i] >= c[i] for i in c)):
			print('YES')
			break
	else:
		print('NO')


def get_list_digits(n):
	n_list = list()
	if n == 0:
		n_list.append(0)
	while n > 0:
		n_list.append(n % 10)
		n //= 10
	return n_list

def exists_permutation_devisable_by_8(n):
	if n == 0:
		return True
	n_list = get_list_digits(n)
	for d in (x for x in range(2000) if x % 8 == 0):
		d_list = get_list_digits(d)
		if d >= 1000:
			d_list.remove(1)
		d_zeros = d_list.count(0)
		n_zeros = n_list.count(0)
		if len(n_list) > 3:
			n_required_zeros = 3
		else:
			n_required_zeros = len(n_list)
		n_required_zeros = n_required_zeros - len(d_list) + d_zeros
		match = True
		for d_digit in d_list:
			if d_list.count(d_digit) > n_list.count(d_digit):
				match = False
		if match and n_required_zeros <= n_zeros:
			return True
	return False

def do_it():
	Ts = input()
	T = int(Ts)
	for i in range(T):
		xs = input()
		x = int(xs)
		if exists_permutation_devisable_by_8(x):
			print('YES')
		else:
			print('NO')
do_it()


import itertools, sys
T = int(sys.stdin.readline())
for _ in range(T):
	N = sys.stdin.readline().strip()
	if len(N) == 1:
		print('YES' if N == '8' or N == '0' else 'NO')
	elif len(N) == 2:
		print('YES' if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0 else 'NO')
	else:
		for s in itertools.permutations(N, 3):
			if int(''.join(s)) % 8 == 0:
				print('YES')
				break
		else:
			print('NO')


from collections import Counter

def main():
	a = [Counter(map(int, format(8 * i, '03d'))) for i in range(125)]
	for i in range(int(input())):
		n = input().strip()
		if len(n) == 1:
			result = n in '08'
		elif len(n) == 2:
			result = int(n) % 8 == 0 or int(''.join(reversed(n))) % 8 == 0
		else:
			c = Counter(map(int, n))
			result = any((all((c.get(k, 0) >= v for (k, v) in x.items())) for x in a))
		print('YES' if result else 'NO')
main()


import itertools
T = int(input())
for _ in range(T):
	N = input()
	l = len(N)
	found = any([int(''.join(x)) % 8 == 0 for x in itertools.permutations(N, min(3, l))])
	if found:
		print('YES')
	else:
		print('NO')


t = int(input())
for ii in range(t):
	num = input()
	l = list(map(int, list(num)))
	flag = False
	if len(l) > 2:
		for i in range(len(l)):
			for j in range(len(l)):
				if j != i:
					for k in range(len(l)):
						if (k != i and k != j) and l[k] % 2 == 0:
							if (l[i] * 100 + l[j] * 10 + l[k]) % 8 == 0:
								flag = True
								break
	elif len(l) == 1:
		if l[0] % 8 == 0:
			flag = True
	elif (l[1] * 10 + l[0]) % 8 == 0 or (l[0] * 10 + l[1]) % 8 == 0:
		flag = True
	if flag:
		print('YES')
	else:
		print('NO')


from itertools import permutations

def yes():
	print('YES')

def no():
	print('NO')
S = {j for j in range(0, 1000) if j % 8 == 0}
T = int(input())
for t in range(T):
	N = input().strip()
	C = [0 for j in range(10)]
	for j in N:
		C[int(j)] += 1
	L = []
	for (i, c) in enumerate(C):
		for j in range(min(c, 3)):
			L.append(i)
	if len(N) == 1:
		if N == '0' or N == '8':
			yes()
		else:
			no()
	elif len(N) == 2:
		if 10 * int(N[0]) + int(N[1]) in S or 10 * int(N[1]) + int(N[0]) in S:
			yes()
		else:
			no()
	else:
		for p in permutations(L, 3):
			if p[0] * 100 + p[1] * 10 + p[2] in S:
				yes()
				break
		else:
			no()


from collections import Counter
checker = {i: tuple(map(Counter, {tuple(sorted(str(8 * x).zfill(i))) for x in range((10 ** i - 1) // 8 + 1)})) for i in range(1, 4)}
for _ in range(int(input())):
	y = Counter(input())
	for x in checker[min(sum(y.values()), 3)]:
		if all((x[i] <= y[i] for i in x)):
			print('YES')
			break
	else:
		print('NO')


tab = []
for i in range(125):
	s = str(i * 8 + 1000)[-3:]
	t = dict()
	for i in s:
		i = int(i)
		try:
			t[i] += 1
		except KeyError:
			t[i] = 1
	tab.append(t)
for _ in range(int(input())):
	N = input()
	dig = [0] * 10
	if len(N) < 3:
		if int(N) % 8 == 0 or int(N[::-1]) % 8 == 0:
			print('YES')
		else:
			print('NO')
		continue
	else:
		for i in N:
			dig[int(i)] += 1
		for i in tab:
			for j in i:
				if dig[j] < i[j]:
					break
			else:
				print('YES')
				break
		else:
			print('NO')


for _ in range(int(input())):
	i = list(input())
	i.sort(reverse=True)
	l = len(i)
	j = int(''.join(i))
	if j == 85310:
		print('NO')
		continue
	k = 10 * (l - 1)
	if k == 10:
		k = 16
	f1 = False
	while k <= j:
		m = list(str(k))
		f = True
		for p in set(m):
			if i.count(p) < m.count(p):
				f = False
				break
		if f:
			f1 = True
			break
		k += 8
	if f1:
		print('YES')
	else:
		print('NO')


from itertools import permutations
x = input()
listofanswers = []
for i in range(int(x)):
	perms = []
	y = input()
	answer = 'NO'
	if len(y) < 3:
		perms = [int(''.join(p)) for p in permutations(y)]
	else:
		perms = [int(''.join(p)) for p in permutations(y, 3)]
	for j in perms:
		if j % 8 == 0:
			answer = 'YES'
	listofanswers += [answer]
for i in listofanswers:
	print(i)


from itertools import permutations

def perm_is_div_by_8(n):
	digits = str(n)
	if not any((int(d) % 2 == 0 for d in digits)):
		return False
	for perm in permutations(digits, min(3, len(digits))):
		if int(''.join(perm)) % 8 == 0:
			return True
	return False
for _ in range(int(input().strip())):
	print('YES' if perm_is_div_by_8(input().strip()) else 'NO')


import itertools

def div_eight(string):
	for c in itertools.permutations(string, min(len(string), 3)):
		if int(''.join(c)) % 8 == 0:
			return True
	return False
import sys
_ = sys.stdin.readline()
for line in sys.stdin:
	if div_eight(line.strip()):
		print('YES')
	else:
		print('NO')


t = int(input())
for x in range(t):
	max = 1000
	n = input()
	if len(n) < 3:
		max = 100
	if len(n) < 2:
		max = 10
	a = [0 for y in range(10)]
	for y in range(len(n)):
		a[int(n[y])] += 1
	yes = 'NO'
	for i in range(0, max, 8):
		si = str(i)
		while len(si) < 3:
			si = '0' + si
		if len(n) < 3 or a[int(si[0])] > 0 or (i < 100 and int(n) < 100 and (int(si[0]) == 0)):
			if len(n) >= 3:
				a[int(si[0])] = a[int(si[0])] - 1
			if len(n) < 2 or a[int(si[1])] > 0 or (i < 10 and int(n) < 10 and (int(si[1]) == 0)):
				if len(n) >= 2:
					a[int(si[1])] = a[int(si[1])] - 1
				if a[int(si[2])] > 0:
					yes = 'YES'
					break
				if len(n) >= 2:
					a[int(si[1])] += 1
			if len(n) >= 3:
				a[int(si[0])] += 1
	print(yes)


import itertools

def check(num):
	x = list(range(len(num)))
	if len(x) == 1:
		if num[0] % 8 == 0:
			return True
		else:
			return False
	if len(x) == 2:
		if (num[0] + 10 * num[1]) % 8 == 0 or (num[1] + 10 * num[0]) % 8 == 0:
			return True
		else:
			return False
	for i in itertools.combinations(x, 3):
		for j in itertools.permutations(i):
			if (num[j[0]] + 2 * num[j[1]] + 4 * num[j[2]]) % 8 == 0:
				return True
	return False
T = int(input())
for i in range(T):
	N = [int(j) for j in input()]
	if check(N):
		print('YES')
	else:
		print('NO')


def main():
	t = int(input())
	for _ in range(t):
		s = input()
		if possible(s) == 1:
			print('YES')
		else:
			print('NO')

def possible(s):
	l = len(s)
	if l == 1:
		if s == '0' or s == '8':
			return 1
	elif l == 2:
		if div8([s[0], s[1]]) == 1 or div8([s[1], s[0]]) == 1:
			return 1
	else:
		for i in range(l):
			for j in range(l):
				if j != i:
					for k in range(l):
						if k != i and k != j:
							if div8([s[i], s[j], s[k]]) == 1:
								return 1
	return 0

def div8(a):
	s = ''.join(a)
	v = int(s) % 8
	if v == 0:
		return 1
	else:
		return 0
main()


import itertools
T = int(input())
for i in range(T):
	result = ''
	N = [int(x) for x in list(input().strip())]
	if len(N) == 1:
		perms = N
	elif len(N) == 2:
		perms = [N[0] * 10 + N[1], N[1] * 10 + N[0]]
	else:
		perms = [sum((v * 10 ** (3 - 1 - i) for (i, v) in enumerate(value))) for value in itertools.permutations(N, 3)]
	for p in perms:
		if p % 8 == 0:
			result = 'YES'
			break
	print(result or 'NO')


import sys
import itertools
t = int(sys.stdin.readline())

def isdiv(digits):
	if len(digits) == 1:
		return digits[0] % 8 == 0
	elif len(digits) == 2:
		return (digits[0] + digits[1] * 2) % 8 == 0
	return (digits[0] + 2 * digits[1] + 4 * digits[2]) % 8 == 0
for i in range(t):
	n = sys.stdin.readline()
	counts = dict()
	for digit in map(ascii, range(10)):
		counts[digit] = n.count(digit)
	digits = list()
	for digit in counts:
		digits.extend([digit] * min(3, counts[digit]))
	digits = list(map(int, digits))
	if any(map(isdiv, itertools.permutations(digits, min(3, len(digits))))):
		print('YES')
	else:
		print('NO')