Advertisement
enigmjoe

Cf D

Dec 6th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import sys
  3. from io import BytesIO, IOBase
  4. from collections import deque, defaultdict
  5. import bisect
  6. # from heapq import heappop, heapify
  7. import math
  8. from itertools import accumulate
  9. # from functools import cache
  10.  
  11. def ii():  return int(input())
  12. def si():  return input()
  13. def mi():  return map(int,input().strip().split())
  14. def msi(): return map(str,input().strip().split())
  15. def li():  return list(mi())
  16. MOD = 1000000007
  17.  
  18. def Solve():
  19.     s = input()
  20.     n = len(s)
  21.     arr = list(int(s[i]) for i in range(n))
  22.     for i in range(1, n):
  23.         if arr[i] != 0 and arr[i] > arr[i-1]:
  24.             j, cnt = i, arr[i]
  25.             while j > 0 and cnt > arr[j-1]+1:
  26.                 cnt -= 1
  27.                 arr[j], arr[j-1] = arr[j-1], arr[j]
  28.                 arr[j-1] = cnt
  29.                 j -= 1
  30.     print(''.join([str(arr[i]) for i in range(n)]))
  31.  
  32.  
  33. def main():
  34.     for tt in range(ii()):
  35.        Solve()
  36.  
  37. # region fastio
  38.  
  39. BUFSIZE = 8192
  40.  
  41. class FastIO(IOBase):
  42.     newlines = 0
  43.  
  44.     def __init__(self, file):
  45.         self._file = file
  46.         self._fd = file.fileno()
  47.         self.buffer = BytesIO()
  48.         self.writable = "x" in file.mode or "r" not in file.mode
  49.         self.write = self.buffer.write if self.writable else None
  50.  
  51.     def read(self):
  52.         while True:
  53.             b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
  54.             if not b:
  55.                 break
  56.             ptr = self.buffer.tell()
  57.             self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
  58.         self.newlines = 0
  59.         return self.buffer.read()
  60.  
  61.     def readline(self):
  62.         while self.newlines == 0:
  63.             b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
  64.             self.newlines = b.count(b"\n") + (not b)
  65.             ptr = self.buffer.tell()
  66.             self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
  67.         self.newlines -= 1
  68.         return self.buffer.readline()
  69.  
  70.     def flush(self):
  71.         if self.writable:
  72.             os.write(self._fd, self.buffer.getvalue())
  73.             self.buffer.truncate(0), self.buffer.seek(0)
  74.  
  75.  
  76. class IOWrapper(IOBase):
  77.     def __init__(self, file):
  78.         self.buffer = FastIO(file)
  79.         self.flush = self.buffer.flush
  80.         self.writable = self.buffer.writable
  81.         self.write = lambda s: self.buffer.write(s.encode("ascii"))
  82.         self.read = lambda: self.buffer.read().decode("ascii")
  83.         self.readline = lambda: self.buffer.readline().decode("ascii")
  84.  
  85.  
  86. sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
  87. input = lambda: sys.stdin.readline().rstrip("\r\n")
  88.  
  89.  
  90. # endregion
  91.  
  92. if __name__ == "__main__":
  93.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement