Lucky Numbers
Olympus City recently launched the production of personal starships. Now everyone on Mars can buy one and fly to other planets inexpensively.Each starship has a number —some positive integer $$$x$$$. Let's define the luckiness of a number $$$x$$$ as the difference between the largest and smallest digits of that number. For example, $$$142857$$$ has $$$8$$$ as its largest digit and $$$1$$$ as its smallest digit, so its luckiness is $$$8-1=7$$$. And the number $$$111$$$ has all digits equal to $$$1$$$, so its luckiness is zero.Hateehc is a famous Martian blogger who often flies to different corners of the solar system. To release interesting videos even faster, he decided to buy himself a starship. When he came to the store, he saw starships with numbers from $$$l$$$ to $$$r$$$ inclusively. While in the store, Hateehc wanted to find a starship with the luckiest number.Since there are a lot of starships in the store, and Hateehc can't program, you have to help the blogger and write a program that answers his question.  
Let's look at two test examples:   the luckiness of the number $$$59$$$ is $$$9 - 5 = 4$$$;  the luckiness of $$$60$$$ equals $$$6 - 0 = 6$$$;  the luckiness of $$$61$$$ equals $$$6 - 1 = 5$$$;  the luckiness of $$$62$$$ equals $$$6 - 2 = 4$$$;  the luckiness of $$$63$$$ is $$$6 - 3 = 3$$$.  Thus, the luckiest number is $$$60$$$.In the fifth test example, the luckiest number is $$$90$$$.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10\,000$$$) —the number of test cases.Each of the following $$$t$$$ lines contains a description of the test case. The description consists of two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 10^6$$$) — the largest and smallest numbers of the starships in the store.
Print $$$t$$$ lines, one line for each test case, containing the luckiest starship number in the store.If there are several ways to choose the luckiest number, output any of them.
def luckiness(n):
    if n < 10:
        return 0
    m = n % 10
    M = n % 10
    n = n // 10
    while n:
        a = n % 10
        if a > M:
            M = a
        elif a < m:
            m = a
        n = n // 10
    return M - m

def max_luckiness(l, r):
    n = 1 + r - l
    ans = l
    m = luckiness(l)
    for i in range(1, n):
        t = luckiness(l + i)
        if t >= m:
            ans = l + i
            m = t
        if m == 9:
            break
    return ans

def main():
    t = int(input())
    for i in range(t):
        (l, r) = map(int, input().split())
        print(max_luckiness(l, r))
if __name__ == '__main__':
    main()
t = int(input())
for _ in range(t):
    (l, r) = list(map(int, input().split()))
    bigger_number = 0
    bigger_luck = 0
    for i in range(r, l - 1, -1):
        if bigger_luck == 9:
            break
        s = sorted(str(i))
        if int(s[-1]) - int(s[0]) >= bigger_luck:
            bigger_luck = int(s[-1]) - int(s[0])
            bigger_number = i
    print(bigger_number)
from string import ascii_lowercase

class reader(object):

    def __init__(self):
        self.readone = lambda : int(input())
        self.readfixed = lambda : map(int, input().split())
        self.readtuple = lambda : (int(e) for e in input().split())
        self.readlist = lambda : [int(e) for e in input().split()]

class printer(object):

    def __init__(self):
        self.printone = lambda p: print(p)
        self.printoneDim = lambda col: print([e for e in col])
        self.printtwoDim = lambda col: print([[p for p in e] for e in col])

    def printList(self, col):
        for e in col:
            print(e)

class point(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def getCoords(self):
        return (self.x, self.y)

def function(*args):
    r = reader()
    n = r.readone()
    res = []
    for _ in range(n):
        (mn, mx) = r.readfixed()
        luck = 0
        out = mn
        for i in range(mn, mx + 1):
            st = str(i)
            diff = int(max(st)) - int(min(st))
            if diff > luck:
                luck = diff
                out = i
                if luck == 9:
                    break
        res.append(out)
    return res
if __name__ == '__main__':
    res = function()
    p = printer()
    p.printList(res)