Read problems statements in Mandarin chinese, Russian and Vietnamese as well. 

Given an integer A that contains n > 1 digits. You are asked to remove exactly one digit to make the result number is divisible by 6, also make it biggest possible.
 
------ Input ------ 

The first line contains an integer T, denotes the number of test cases. Each test case is described by an integer A in a single line.
 
------ Output ------ 

Each test case, output the answer on a single line (it's allowed to delete a digit such that the resulting number contains leading zeroes, you shouldn't remove them in this case). If it is impossible to make the result number satisfies the above condition,
output -1.
 
------ Constraints ------ 

$1 ≤ T ≤ 100$
$2 ≤ n ≤ 10^{5}$
$A will not contain leading zeroes$
$the sum of n over all test cases ≤ 10^{6}$

Subtasks:

$Subtask #1 (30 points): the sum of n over all test cases ≤ 2000$
$Subtask #2 (70 points): original constrains$

----- Sample Input 1 ------ 
3
123
100222
10003
----- Sample Output 1 ------ 
12
00222
-1

Source: codechef | Difficulty: HARD
Tags: Ad-hoc


Solutions:
for t in range(int(input())):
	s = input().strip()
	a = [int(c) for c in s]
	asu = sum(a)
	amod = asu % 3
	if a[-1] % 2 != 0:
		if a[-2] % 2 == 0:
			adiv3 = amod - a[-1]
			if adiv3 == 0 or adiv3 == -3 or adiv3 == -6 or (adiv3 == -9):
				print(s[:-1])
			else:
				print(-1)
		else:
			print(-1)
	else:
		max1 = -1
		for i in range(1, len(a) - 1):
			adiv3 = amod - a[i]
			if adiv3 == 0 or adiv3 == -3 or adiv3 == -6 or (adiv3 == -9):
				max1 = i
				if a[i + 1] > a[i]:
					break
		adiv3 = amod - a[-1]
		if a[-2] % 2 == 0:
			if adiv3 == 0 or adiv3 == -3 or adiv3 == -6 or (adiv3 == -9):
				if max1 == -1 or a[max1] > a[max1 + 1]:
					max1 = len(a) - 1
		adiv3 = amod - a[0]
		if adiv3 == 0 or adiv3 == -3 or adiv3 == -6 or (adiv3 == -9):
			if max1 == -1 or a[1] > a[0]:
				max1 = 0
		if max1 == -1:
			print(-1)
		else:
			print(s[0:max1] + s[max1 + 1:])


for _ in range(int(input())):
	n = input()
	l = len(n)
	ans = '-1'
	tot = sum(map(int, n))
	for i in range(l):
		y = n[:i] + n[i + 1:]
		if int(y[-1]) % 2 == 0 and (tot - int(n[i])) % 3 == 0 and (y > ans):
			ans = y
	print(ans)


for t in range(int(input())):
	n = str(input())
	tot = 0
	ans = '-1'
	tot = sum(map(int, n))
	for i in range(len(n)):
		num = n[:i] + n[i + 1:]
		if int(num[-1]) % 2 == 0 and (tot - int(n[i])) % 3 == 0 and (num > ans):
			ans = num
	print(ans)


for _ in range(int(input())):
	N = input()
	Sum = 0
	if int(N[-1]) % 2 != 0:
		for key in N:
			Sum += int(key)
		if (Sum - int(N[-1])) % 3 == 0 and int(N[-2]) % 2 == 0:
			print(N[:-1])
		else:
			print(-1)
	else:
		flag = 0
		result = '0'
		for key in N:
			Sum += int(key)
		length = len(N)
		for i in range(length - 1):
			if (Sum - int(N[i])) % 3 == 0:
				result = N[0:i] + N[i + 1:]
				flag = 1
				if int(N[i + 1]) > int(N[i]):
					flag = 2
					break
		if flag != 2:
			if (Sum - int(N[length - 1])) % 3 == 0 and int(N[length - 2]) % 2 == 0:
				result = N[:-1]
		if flag == 0:
			print(-1)
		elif flag == 2:
			print(result)
		else:
			print(result)


k = int(input())
while k > 0:
	k -= 1
	s = str(input())
	n = len(s)
	t = 0
	for i in range(n):
		t += int(s[i])
	p = ''
	for i in range(n):
		q = t
		q -= int(s[i])
		if q % 3 == 0:
			m = s[:i] + s[i + 1:]
			if int(m[-1]) % 2 == 0 and m > p:
				p = m
	if len(p) != 0:
		print(p)
	else:
		print(-1)


