#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define mem(a) memset(a,0,sizeof(a))
#define sqr(x) ((x)*(x))

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;

const int mn = 200005;
const int mod = 1e9+7;
#define P mod

int n,k;
vector<ll> v[mn];
set<pll> nus1,nus2,us2,us1,nus3;
int poi[mn];//pointer
ll ans=0;

void ins(int color){
    //printf("ins:%d\n",color);
    int &p=poi[color];
    int u=v[color][p];
    p--;
    int size=sz(v[color]);
    { //nus1
        if (size-1-p>=3){
            auto g=nus1.find({u,color});
            nus1.erase(g);
        }
        if (p!=-1&&size-1-p>=2) nus1.insert({v[color][p],color});
    }
    if (p==size-1-1){
        //printf("erase :color%d %lld\n",color,u+v[color][p]);
        nus2.erase({u+v[color][p],color}); //nus2
        //for(auto &i:nus2) printf(":::%lld %lld\n",i.first,i.second);
    }
    if (p>=1){//nus3
        ll g=1ll*u+v[color][p]+v[color][p-1];
        nus3.erase({g,color});
        if (p>=2) nus3.insert({v[color][p]+v[color][p-1]+v[color][p-2],color});
    }
    if (size-1-p>=3) {
        if (size-1-p>3) us1.erase({v[color][p+2],color});
        us1.insert({u,color}); // us1
    }
    if (size-1-p==3){//us2
        auto g=us2.find({v[color][p+2]+v[color][p+3],color});
        us2.erase(g);
    }
    if (size-1-p==2){
        us2.insert({v[color][p+1]+v[color][p+2],color});
    }
    ans+=u;
}

void del(int color){
    int &p=poi[color];
    p++;
    int u=v[color][p];
    int size=sz(v[color]);
    {//nus1
        if (p!=0&&size-1-p>=1){
            auto g=nus1.find({v[color][p-1],color});
            nus1.erase(g);
        }
        if (size-1-p>=2)nus1.insert({u,color});
    }
    if (p==size-1){
        //printf("insert :color%d\n",color);
        nus2.insert({u+v[color][p-1],color}); //nus2
    }
    if (p+1>=4) nus3.erase({v[color][p-1]+v[color][p-2]+v[color][p-3],color});
    if (p+1>=3){//nus3
        nus3.insert({u+v[color][p-1]+v[color][p-2],color});
    }
    if (size-1-p>=2){//us1
        auto g=us1.find({u,color});
        us1.erase(g);
        if (size-1-p>2) us1.insert({v[color][p+1],color});
    }
    if (size-1-p==2){
        us2.insert({v[color][p+1]+v[color][p+2],color});
    }
    if (size-1-p==1){
        auto g=us2.find({v[color][p]+v[color][p+1],color});
        us2.erase(g);
    }
    ans-=u;
}

ll getA(){
    if (nus1.empty()) return -1e18;
    return nus1.rbegin()->first;
}
void doA(){
    auto u=nus1.end();
    u--;
    ins(u->second);
}

ll getB(){
    if (us1.empty()||nus2.empty()) return -1e18;
    auto u=us1.begin();
    auto v=nus2.rbegin();
    return v->first-u->first;
}
void doB(){
    auto u=*us1.begin();
    auto v=*nus2.rbegin();
    del(u.second);
    ins(v.second);
    ins(v.second);
}

ll getC(){
    //printf("getC: %d %d\n",sz(us2),sz(nus3));
    if (us2.empty()||nus3.empty()) return -1e18;
    auto u=us2.begin();
    auto v=nus3.rbegin();
    return v->first-u->first;
}
void doC(){
    auto u=*us2.begin();
    auto v=*nus3.rbegin();
    del(u.second);
    del(u.second);
    ins(v.second);
    ins(v.second);
    ins(v.second);
}

ll getD(){
    if (us1.empty()||sz(nus2)<2) return -1e18;
    ll ans=0;
    auto u=us1.begin();
    int color=u->second;
    //printf("%d %d\n",poi[color],sz(v[color]));
    for(int i=1;i<=3;i++) ans-=v[color][poi[color]+i];
    auto v=nus2.rbegin();
    ans+=v->first;
    v++;
    ans+=v->first;
    return ans;
}
void doD(){
    if (us2.empty()||nus2.empty()) return;
    auto u=*us2.begin();
    auto v=*nus2.rbegin();
    del(u.second);
    del(u.second);
    ins(v.second);
    ins(v.second);
}

int main() {
#ifdef trote
    freopen("../1.txt", "r", stdin);
#endif
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
        int u,w;
        scanf("%d%d",&u,&w);
        v[u].pb(w);
    }
    for(int i=1;i<=k;i++) {
        sort(v[i].begin(),v[i].end()),poi[i]=sz(v[i])-1;
        nus2.insert({v[i][poi[i]]+v[i][poi[i]-1],i});
        if (sz(v[i])>=3) nus3.insert({v[i].back()+v[i][poi[i]-1]+v[i][poi[i]-2],i});
    }
    for(int i=1;i<=n;i++){
        ll nxtans=-1e17,choose=0;
        if (getA()>nxtans) nxtans=getA(),choose=1;
        if (getB()>nxtans) nxtans=getB(),choose=2;
        if (getC()>nxtans) nxtans=getC(),choose=3;
        if (getD()>nxtans) nxtans=getD(),choose=4;
        //printf("%lld %lld %lld %lld\n",getA(),getB(),getC(),getD());
        //printf("choose:%d\n",choose);
        if (choose==0) {
            puts("-1");
            auto u=*nus2.rbegin();
            ins(u.second);
            ins(u.second);
            i++;
        }
        else if (choose==1) doA();
        else if (choose==2) doB();
        else if (choose==3) doC();
        else doB(),doD();
        printf("%lld\n",ans);
        //printf("--------------------------%d\n",i);
        //for(int i=1;i<=k;i++) printf("%d %d\n",i,sz(v[i])-1-poi[i]);
    }
    return 0;
}

Measured memory_bytes: 19584