#!/usr/bin/env python

import sys

T = int(raw_input())
numbers = set(range(1,10))
ignore = lambda x : sorted(i for i in str(x) if int(i) > 0)
for t in range(T):
    N = int(raw_input())
    a = ignore(N)
    b = set(a)
    d = N + 1
    while True:
        e = ignore(d)
        if set(e) == b and e == a:
            break
        else:
            d += 1
    print "Case #%d: %d" % (t+1, d)
    sys.stdout.flush()
        
    

79ca169c32ba3f965a5785f0e863d321a8f943f5
#!/usr/bin/env python

import sys

T = int(raw_input())
numbers = set(range(1,10))
ignore = lambda x : sorted(i for i in str(x) if int(i) > 0)
for t in range(T):
    N = int(raw_input())
    a = ignore(N)
    b = set(a)
    d = N + 1
    while True:
        e = ignore(d)
        if set(e) == b and e == a:
            break
        else:
            d += 1
    print "Case #%d: %d" % (t+1, d)
    sys.stdout.flush()
        
    
