Help me in solving DAA014 problem

My issue

i have got the correct answer but iam gett time limit exceeded while submitting

My code

def count_inversions(arr):
    count = 0
    for i in range(1, len(arr)):
        j = i
        temp = arr[i]
        while j > 0 and arr[j - 1] > temp:
            arr[j] = arr[j - 1]
            j -= 1
            count += 1
        arr[j] = temp
    return count

n = int(input())
for _ in range(n):
    input()  
    arr = list(map(int, input().split()))
    inversions = count_inversions(arr[:]) 
    print(inversions)

Learning course: Analysis and Design of Algorithms
Problem Link: Inversion Count in Analysis and Design of Algorithms