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

It's well known that our friend Balsa likes the digit $3$ very much. He's so obsessed with this digit that he spends time thinking about it even during holidays.

Today, Balsa invented an interesting problem: For a positive integer $N$, find the smallest integer greater than $N$ such that its decimal representation contains the digit $3$ at least three times.
 
You are given the integer $N$. Please solve this problem for Balsa.

------  Input  ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first and only line of each test case contains a single integer $N$.

------  Output ------
For each test case, print a single line containing one integer — the smallest number greater than $N$ containing the digit $3$ at least three times.

------  Constraints ------
$1 ≤ T ≤ 40$
$1 ≤ N ≤ 2 \cdot 10^{9}$

----- Sample Input 1 ------ 
3
221
333
3002
----- Sample Output 1 ------ 
333
1333
3033

Difficulty: MEDIUM
Tags: Mathematics

Implementation

Complete search
Skill types: Complete search

Starter code:

Solutions:
for _ in range(int(input())):
	n = int(input())
	n += 1
	while 1 > 0:
		s = str(n)
		if str(n).count('3') >= 3:
			break
		n += 1
	print(n)


for q in range(int(input())):
	n = int(input())
	n += 1
	while True:
		cnt = str(n).count('3')
		if cnt >= 3:
			break
		n += 1
	print(n)


for _ in range(int(input())):
	N = int(input()) + 1
	while list(str(N)).count('3') < 3:
		N += 1
	print(N)


for j in range(int(input())):
	g = int(input())
	g += 1
	r = str(g).count('3')
	while r < 3:
		g += 1
		r = str(g).count('3')
	print(g)


t = int(input())
for i in range(t):
	N = int(input())
	s = N + 1
	while True:
		c = str(s)
		if c.count('3') >= 3:
			break
		s = s + 1
	print(s)


for i in range(int(input())):
	n = int(input())
	k = n
	new = str(k + 1)
	while new.count('3') < 3:
		k += 1
		new = str(k)
	print(new)


for i in range(int(input())):
	n = int(input())
	while n > 0:
		n += 1
		if str(n).count('3') >= 3:
			print(n)
			break


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	for i in range(n + 1, n + 10000):
		s = Counter(str(i))
		if s['3'] >= 3:
			print(i)
			break


for j in range(int(input())):
	n = int(input())
	while True:
		m = n + 1
		m = str(m)
		a = list(m)
		if a.count('3') >= 3:
			print(m)
			break
		else:
			n += 1


def solve(n):
	return False if n.count('3') >= 3 else True
t = int(input())
for _ in range(t):
	n = int(input())
	n += 1
	while solve(str(n)):
		n += 1
	print(n)


for _ in range(int(input())):
	n = int(input()) + 1
	while str(n).count('3') < 3:
		n += 1
	print(n)


def check(num):
	temp = num
	count = 0
	while temp > 0:
		r = temp % 10
		if r == 3:
			count += 1
		temp = temp // 10
	if count >= 3:
		return 1
	else:
		return 0
t = int(input())
for _ in range(t):
	n = int(input())
	n = n + 1
	while True:
		if check(n):
			break
		n = n + 1
	print(n)


t = 0
t = int(input())
for t in range(0, t):
	s = input()
	k = int(s) + 1
	while 1:
		k = str(k)
		if k.count('3') >= 3:
			print(k)
			break
		k = int(k) + 1


for i in range(int(input())):
	num = int(input()) + 1
	while not str(num).count('3') >= 3:
		num += 1
	print(num)


T = int(input())
for _ in range(T):
	s = input()
	i = int(s) + 1
	while 1:
		i = str(i)
		if i.count('3') >= 3:
			print(i)
			break
		i = int(i)
		i += 1


def check(num):
	temp = num
	c = 0
	while temp > 0:
		r = temp % 10
		if r == 3:
			c = c + 1
		temp = temp // 10
	if c >= 3:
		return 1
	else:
		return 0
x = int(input())
for i in range(x):
	n = int(input())
	n = n + 1
	while True:
		if check(n):
			break
		n = n + 1
	print(n)


t = int(input())
for _ in range(t):
	n = int(input())
	m = n
	c3 = 0
	if n < 333:
		print(333)
	elif n == 333:
		print(1333)
	else:
		m = m + 1
		while m:
			l = m
			c3 = 0
			while l > 0:
				if l % 10 == 3:
					c3 = c3 + 1
				l = l // 10
			if c3 >= 3:
				break
			m = m + 1
		print(m)


t = int(input())
for i in range(t):
	m = int(input())
	while 1:
		m = m + 1
		x = str(m)
		if x.count('3') >= 3:
			print(x)
			break


def check(n):
	temp = n
	c = 0
	while temp > 0:
		r = temp % 10
		temp = temp // 10
		if r == 3:
			c = c + 1
	if c >= 3:
		return 1
	return 0
t = int(input())
for _ in range(t):
	n = int(input())
	n = n + 1
	while True:
		if check(n):
			break
		n = n + 1
	print(n)


def count(n):
	c = 0
	while n > 0:
		if n % 10 == 3:
			c += 1
		n //= 10
	return c