t = int(input())
for _ in range(t):
	a = input()
	ans = '-1'
	s = sum(map(int, a))
	for i in range(len(a)):
		r = a[:i] + a[i + 1:]
		s1 = s - int(a[i])
		if int(r[-1]) % 2 == 0 and s1 % 3 == 0 and (r > ans):
			ans = r
	print(ans)


for i in range(int(input())):
	n = input()
	maxval = '-1'
	a = sum(map(int, n))
	for pos in range(len(n)):
		nsum = a - int(n[pos])
		y = n[:pos] + n[pos + 1:]
		if int(y[-1]) % 2 == 0 and nsum % 3 == 0 and (maxval < y):
			maxval = y
	print(maxval)


T = int(input())
for k in range(T):
	num = [int(l) for l in input()]
	l = len(num)
	tot = num[-1]
	for l in num[:-1]:
		tot = (tot + 4 * l) % 6
	last = -1
	finished = False
	for (j, l) in enumerate(num[:-1]):
		if tot == 4 * int(l) % 6:
			if num[j] < num[j + 1]:
				print(''.join([str(i) for i in num[:j] + num[j + 1:]]))
				finished = True
				break
			if num[j] > num[j + 1]:
				last = ''.join([str(i) for i in num[:j] + num[j + 1:]])
	if not finished:
		if (tot - num[-1] - 3 * num[-2]) % 6 == 0:
			print(''.join([str(i) for i in num[:-1]]))
		else:
			print(last)


t = int(input())
for i in range(t):
	n = int(input())
	c = str(n)
	if n % 10 % 2 != 0:
		c = c[:-1]
		c = int(c)
		if c % 6 == 0:
			print(c)
		else:
			print(-1)
	else:
		s = 0
		for i in c:
			s = s + int(i)
		tb = -2
		g = s % 3
		for i in range(len(c) - 1):
			if int(c[i]) % 3 == g:
				if c[i] < c[i + 1]:
					tb = -1
					c = c[:i] + c[i + 1:]
					break
				elif i == len(c) - 2:
					if int(c[i + 1]) % 3 == g and int(c[i]) % 2 == 0:
						tb = i + 1
					else:
						tb = i
				else:
					tb = i
			elif i == len(c) - 2:
				if int(c[i + 1]) % 3 == g and int(c[i]) % 2 == 0:
					tb = i + 1
		if tb == -2:
			print(-1)
		elif tb == -1:
			print(c)
		else:
			print(c[:tb] + c[tb + 1:])


for t in range(int(input())):
	a = input()
	m = '-1'
	s = sum(map(int, a))
	temp_len = len(a)
	for i in range(temp_len):
		temp_s = s - int(a[i])
		temp_a = a[0:i] + a[i + 1:temp_len]
		if temp_a[-1] == 0:
			continue
		if int(temp_a[-1]) % 2 == 0 and temp_s % 3 == 0 and (m < temp_a):
			m = temp_a
	print(m)


for t in range(int(input())):
	a = input()
	m = '-1'
	s = 0
	length = len(a)
	for i in range(length):
		s += int(a[i])
	for i in range(length):
		temp_s = s - int(a[i])
		temp_a = a[0:i] + a[i + 1:length]
		if temp_a[-1] == 0:
			continue
		z = temp_a
		if int(temp_a[-1]) % 2 == 0 and temp_s % 3 == 0 and (m < z):
			m = temp_a
	print(m)


T = int(input())
for _ in range(T):
	s = input().strip()
	a = [int(c) for c in s]
	asu = sum(a)
	amod = asu % 3
	if a[-1] % 2 != 0:
		if a[-2] % 2 == 0:
			adiv3 = amod - a[-1]
			if adiv3 == 0 or adiv3 == -3 or adiv3 == -6 or (adiv3 == -9):
				print(s[:-1])
			else:
				print(-1)
		else:
			print(-1)
	else:
		max = -1
		for i in range(1, len(a) - 1):
			adiv3 = amod - a[i]
			if adiv3 == 0 or adiv3 == -3 or adiv3 == -6 or (adiv3 == -9):
				max = i
				if a[i + 1] > a[i]:
					break
		adiv3 = amod - a[-1]
		if a[-2] % 2 == 0:
			if adiv3 == 0 or adiv3 == -3 or adiv3 == -6 or (adiv3 == -9):
				if max == -1 or a[max] > a[max + 1]:
					max = len(a) - 1
		adiv3 = amod - a[0]
		if adiv3 == 0 or adiv3 == -3 or adiv3 == -6 or (adiv3 == -9):
			if max == -1 or a[1] > a[0]:
				max = 0
		if max == -1:
			print(-1)
		else:
			print(s[0:max] + s[max + 1:])


