fork download
  1. cases = int(input())
  2. for case in range(cases):
  3. n = int(input())
  4. a = [int(i) for i in input().split()]
  5. x,y = -1,-2
  6. c = 0
  7. while y >= - len(a):
  8. if a[y] < a[x]:
  9. a[y], a[x] = a[x], a[y]
  10. c += 1
  11. break
  12. else:
  13. x -= 1
  14. y -= 1
  15. if c > 0:
  16. if a[x+1] > a[x]:
  17. temp = a[x + 1]
  18. a.remove(a[x + 1])
  19. a.append(temp)
  20. else:
  21. temp = a[x]
  22. a.remove(a[x])
  23. a.append(temp)
  24.  
  25. s = [str(i) for i in a]
  26. res = int("".join(s))
  27. print(res)
  28. else:
  29. print(-1)
Success #stdin #stdout 0.02s 9292KB
stdin
2
5
1 5 4 8 3
10
1 4 7 4 5 8 4 1 2 6
stdout
15834
1474584162