from collections import deque def solve(): n = int(input()) plist = list(map(int, input().split())) deq = deque() deq.append(plist[0]) f = plist[0] # for i in range(1, n): # p = plist[i] # if p > f: # deq.append(p) # else: # deq.appendleft(p) # f = p # bit = Bit(n+10) tento = 0 # for i in range(n): # p = deq[i] # tento += i - bit.sum(p) # bit.add(p, 1) print(tento) print(*deq) q = int(input()) while q: q -= 1 solve()