for i in range(int(input())):
	largest = '-1'
	sum = 0
	n = input()
	length = len(n)
	for i in range(length):
		sum += int(n[i])
	for i in range(length):
		newlarge = n[0:i] + n[i + 1:length]
		newSum = sum - int(n[i])
		if newSum % 3 == 0 and int(newlarge[-1]) % 2 == 0 and (newlarge > largest):
			largest = newlarge
	print(largest)


def getNum(digit):
	return ord(digit) - ord('0')

def divisible(n):
	if ord(n[-1]) % 2 != 0:
		return False
	digit_sum = sum([getNum(i) for i in n])
	return digit_sum % 3 == 0

def getRes(n, mod):
	d = []
	leftMost = -1
	rightMost = -1
	for (index, digit) in enumerate(n):
		if getNum(digit) % 3 == mod:
			if index != len(n) - 1 and getNum(n[index + 1]) > getNum(digit):
				leftMost = index
				break
			if index == len(n) - 1:
				if divisible(n[:-1]):
					rightMost = index
			else:
				rightMost = index
	target = 0
	if leftMost != -1:
		target = leftMost
	elif rightMost != -1:
		target = rightMost
	else:
		return -1
	res = n[:target] + n[target + 1:]
	return res
for _ in range(int(input())):
	n = input()
	if divisible(n):
		print(getRes(n, 0))
		continue
	if getNum(n[-1]) % 2 != 0:
		if divisible(n[:-1]):
			print(n[:-1])
		else:
			print(-1)
	else:
		sum_digits = sum([getNum(i) for i in n])
		mod = sum_digits % 3
		print(getRes(n, mod))


T = int(input())
for i in range(0, T):
	num = list(input())
	s = 0
	f = 0
	for x in num:
		s += int(x)
	if int(num[-1]) % 2 != 0:
		if int(num[-2]) % 2 != 0:
			print('-1')
			continue
		if (s - int(num[-1])) % 3 != 0:
			print('-1')
			continue
		num.pop()
		print(''.join(num))
		continue
	l = len(num)
	for i in range(0, l):
		lastdigit = l - 1
		if i == l - 1:
			lastdigit -= 1
		if (s - int(num[i])) % 3 != 0 or int(num[lastdigit]) % 2 != 0:
			continue
		if f == 0:
			_max = list(num)
			_max.pop(i)
			f = 1
		else:
			z = 0
			for j in range(0, len(num)):
				if j == i:
					continue
				if int(num[j]) > int(_max[z]):
					_max = list(num)
					_max.pop(i)
				elif int(_max[z]) > int(num[j]):
					break
				z = z + 1
	if f:
		print(''.join(_max))
	else:
		print('-1')


t = int(input())
for test in range(t):
	d = input()
	a = [int(i) for i in d]
	if len(a) == 2:
		if 6 in a:
			print(6)
		elif 0 in a:
			print(0)
		else:
			print(-1)
		continue
	sums = sum(a)
	index = -1
	flag = 0
	for i in range(len(a) - 1):
		if (sums - a[i]) % 3 == 0:
			index = i
			if a[i] < a[i + 1]:
				if a[-1] % 2 == 0:
					for i in range(index):
						print(a[i], end='')
					for i in range(index + 1, len(a)):
						print(a[i], end='')
					print()
					flag = 1
				break
	if flag == 1:
		continue
	if (sums - a[-1]) % 3 == 0 and a[-2] % 2 == 0:
		for i in range(len(a) - 1):
			print(a[i], end='')
		print()
		continue
	if index != -1:
		if a[-1] % 2 == 0:
			for i in range(index):
				print(a[i], end='')
			for i in range(index + 1, len(a)):
				print(a[i], end='')
			print()
			continue
	print(-1)


for _ in range(int(input().strip())):
	s = input()
	n = len(s)
	summ = 0
	ind = -1
	flag = 0
	for ele in s:
		summ += int(ele)
	if int(s[n - 1]) % 2 == 1 and int(s[n - 2]) % 2 == 1:
		flag = 1
	elif int(s[n - 1]) % 2 == 1 and (summ - int(s[n - 1])) % 3 != 0:
		flag = 1
	elif int(s[n - 1]) % 2 == 1:
		ind = n - 1
	if flag != 1 and ind == -1:
		if int(s[n - 2]) % 2 == 0:
			for i in range(n):
				if (summ - int(s[i])) % 3 == 0 and i < n - 1 and (s[i + 1] > s[i]):
					ind = i
					break
				if (summ - int(s[i])) % 3 == 0:
					ind = i
		else:
			for i in range(n - 1):
				if (summ - int(s[i])) % 3 == 0 and i < n - 1 and (s[i + 1] > s[i]):
					ind = i
					break
				if (summ - int(s[i])) % 3 == 0:
					ind = i
	if flag == 1 or ind == -1:
		print('-1')
	elif ind == n - 1 and int(s[ind - 1]) % 2 == 1:
		print('-1')
	else:
		for i in range(n):
			if i == ind:
				continue
			print(s[i], end='')
		print('')


