Find the test case causing Runtime in test 3 of equal pairs hard

from collections import Counter

def chk(n):
if n<len(m):
return m[n]
else:
m.append(chk(n-1)+n-1)
return m[n]

def pg(a):
d = Counter(a)
res = 0
k = sorted(d.values())
if len(k) == 1: return chk(k[0])
if 0 in a:
sub = 0
for i in d:
if i==0:
sub = d[i]
break
k.remove(sub)
k[-1] += sub
for i in k:
if i>1:
res += chk(i)
return res

t = int(input())
for i in range(t):
m = [0,0,1,3,6,10]
n = int(input())
a = [0]*n
for j in range(n):
p,q = map(int,input().split())
a[p-1] = q
print(pg(a),end = " ")
print()