Help me in solving MAXEQUAL problem

My issue

why this approach wrong on this

My code

#include <bits/stdc++.h>
using namespace std;
#define ll long long int



int main() {
	// your code goes here
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        vector<int>v(n,0);
        unordered_map<int,int>map;
        int ma=0;
        for(int i=0;i<n;i++){
            int x,y;
            cin>>x>>y;
            v[x-1]=y;
            map[y]++;
           ll ans=0;
           int ma= max(ma, map[y]);
            for (auto it = map.begin(); it != map.end(); ++it){
                int d=it->second;
                if(d>0){
                    ans+=(d)*1LL*(d-1)/2;
                }
            }
               ans -= ma *1LL* (ma - 1) / 2;
               ma += (n-i-1);
               ans += ma *1LL* (ma - 1) / 2;
              cout<<ans<<" ";
        }
        cout<<endl;
    }
}

Problem Link: Equal Pairs (Hard) Practice Coding Problem