for t in range(int(input())):
	a = list(input())
	l = len(a)
	for i in range(l):
		a[i] = int(a[i])
	if a[-1] % 2 == 1 and a[-2] % 2 == 1:
		print(-1)
		continue
	s = sum(a)
	ans = '-1'
	if a[-1] % 2 == 1:
		s -= a[-1]
		if s % 3 != 0:
			print(-1)
			continue
		else:
			for i in range(l):
				a[i] = str(a[i])
			del a[-1]
			print(''.join(a))
			continue
	elif a[-1] % 2 == 0:
		if a[-2] % 2 == 0:
			if (s - a[-1]) % 3 == 0:
				ans = l - 1
	i = 0
	while i <= l - 2:
		if (s - a[i]) % 3 == 0:
			if ans == '-1':
				ans = i
			elif ans + 1 < l and a[ans + 1] <= a[ans]:
				ans = i
			elif ans + 1 >= l:
				if a[i + 1] >= a[i]:
					ans = i
		i += 1
	if ans == '-1':
		print(-1)
		continue
	for i in range(l):
		a[i] = str(a[i])
	del a[ans]
	print(''.join(a))


T = int(input())
for _ in range(T):
	s = input()
	if int(s[-1]) % 2 != 0:
		s = s[:-1]
		if int(s[-1]) % 2 != 0:
			print(-1)
			continue
		S = sum([int(x) for x in list(s)]) % 3
		if S != 0:
			print(-1)
			continue
		else:
			print(s)
			continue
	S = sum([int(x) for x in list(s)]) % 3
	shortcutTaken = False
	for i in range(len(s) - 1):
		if int(s[i]) % 3 == S and int(s[i]) < int(s[i + 1]):
			print(s[:i] + s[i + 1:])
			shortcutTaken = True
			break
	if shortcutTaken:
		continue
	best = '-1'
	for i in range(len(s)):
		k = s[:i] + s[i + 1:]
		if int(k) > int(best) and int(k) % 6 == 0:
			best = k
	print(best)


t = int(input())
ctr = 0
while t > ctr:
	a = input()
	sum = 0
	flag = 0
	x = 0
	f = 0
	for i in a:
		sum += int(i)
	sum = sum % 3
	if int(a[-1]) % 2 == 1:
		if int(a[-1]) % 3 != sum or int(a[-2]) % 2 == 1:
			print(-1)
		elif int(a[-1]) % 3 == sum:
			print(a[:-1])
		else:
			print(-1)
	else:
		for i in range(len(a) - 1):
			if int(a[i]) % 3 == sum:
				x = 1
			if int(a[i]) % 3 == sum and int(a[i]) < int(a[i + 1]):
				a = a[:i] + a[i + 1:]
				flag = 1
				break
		if int(a[-1]) % 3 == sum and int(a[-2]) % 2 == 0:
			f = 1
		if flag == 1:
			print(a)
		elif f == 1:
			print(a[:-1])
		elif x == 1:
			for i in range(len(a) - 2, -1, -1):
				if int(a[i]) % 3 == sum:
					a = a[:i] + a[i + 1:]
					print(a)
					break
		else:
			print(-1)
	ctr += 1


t = int(input())
for _ in range(t):
	a = input()
	n = len(a)
	if int(a[n - 1]) % 2 != 0 and int(a[n - 2]) % 2 != 0:
		print('-1')
		continue
	elif int(a[n - 1]) % 2 != 0:
		tmp = a[0:n - 1]
		if int(tmp) % 3 == 0:
			print(tmp)
		else:
			print('-1')
		continue
	sim = 0
	ans = []
	for i in a:
		sim += int(i)
	for i in range(n):
		tmp = sim - int(a[i])
		if tmp % 3 != 0:
			continue
		if i == n - 1 and int(a[n - 2]) % 2 == 0:
			ans.append(a[0:i])
		elif i < n - 1:
			ans.append(a[0:i] + a[i + 1:n])
	if len(ans) == 0:
		print('-1')
	else:
		print(max(ans))


