Your original code iterates over the list in the order of N+1 times - once to generate a set of unique values, and once for each unique value to count the number of occurrences.
Your dictionary code also iterates over the list in the order of N+1 times - once for the outer loop, and once for each unique value to count the number of occurrences. That isn't going to improve the performance at all.
Try something like this:
def calc_count(lst):
d = dict()
for item in lst:
if (item in d):
d[item] = d[item] + 1
else
d[item] = 1
return d
n = int(input())
for i in range(n):
m = int(input())
line = [int(i) for i in input().split()]
dd = calc_count(line)
summa = 0
for item in dd:
cnt = dd[item]
summa += (cnt // 3 * 2 + cnt % 3) * item
print(summa)
That iterates over the list once, either adding or updating the count stored in the dictionary.
NB: Since you don't know the final count when you add or update the dictionary, the formula needs to remain in the code that loops over the final dictionary.