import java.util.Scanner;
class BALSA {
static int findNum(int n) {
int x = n + 1, y, i;
int count = 0;
outer: while (true) {
i=x;
while (true) {
y = i % 10;
i = i / 10;
if (y == 3)
++count;
else {
if (count == 3)
break outer;
else if (i == 0) {
count=0;
break;
}
}
}
++x;
}
return x;
}
public static void main(String[] args) {
int T;
int n;
Scanner scan = new Scanner(System.in);
T = scan.nextInt();
while (T>0) {
n = scan.nextInt();
System.out.println(findNum(n));
T--;
}
}
}