t = int(input())
for i in range(t):
	a = int(input())
	sa = str(a)
	b = []
	b.extend(map(int, list(sa)))
	s = sum(b)
	c = []
	c.extend(map(str, list(sa)))
	if a % 2 != 0:
		if (s - int(sa[-1])) % 3 != 0:
			print(-1)
		elif int(sa[0:len(sa) - 1]) % 2 != 0:
			print(-1)
		else:
			print(sa[0:len(sa) - 1])
	else:
		tag = 0
		tag1 = 0
		for j in range(len(sa) - 1):
			if (s - b[j]) % 3 == 0:
				ans = j
				tag = 1
				if b[j] < b[j + 1]:
					tag1 = 1
					break
		if b[-2] % 2 == 0 and (s - b[-1]) % 3 == 0:
			if tag1 == 0:
				ans = len(b) - 1
				tag = 1
		if tag == 0:
			print(-1)
		else:
			print(''.join(c[0:ans]) + ''.join(c[ans + 1:len(c)]))


def divby3(a):
	r = 0
	for i in a:
		r = (r + int(i)) % 3
	return r

def meme(a):
	r = divby3(a)
	last = int(a[len(a) - 1])
	if last % 2 == 1:
		if last % 3 == r and int(a[len(a) - 2]) % 2 == 0:
			return a[:len(a) - 1]
		return -1
	m = ''
	for i in range(len(a) - 1):
		if int(a[i]) % 3 == r:
			if m < a[:i] + a[i + 1:]:
				m = a[:i] + a[i + 1:]
	if len(a) >= 2:
		if int(a[len(a) - 2]) % 2 == 0:
			if int(a[len(a) - 1]) % 3 == r:
				if m < a[:len(a) - 1]:
					m = a[:len(a) - 1]
	if m == '':
		return -1
	return m
t = int(input())
for _ in range(t):
	a = input()
	if a[len(a) - 1] == ' ':
		a = a[:len(a) - 1]
	c = meme(a)
	print(c)


t = int(input())
for _ in range(t):
	cur = 0
	a = input()
	l = len(a)
	x = int(a[l - 1])
	if x % 2 != 0:
		if int(a[l - 2]) % 2 != 0:
			print(-1)
		else:
			sum = 0
			for i in range(l - 1):
				sum += int(a[i])
			if sum % 3 == 0:
				print(a[0:l - 1])
			else:
				print(-1)
	else:
		sum = 0
		c = ''
		for i in range(l):
			sum += int(a[i])
		for i in range(l - 1):
			if (sum - int(a[i])) % 3 == 0 and a[0:i] + a[i + 1:l] > c:
				c = a[0:i] + a[i + 1:l]
		if int(a[l - 2]) % 2 == 0 and (sum - int(a[l - 1])) % 3 == 0 and (a[0:l - 1] > c):
			c = a[0:l - 1]
		if c == '':
			c = '-1'
		print(c)


t = int(input())

def s(n):
	ret = 0
	for char in n:
		ret += int(char)
	return ret
for _ in range(t):
	n = input()
	if int(n[-1]) % 2 and int(n[-2]) % 2:
		print(-1)
	elif int(n[-1]) % 2 and int(n[-2]) % 2 == 0:
		if int(n[:-1]) % 6 == 0:
			print(n[:-1])
		else:
			print(-1)
	else:
		d = s(n)
		k = []
		for i in range(10):
			if i % 3 == d % 3:
				k.append(str(i))
		w = False
		last = None
		for i in range(len(n) - 1):
			if n[i] in k:
				last = i
				if int(n[i + 1]) > int(n[i]):
					print(n[:i] + n[i + 1:])
					w = True
					break
		if int(n[:-1]) % 6 == 0:
			last = len(n) - 1
		if not w:
			if last is None:
				print(-1)
			else:
				print(n[:last] + n[last + 1:])


for _ in range(int(input())):
	a = [int(i) for i in list(input())]
	x = sum(a)
	if a[-1] % 2:
		if (x - a[-1]) % 3 == 0 and a[-2] % 2 == 0:
			print(*a[:-1], sep='')
		else:
			print(-1)
		continue
	flag = False
	for i in range(0, len(a) - 1):
		if a[i] < a[i + 1] and (x - a[i]) % 3 == 0:
			print(*a[:i] + a[i + 1:], sep='')
			flag = True
			break
	if flag:
		continue
	if a[-2] % 2 == 0 and (x - a[-1]) % 3 == 0:
		print(*a[:-1], sep='')
		continue
	for i in range(len(a) - 2, -1, -1):
		if (x - a[i]) % 3 == 0:
			print(*a[:i] + a[i + 1:], sep='')
			flag = True
			break
	if not flag:
		print(-1)