Problem: 2050D - Digital string maximization
Language: python

Solution 1 (runtime: 1171 ms):

class LinkedList:
    def __init__(self, l):
        self.list = l
        self.len = len(l)
        self.next = [i+1 for i in range(self.len)]
        self.next[-1] = None
        self.prev = [i-1 for i in range(self.len)]
        self.prev[0] = None
        self.head = 0
        self.current = self.head
    
    def to_head(self):
        self.current = self.head
        
    def pop(self, i=None):
        if i == None:
            self.current = self.head
        else:
            self.current = i
        next_ = self.next[self.current]
        prev_ = self.prev[self.current]
        if self.len == 1:
            self.head=None  
        elif self.current == self.head:
            self.head = self.next[self.head]
            self.prev[self.head] = None
        elif next_ == None:
            self.next[prev_] = None 
        else:
            self.next[prev_] = next_
            self.prev[next_] = prev_
        self.len -= 1
        
    def to_next(self):
        self.current = self.next[self.current]
        
    def elem(self):
        return self.list[self.current]
    
    def print_(self):
        i = self.head
        data = []
        data_n = []
        while i != None:
            data.append(self.list[i])
            data_n.append(self.next[i])
            i = self.next[i]
        print(data, data_n)
    
        


t = int(input())
for k in range(t):
    s = LinkedList(list(map(int,list(str(input())))))
    res = ''
    n = s.len
    count_masked = 0
    i = 0
    #s.print_()
    while s.len > 0:
        s.to_head()
        cost=1
        max_v=s.elem()
        max_j=-1
        s.to_next()
        while 9 - (s.list[s.head] + cost) > 0 and s.current != None:
            if s.list[s.current] - cost > max_v:
                max_v = s.list[s.current] - cost
                max_j = s.current
            cost+=1
            s.to_next()
        if max_j != -1:
            res+=str(max_v)
            s.pop(max_j)
        else:
            res+=str(s.list[s.head])
            s.pop()
        #s.print_()
    print(res)

Solution 2 (runtime: 312 ms):
import math
from decimal import Decimal, getcontext
import itertools
import queue
from collections import Counter, deque
import bisect
import sys


def list_read(t: type = int):
    return list(map(t, input().split()))


def params_read(t: type = int):
    return map(t, input().split())


def pprint(A):
    if len(A) > 0:
        if type(A[0]) != list:
            A = [A]
    for i in range(len(A)):
        print(*A[i])


def sieve(N):
    primes = [i for i in range(N + 1)]
    primes[1] = 0
    i = 2
    while i <= N:
        if primes[i] != 0:
            j = i + i
            while j <= N:
                primes[j] = 0
                j = j + i
        i += 1
    primes = [i for i in primes if i != 0]

    return primes


def sum_of_the_digits(a):
    return sum(map(int, str(a)))


def binpow(a, n, mod):
    res = 1
    while n != 0:
        if n & 1:
            res = res * a % mod
        a = a * a % mod
        n >>= 1
    return res


def get_next_symbol(a, p=26):
    ABC = list(".abcdefghijklmnopqrstuvwxyz{")
    a = ord(a)
    a += 1
    a = chr(a)
    p = ABC[p + 1]
    if a == p:
        a = "a"
    return a


def Cnk(n, k, MOD):
    if k > n:
        return 0
    if k == 0 or k == n:
        return 1
    k = min(k, n - k)
    c = 1
    for i in range(k):
        c = c * (n - i) // (i + 1)
        c %= MOD
    return c


def alg_sum(a, b, n):
    return int((a + b) / 2 * n)  # int(Decimal(a + b)/2 * n)


def get_factors(n):
    factors = []

    i = 1
    while i * i < n:
        if n % i == 0:
            factors.append(i)
            factors.append(n // i)
        i += 1

    if i * i == n:
        factors.append(i)

    return factors


def factorize(n):
    primes = []
    while n % 2 == 0:
        primes.append(2)
        n //= 2

    i = 3
    while i * i <= n:
        while n % i == 0:
            primes.append(i)
            n //= i
        i += 2

    if n > 1:
        primes.append(n)

    return primes


def fact_for_binom(n, MOD):
    # (f[N] * invf[K] * invf[N - K]) % MOD

    f = [1]
    invf = [1]

    for i in range(1, n):
        f.append(f[-1] * i % MOD)
        f[-1] %= MOD

        invf.append(binpow(f[-1], MOD - 2, MOD))
        invf[-1] %= MOD
    return f, invf


# n = int(input())
# MOD = 1000000007
def solve():
    A = list(input())

    for i in range(len(A)):
        curr_d = int(A[i])
        pos = i
        for j in range(i+1, min(len(A), i + 10)):
            new_d = int(A[j]) - j + i
            if new_d > curr_d:
                curr_d = new_d
                pos = j
        A[pos] = str(curr_d)
        for j in range(pos-1, i-1, -1):
            A[j], A[j+1] = A[j+1], A[j]
    print("".join(A))

def main():
    t = 1
    t = int(input())
    for i in range(t):
        solve()


main()