fork(1) download
  1. #https://w...content-available-to-author-only...j.com/problems/JNEXT/
  2. t = int(input())
  3.  
  4. for k in range(t):
  5. n = int(input())
  6. no = list(map(int,input().split(" ")))
  7. i=n-1
  8. found = True
  9. while i > 0 and no[i - 1] >= no[i]:
  10. i -= 1
  11. if i <= 0:
  12. found=False
  13.  
  14. if(found==True):
  15. j = len(no) - 1
  16. while no[j] <= no[i - 1]:
  17. j -= 1
  18. no[i - 1], no[j] = no[j], no[i - 1]
  19.  
  20. list1 = no[0:i]
  21. list2 = no[i:n]
  22. list2.sort()
  23. no=list1+list2
  24. no=''.join(str(x) for x in no)
  25. print(no)
  26.  
  27. else:
  28. print(-1)
  29.  
  30.  
  31.  
Success #stdin #stdout 0.01s 27640KB
stdin
1
5
1 5 4 3 8
stdout
15483