simply and easy way
#include<iostream>
#include<bits/stdc++.h>
#define int long long
using namespace std;
main() {
int test;
cin>>test;
while(test--){
int n=0,m=0;
cin>>n>>m;
int a[m+1];
for(int i=0;i<m+1;i++)
{
a[i]=0;
}
int temp=0,temp2=0;
for(int i=0;i<n;i++)
{
cin>>temp>>temp2;
if(a[temp]<temp2)
{
a[temp]=temp2;
}
}
sort(a+1,a+m+1);
cout<<a[m]+a[m-1]<<endl;
}
return 0;
}