for item in range(int(input())):
	n = int(input())
	n += 1
	while count(n) < 3:
		n += 1
	print(n)


def chk(n):
	t = n
	c = 0
	while t > 0:
		r = t % 10
		if r == 3:
			c += 1
		t //= 10
	if c >= 3:
		return 1
	return 0
for t in range(int(input())):
	n = int(input())
	n += 1
	while True:
		if chk(n):
			break
		n += 1
	print(n)


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	for i in range(n + 1, sys.maxsize):
		s = str(i)
		if s.count('3') >= 3:
			print(s)
			break


for i in range(int(input())):
	n = int(input())
	j = False
	while '1':
		n += 1
		if str(n).count('3') >= 3:
			print(n)
			break


for _ in range(int(input())):
	list1 = []
	i = int(input())
	i1 = i + 1
	while len(list1) != 1:
		if str(i1).count('3') >= 3:
			list1.append(i)
		else:
			i1 += 1
	print(i1)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	while True:
		n += 1
		if str(n).count('3') >= 3:
			print(n)
			break


def check(num):
	t = num
	c = 0
	while t > 0:
		x = t % 10
		if x == 3:
			c += 1
		t = t // 10
	if c >= 3:
		return 1
	else:
		return 0
for _ in range(int(input())):
	n = int(input())
	n = n + 1
	while True:
		if check(n):
			break
		else:
			n = n + 1
	print(n)


def solve(n):
	n += 1
	while str(n).count('3') < 3:
		n += 1
	return n

def main():
	for _ in range(int(input())):
		n = int(input())
		print(solve(n))
main()


for _ in range(int(input())):
	n = int(input())
	while True:
		n += 1
		if str(n).count('3') >= 3:
			print(n)
			break


for _ in range(int(input())):
	n = int(input())
	if n < 333:
		print(333)
	elif n >= 333 and n <= 1000:
		print(1333)
	else:
		while True:
			n += 1
			if str(n).count('3') >= 3:
				break
		print(n)


N = int(input())
for i in range(N):
	n = int(input())
	while True:
		n += 1
		if str(n).count('3') >= 3:
			break
	print(n)


def check(n):
	temp = n
	c = 0
	p = 0
	while temp > 0:
		k = temp % 10
		if k == 3:
			c += 1
		if c == 3:
			p = temp
			break
		temp = temp // 10
	return p
T = int(input())
for t in range(T):
	N = int(input())
	N = N + 1
	while True:
		if check(N):
			break
		N = N + 1
	print(N)


def doit(n):
	return str(n).count('3')
total_testcases = int(input())
for testcase in range(total_testcases):
	n = int(input())
	n += 1
	while doit(n) < 3:
		n += 1
	print(n)


from math import *
import sys

def input():
	return sys.stdin.readline().replace('\n', '').strip()
sys.setrecursionlimit(10 ** 9)
for _ in range(int(input())):
	n = int(input())
	while True:
		n += 1
		if str(n).count('3') >= 3:
			print(n)
			break


for u in range(int(input())):
	n = int(input())
	n += 1
	q = str(n).count('3')
	while q < 3:
		n += 1
		q = str(n).count('3')
	print(n)


for _ in range(int(input())):
	a = int(input())
	ans = 0
	for i in range(1, 1001):
		a += 1
		if str(a).count('3') >= 3:
			ans = a
			break
	print(ans)


for _ in range(int(input())):
	n = int(input())
	if n < 333:
		ans = 333
	else:
		while True:
			n += 1
			nn = str(n)
			xd = nn.count('3')
			if xd >= 3:
				ans = nn
				break
	print(ans)


for tc in range(int(input())):
	num = int(input())
	while True:
		num += 1
		Str = str(num)
		ans = 0
		ans = Str.count('3')
		if ans >= 3:
			print(Str)
			break


for _ in range(int(input())):
	n = int(input())
	while True:
		c = 0
		x = str(n + 1)
		ans = x.count('3')
		if ans >= 3:
			print(x)
			break
		n += 1


it = int(input())
for i in range(it):
	num = int(input())
	s = num + 1
	while True:
		count = str(s)
		if count.count('3') >= 3:
			break
		s = s + 1
	print(s)


for i in range(int(input())):
	m = int(input())
	while m > 0:
		m = m + 1
		s = str(m)
		b = s.count('3')
		if b >= 3:
			print(m)
			break


for t in range(int(input())):
	n = int(input())
	(k, count) = (0, 0)
	a = ''
	if n < 333:
		print('333')
	else:
		for i in range(n + 1, n * 10):
			a = str(i)
			k = a.count('3')
			if k >= 3:
				print(i)
				break


for i in range(int(input())):
	n = int(input())
	ans = 0
	if n < 333:
		print('333')
	else:
		for j in range(n + 1, n * 10):
			count = 0
			for k in str(j):
				if k == '3':
					count += 1
			if count >= 3:
				print(j)
				break


for _ in range(int(input())):
	p = int(input())
	while p > 0:
		p += 1
		if str(p).count('3') >= 3:
			print(p)
			break


for _ in range(int(input())):
	n = int(input())
	while n != 0:
		n = n + 1
		x = str(n)
		y = x.count('3')
		if y >= 3:
			print(n)
			break