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

ll mod = 1000000007;
ll z = 1000000000;
#define PI 3.14159265
const ll N = 1e5+1;

#define mp                make_pair
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (ll)((x).size())
#define fr                first
#define sc                second
#define piv               pair<ll,vector<ll>>
#define pvi               pair<vector<ll>,ll>
#define pii               pair<ll,ll>
#define vi                vector<ll>
#define vvi               vector<vector<ll>>
#define vp                vector<pair<ll,ll>>
#define rep(i,a,b)        for(ll i=a;i<b;i++)
#define repr(i,a,b)       for(ll i=b-1;i>=a;i--)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;

#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

// #define dbg(x) cerr<<#x<<" - "<<x<<endl;
// #define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";

// ll euclideandis(ll a, ll b)
// {
// 	return sqrt(a*a + b*b);
// }

// bool cmp(const pair<ll,ll>& p1, const pair<ll,ll>& p2)
// {
// 	if(p1.first == p2.first)
// 	{
// 		return (p1.second > p2.second);
// 	}
// 	return (p1.first < p2.first);
// }

// ll sumofdigit(ll a){ll ans = 0;while(a){ans += a%10;a = a/10;}return ans;}

// vi fib(100005);
// void fibo()
// {
// 	fib[0] = 1;fib[1] = 1;fib[2] = 2;
// 	for(int i=3;i<=10000;i++)
// 		fib[i] = (fib[i-1]%mod+fib[i-2]%mod)%mod;
// }

// void DFS(pii end)
// {
// 	queue<pii> q;
// 	q.push(end);
// 	int movex[] = {1,0,-1,0};
// 	int movey[] = {0,1,0,-1};
// 	while(!q.empty())
// 	{
// 		pii p = q.front();
// 		q.pop();
// 		int x = p.fr;
// 		int y = p.sc;
// 		rep(i,0,4)
// 		{
// 			int X = x + movex[i];
// 			int Y = y + movey[i];
// 			if(X>=0 && X<r && Y>=0 && Y<c && !visited[X][Y] && a[X][Y] != 'T')
// 			{
// 				dist[X][Y] = min(dist[X][Y], dist[x][y] + 1);
// 				q.push(mp(X,Y));
// 			}
// 		}
// 	}
// }

// vector<bool> visited(N, false);
// vector<vector<pair<ll,ll>>> adj(N);
// vector<pair<ll,ll>> edges;
// vector<ll> dist(N,INT_MAX);
// void dijkstra(ll start, ll n)
// {
// 	set<pair<ll,ll>> s;
// 	s.insert(mp(0,start));
// 	while(!s.empty())
// 	{
// 		pii p = *s.begin();
// 		s.erase(s.begin());
// 		for(pii u:adj[p.sc])
// 		{
// 			ll to = u.fr;
// 			ll time = u.sc;
// 			if(dist[to] > dist[p.sc]+time)
// 			{
// 				s.erase({dist[to], to});
// 				dist[to] = dist[p.sc]+time;
// 				s.insert({dist[to], to});
// 			}
// 		}
// 	}
// }

// vvi dp(100, vi(100, 0));

// void lcs(string a, string b, ll i, ll j, ll n, ll m)
// {
// 	for(int i=1;i<=n;i++)
// 	{
// 		for(int j=1;j<=m;j++)
// 		{
// 			if(a[i-1] == b[j-1])
// 				dp[i][j] = dp[i-1][j-1] + 1;
// 			else
// 			{
// 				dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
// 			}
// 		}
// 	}
// 	cout<<dp[n][m]<<endl;
// }
// vector<string> ans;
// void findlcs(int i,int j, string str)
// {
// 	if(i == 0 && j == 0)
// 	{
// 		ans.pb(str);
// 		return;
// 	}
// 	if(a[i-1] == b[j-1])
// 	{
// 		string temp = str;
// 		temp += a[i-1];
// 		findlcs(i-1, j-1, temp);
// 	}
// 	else
// 	{
// 		if(dp[i-1][j] == dp[i][j])
// 		{
// 			lcs(i-1,j,str);
// 		}
// 		if(dp[i][j-1] == dp[i][j])
// 		{
// 			lcs(i,j-1,str);
// 		}
// 	}
// }

vp adj;
vi dp(N,0), stops, sum(N,0);

int main()
{
	fio;
	ll t = 1;
	// cin>>t;
	while(t--)
	{
		ll n,h;
		cin>>n>>h;
		vi a(n);
		vp pairs;
		rep(i,0,n)
		{
			cin>>a[i];
			pairs.pb(mp(a[i],i));
		}
		sort(all(pairs));
		ll flag = 0;
		ll mg = (pairs[n-1].fr + pairs[n-2].fr) - (pairs[0].fr + pairs[1].fr);
		ll index = -1;
		ll maxi, mini;
		if(n == 2)
		{
			cout<<0<<endl;
			cout<<1<<" "<<1;
			continue;
		}
		rep(i,0,n-1)
		{
			maxi = pairs[i].fr + pairs[n-1].fr + h;
			mini = pairs[i].fr + pairs[n-1].fr + h;
			maxi = max(maxi,pairs[n-2].fr + pairs[n-1].fr);
			if(i == 0)
			{
				mini = min(mini, pairs[0].fr + pairs[1].fr + h);
				mini = min(mini, pairs[2].fr + pairs[1].fr);
			}
			else
				mini = min(mini, pairs[0].fr + pairs[1].fr);
			ll diff = (maxi-mini);
			if(diff < mg)
			{
				index = i;
				mg = diff;
			}
		}
		if(index == -1)
		{
			cout<<mg<<endl;
			rep(i,0,n)
				cout<<2<<" ";
		}
		else
		{
			cout<<mg<<endl;
			rep(i,0,index+1)
			{
				a[pairs[i].sc] = 2;
				// 	cout<<2<<" ";
			}
			rep(i,index+1,n)
			{
				a[pairs[i].sc] = 1;
					cout<<1<<" ";
			}
			rep(i,0,n)
			{
				cout<<a[i]<<" ";
			}
		}
	}
}


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

ll mod = 1000000007;
ll z = 1000000000;
#define PI 3.14159265
const ll N = 1e5+1;

#define mp                make_pair
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (ll)((x).size())
#define fr                first
#define sc                second
#define piv               pair<ll,vector<ll>>
#define pvi               pair<vector<ll>,ll>
#define pii               pair<ll,ll>
#define vi                vector<ll>
#define vvi               vector<vector<ll>>
#define vp                vector<pair<ll,ll>>
#define rep(i,a,b)        for(ll i=a;i<b;i++)
#define repr(i,a,b)       for(ll i=b-1;i>=a;i--)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;

#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

// #define dbg(x) cerr<<#x<<" - "<<x<<endl;
// #define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";

// ll euclideandis(ll a, ll b)
// {
// 	return sqrt(a*a + b*b);
// }

// bool cmp(const pair<ll,ll>& p1, const pair<ll,ll>& p2)
// {
// 	if(p1.first == p2.first)
// 	{
// 		return (p1.second > p2.second);
// 	}
// 	return (p1.first < p2.first);
// }

// ll sumofdigit(ll a){ll ans = 0;while(a){ans += a%10;a = a/10;}return ans;}

// vi fib(100005);
// void fibo()
// {
// 	fib[0] = 1;fib[1] = 1;fib[2] = 2;
// 	for(int i=3;i<=10000;i++)
// 		fib[i] = (fib[i-1]%mod+fib[i-2]%mod)%mod;
// }

// void DFS(pii end)
// {
// 	queue<pii> q;
// 	q.push(end);
// 	int movex[] = {1,0,-1,0};
// 	int movey[] = {0,1,0,-1};
// 	while(!q.empty())
// 	{
// 		pii p = q.front();
// 		q.pop();
// 		int x = p.fr;
// 		int y = p.sc;
// 		rep(i,0,4)
// 		{
// 			int X = x + movex[i];
// 			int Y = y + movey[i];
// 			if(X>=0 && X<r && Y>=0 && Y<c && !visited[X][Y] && a[X][Y] != 'T')
// 			{
// 				dist[X][Y] = min(dist[X][Y], dist[x][y] + 1);
// 				q.push(mp(X,Y));
// 			}
// 		}
// 	}
// }

// vector<bool> visited(N, false);
// vector<vector<pair<ll,ll>>> adj(N);
// vector<pair<ll,ll>> edges;
// vector<ll> dist(N,INT_MAX);
// void dijkstra(ll start, ll n)
// {
// 	set<pair<ll,ll>> s;
// 	s.insert(mp(0,start));
// 	while(!s.empty())
// 	{
// 		pii p = *s.begin();
// 		s.erase(s.begin());
// 		for(pii u:adj[p.sc])
// 		{
// 			ll to = u.fr;
// 			ll time = u.sc;
// 			if(dist[to] > dist[p.sc]+time)
// 			{
// 				s.erase({dist[to], to});
// 				dist[to] = dist[p.sc]+time;
// 				s.insert({dist[to], to});
// 			}
// 		}
// 	}
// }

// vvi dp(100, vi(100, 0));

// void lcs(string a, string b, ll i, ll j, ll n, ll m)
// {
// 	for(int i=1;i<=n;i++)
// 	{
// 		for(int j=1;j<=m;j++)
// 		{
// 			if(a[i-1] == b[j-1])
// 				dp[i][j] = dp[i-1][j-1] + 1;
// 			else
// 			{
// 				dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
// 			}
// 		}
// 	}
// 	cout<<dp[n][m]<<endl;
// }
// vector<string> ans;
// void findlcs(int i,int j, string str)
// {
// 	if(i == 0 && j == 0)
// 	{
// 		ans.pb(str);
// 		return;
// 	}
// 	if(a[i-1] == b[j-1])
// 	{
// 		string temp = str;
// 		temp += a[i-1];
// 		findlcs(i-1, j-1, temp);
// 	}
// 	else
// 	{
// 		if(dp[i-1][j] == dp[i][j])
// 		{
// 			lcs(i-1,j,str);
// 		}
// 		if(dp[i][j-1] == dp[i][j])
// 		{
// 			lcs(i,j-1,str);
// 		}
// 	}
// }

vp adj;
vi dp(N,0), stops, sum(N,0);

int main()
{
	fio;
	ll t = 1;
	// cin>>t;
	while(t--)
	{
		ll n,h;
		cin>>n>>h;
		vi a(n);
		vp pairs;
		rep(i,0,n)
		{
			cin>>a[i];
			pairs.pb(mp(a[i],i));
		}
		sort(all(pairs));
		ll flag = 0;
		ll mg = (pairs[n-1].fr + pairs[n-2].fr) - (pairs[0].fr + pairs[1].fr);
		ll index = -1;
		ll maxi, mini;
		if(n == 2)
		{
			cout<<0<<endl;
			cout<<1<<" "<<1;
			continue;
		}
		rep(i,0,n-1)
		{
			maxi = pairs[i].fr + pairs[n-1].fr + h;
			mini = pairs[i].fr + pairs[n-1].fr + h;
			maxi = max(maxi,pairs[n-2].fr + pairs[n-1].fr);
			if(i == 0)
			{
				mini = min(mini, pairs[0].fr + pairs[1].fr + h);
				mini = min(mini, pairs[2].fr + pairs[1].fr);
			}
			else
				mini = min(mini, pairs[0].fr + pairs[1].fr);
			ll diff = (maxi-mini);
			if(diff < mg)
			{
				index = i;
				mg = diff;
			}
		}
		if(index == -1)
		{
			cout<<mg<<endl;
			rep(i,0,n)
				cout<<2<<" ";
		}
		else
		{
			cout<<mg<<endl;
			rep(i,0,n)
			{
				if(i == index)
					cout<<1<<" ";
				else
					cout<<2<<" ";
			}
		}
	}
}


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

ll mod = 1000000007;
ll z = 1000000000;
#define PI 3.14159265
const ll N = 1e5+1;

#define mp                make_pair
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (ll)((x).size())
#define fr                first
#define sc                second
#define piv               pair<ll,vector<ll>>
#define pvi               pair<vector<ll>,ll>
#define pii               pair<ll,ll>
#define vi                vector<ll>
#define vvi               vector<vector<ll>>
#define vp                vector<pair<ll,ll>>
#define rep(i,a,b)        for(ll i=a;i<b;i++)
#define repr(i,a,b)       for(ll i=b-1;i>=a;i--)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll

template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}

const long long INF=1e18;

#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

// #define dbg(x) cerr<<#x<<" - "<<x<<endl;
// #define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";

// ll euclideandis(ll a, ll b)
// {
// 	return sqrt(a*a + b*b);
// }

// bool cmp(const pair<ll,ll>& p1, const pair<ll,ll>& p2)
// {
// 	if(p1.first == p2.first)
// 	{
// 		return (p1.second > p2.second);
// 	}
// 	return (p1.first < p2.first);
// }

// ll sumofdigit(ll a){ll ans = 0;while(a){ans += a%10;a = a/10;}return ans;}

// vi fib(100005);
// void fibo()
// {
// 	fib[0] = 1;fib[1] = 1;fib[2] = 2;
// 	for(int i=3;i<=10000;i++)
// 		fib[i] = (fib[i-1]%mod+fib[i-2]%mod)%mod;
// }

// void DFS(pii end)
// {
// 	queue<pii> q;
// 	q.push(end);
// 	int movex[] = {1,0,-1,0};
// 	int movey[] = {0,1,0,-1};
// 	while(!q.empty())
// 	{
// 		pii p = q.front();
// 		q.pop();
// 		int x = p.fr;
// 		int y = p.sc;
// 		rep(i,0,4)
// 		{
// 			int X = x + movex[i];
// 			int Y = y + movey[i];
// 			if(X>=0 && X<r && Y>=0 && Y<c && !visited[X][Y] && a[X][Y] != 'T')
// 			{
// 				dist[X][Y] = min(dist[X][Y], dist[x][y] + 1);
// 				q.push(mp(X,Y));
// 			}
// 		}
// 	}
// }

// vector<bool> visited(N, false);
// vector<vector<pair<ll,ll>>> adj(N);
// vector<pair<ll,ll>> edges;
// vector<ll> dist(N,INT_MAX);
// void dijkstra(ll start, ll n)
// {
// 	set<pair<ll,ll>> s;
// 	s.insert(mp(0,start));
// 	while(!s.empty())
// 	{
// 		pii p = *s.begin();
// 		s.erase(s.begin());
// 		for(pii u:adj[p.sc])
// 		{
// 			ll to = u.fr;
// 			ll time = u.sc;
// 			if(dist[to] > dist[p.sc]+time)
// 			{
// 				s.erase({dist[to], to});
// 				dist[to] = dist[p.sc]+time;
// 				s.insert({dist[to], to});
// 			}
// 		}
// 	}
// }

// vvi dp(100, vi(100, 0));

// void lcs(string a, string b, ll i, ll j, ll n, ll m)
// {
// 	for(int i=1;i<=n;i++)
// 	{
// 		for(int j=1;j<=m;j++)
// 		{
// 			if(a[i-1] == b[j-1])
// 				dp[i][j] = dp[i-1][j-1] + 1;
// 			else
// 			{
// 				dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
// 			}
// 		}
// 	}
// 	cout<<dp[n][m]<<endl;
// }
// vector<string> ans;
// void findlcs(int i,int j, string str)
// {
// 	if(i == 0 && j == 0)
// 	{
// 		ans.pb(str);
// 		return;
// 	}
// 	if(a[i-1] == b[j-1])
// 	{
// 		string temp = str;
// 		temp += a[i-1];
// 		findlcs(i-1, j-1, temp);
// 	}
// 	else
// 	{
// 		if(dp[i-1][j] == dp[i][j])
// 		{
// 			lcs(i-1,j,str);
// 		}
// 		if(dp[i][j-1] == dp[i][j])
// 		{
// 			lcs(i,j-1,str);
// 		}
// 	}
// }

vp adj;
vi dp(N,0), stops, sum(N,0);

int main()
{
	fio;
	ll t = 1;
	// cin>>t;
	while(t--)
	{
		ll n,h;
		cin>>n>>h;
		vi a(n);
		vp pairs;
		rep(i,0,n)
		{
			cin>>a[i];
			pairs.pb(mp(a[i],i));
		}
		sort(all(pairs));
		ll flag = 0;
		ll mg = (pairs[n-1].fr + pairs[n-2].fr) - (pairs[0].fr + pairs[1].fr);
		ll index = -1;
		ll maxi, mini;
		if(n == 2)
		{
			cout<<0<<endl;
			cout<<1<<" "<<1;
			continue;
		}
		rep(i,0,n-1)
		{
			maxi = pairs[i].fr + pairs[n-1].fr + h;
			mini = pairs[i].fr + pairs[n-1].fr + h;
			maxi = max(maxi,pairs[n-2].fr + pairs[n-1].fr);
			if(i == 0)
			{
				mini = min(mini, pairs[0].fr + pairs[1].fr + h);
				mini = min(mini, pairs[2].fr + pairs[1].fr);
			}
			else
				mini = min(mini, pairs[0].fr + pairs[1].fr);
			ll diff = (maxi-mini);
			if(diff < mg)
			{
				index = i;
				mg = diff;
			}
		}
		if(index == -1)
		{
			cout<<mg<<endl;
			rep(i,0,n)
				cout<<2<<" ";
		}
		else
		{
			cout<<mg<<endl;
			rep(i,0,index+1)
			{
				a[pairs[i].sc] = 2;
				// 	cout<<2<<" ";
			}
			rep(i,index+1,n)
			{
				a[pairs[i].sc] = 1;
				// 	cout<<1<<" ";
			}
			rep(i,0,n)
			{
				cout<<a[i]<<" ";
			}
		}
	}
}


#include<bits/stdc++.h>
typedef long long ll;
#define M 1000000007
#define endl "\n"
#define rep(a,b,in) for(int i=a;i<b;i+=in)
#define int long long
using namespace std;
int h;
int max(multiset<int> &a, multiset<int> &b)
{
	int ma=0;
	if(a.size()>=2)
	{
		auto  ptr=a.end();
		int sum=*(--ptr);
		sum+=*(--ptr);
		ma=max(ma,sum);
	}
	if(b.size()>=2)
	{
		auto  ptr=b.end();
		int sum=*(--ptr);
		sum+=*(--ptr);
		ma=max(ma,sum);
	}
	if(a.size()>=1 && b.size()>=1)
	{
		auto  ptr=a.end();
		int sum=*(--ptr);
		ptr=b.end();
		sum+=*(--ptr);
		sum+=h;
		ma=max(ma,sum);
	}
	return ma;
}
int min(multiset<int> &a,multiset<int> &b)
{
	int ma=1e18;
	if(a.size()>=2)
	{
		auto  ptr=a.begin();
		int sum=*(ptr);
		sum+=*(++ptr);
		ma=min(ma,sum);
	}
	if(b.size()>=2)
	{
		auto  ptr=b.begin();
		int sum=*(ptr);
		sum+=*(++ptr);
		ma=min(ma,sum);
	}
	if(a.size()>=1 && b.size()>=1)
	{
		auto  ptr=a.begin();
		int sum=*(ptr);
		ptr=b.begin();
		sum+=*(ptr);
		sum+=h;
		ma=min(ma,sum);
	}
	return ma;
}
int32_t main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    int n;
    cin>>n;
    cin>>h;
    pair<int,int> ar[n];
    for(int i=0;i<n;i++)
    {
    	cin>>ar[i].first;
    	ar[i].second=i;
    }
    sort(ar,ar+n);
    reverse(ar,ar+n);
    int ind1=n-1;
    int ind2=n;

    int ans1[n];
    int ans2[n];
    multiset<int> a1,b1;
    multiset<int> b2,a2;
    for(int i=0;i<ind1;i++)
    {
    	ans1[ar[i].second]=1;
    	a1.insert(ar[i].first);
    }
    for(int i=ind1;i<n;i++)
    {
    	if(i%2==ind1%2)
    	{
    		ans1[ar[i].second]=2;
    		b1.insert(ar[i].first);
    	}
    	else
    	{
    		ans1[ar[i].second]=1;
    		a1.insert(ar[i].first);
    	}
    }
    for(int i=0;i<ind2;i++)
    {
    	ans2[ar[i].second]=1;
    	a2.insert(ar[i].first);
    }
    for(int i=ind2;i<n;i++)
    {
    	if(i%2==ind2%2)
    	{
    		ans2[ar[i].second]=2;
    		b2.insert(ar[i].first);
    	}
    	else
    	{
    		ans2[ar[i].second]=1;
    		a2.insert(ar[i].first);
    	}
    }
    if(max(a1,b1)-min(a1,b1)<max(a2,b2)-min(a2,b2))
    {
    	cout<<max(a1,b1)-min(a1,b1)<<endl;
	    for(int i=0;i<n;i++)
	    {
	    	cout<<ans1[i]<<" ";
	    }
    }
    else
    {
    	cout<<max(a2,b2)-min(a2,b2)<<endl;
	    for(int i=0;i<n;i++)
	    {
	    	cout<<ans2[i]<<" ";
	    }
    }

}


#include<bits/stdc++.h>
typedef long long ll;
#define M 1000000007
#define endl "\n"
#define rep(a,b,in) for(int i=a;i<b;i+=in)
#define int long long
using namespace std;
int h;
int max(multiset<int> &a, multiset<int> &b)
{
	int ma=0;
	if(a.size()>=2)
	{
		auto  ptr=a.end();
		int sum=*(--ptr);
		sum+=*(--ptr);
		ma=max(ma,sum);
	}
	if(b.size()>=2)
	{
		auto  ptr=b.end();
		int sum=*(--ptr);
		sum+=*(--ptr);
		ma=max(ma,sum);
	}
	if(a.size()>=1 && b.size()>=1)
	{
		auto  ptr=a.end();
		int sum=*(--ptr);
		ptr=b.end();
		sum+=*(--ptr);
		sum+=h;
		ma=max(ma,sum);
	}
	return ma;
}
int min(multiset<int> &a,multiset<int> &b)
{
	int ma=1e18;
	if(a.size()>=2)
	{
		auto  ptr=a.begin();
		int sum=*(ptr);
		sum+=*(++ptr);
		ma=min(ma,sum);
	}
	if(b.size()>=2)
	{
		auto  ptr=b.begin();
		int sum=*(ptr);
		sum+=*(++ptr);
		ma=min(ma,sum);
	}
	if(a.size()>=1 && b.size()>=1)
	{
		auto  ptr=a.begin();
		int sum=*(ptr);
		ptr=b.begin();
		sum+=*(ptr);
		sum+=h;
		ma=min(ma,sum);
	}
	return ma;
}
int32_t main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    int n;
    cin>>n;
    cin>>h;
    pair<int,int> ar[n];
    for(int i=0;i<n;i++)
    {
    	cin>>ar[i].first;
    	ar[i].second=i;
    }
    sort(ar,ar+n);
    reverse(ar,ar+n);
    int ind1=n-1;
    int ind2=n;
    for(int i=2;i<n;i++)
    {
    	if(ar[i].first+ar[0].first+h<=ar[0].first+ar[1].first)
    	{
    		ind1=i;
    		ind2=i;
    		break;
    	}
    }
    int ans1[n];
    int ans2[n];
    multiset<int> a1,b1;
    multiset<int> b2,a2;
    for(int i=0;i<ind1;i++)
    {
    	ans1[ar[i].second]=1;
    	a1.insert(ar[i].first);
    }
    for(int i=ind1;i<n;i++)
    {
    	if(i%2==ind1%2)
    	{
    		ans1[ar[i].second]=2;
    		b1.insert(ar[i].first);
    	}
    	else
    	{
    		ans1[ar[i].second]=1;
    		a1.insert(ar[i].first);
    	}
    }
    for(int i=0;i<ind2;i++)
    {
    	ans2[ar[i].second]=1;
    	a2.insert(ar[i].first);
    }
    for(int i=ind2;i<n;i++)
    {
    	if(i%2==ind2%2)
    	{
    		ans2[ar[i].second]=2;
    		b2.insert(ar[i].first);
    	}
    	else
    	{
    		ans2[ar[i].second]=1;
    		a2.insert(ar[i].first);
    	}
    }
    if(max(a1,b1)-min(a1,b1)<max(a2,b2)-min(a2,b2))
    {
    	cout<<max(a1,b1)-min(a1,b1)<<endl;
	    for(int i=0;i<n;i++)
	    {
	    	cout<<ans1[i]<<" ";
	    }
    }
    else
    {
    	cout<<max(a2,b2)-min(a2,b2)<<endl;
	    for(int i=0;i<n;i++)
	    {
	    	cout<<ans2[i]<<" ";
	    }
    }

}


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

#define         pb         push_back
#define         sf(x)      scanf("%d",&x)
#define         sfl(x)     scanf("%lld",&x)
#define         pf(x)      printf("%d\n",x)
#define         pfl(x)     printf("%lld\n",x)
#define         endl       '\n'
#define         pii        pair<int,int>
#define         mapii      map<int,int>
#define         mapll      map<ll,ll>
#define         mapci      map<char,int>
#define         mapcl      map<char,ll>
#define         mapsi      map<string,int>
#define         mapsl      map<string,ll>
#define         pll        pair<ll,ll>
#define         vi         vector<int>
#define         vl         vector<ll>
#define         vd         vector<double,double>
#define         all(c)     c.begin(),c.end()
#define         F          first
#define         S          second
#define         mp         make_pair
#define        ftc(x)      cerr << #x << ": " << x << " " << endl;
#define         PI         acos(-1)
#define         lcm(a,b)   ((a*b)/__gcd(a,b))
#define        optimize  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define        sqr(a)       ((a)*(a))

int main()
{
    ll i,j,k,l,m,n,h;

    cin >> n >> h ;

    l = 1e8;

    ll a[n+10];

    for(i=0; i<n; i++)
    {
        cin >> a[i];
        if(a[i] < l)
        {
            m = i;
            l = a[i];
        }
    }

    sort(a,a+n);

    ll ans = (a[n-1]+a[n-2]) - (a[0]+a[1]);

    if(n == 2)
    {
        cout << 0 << endl;

        cout << 1 << " " << 1 << endl;

        return 0;
    }
    if(ans < max(a[n-1]+a[n-2],h+a[0]+a[n-1])-min(a[1]+a[2],(h+a[0]+a[1])))
    {
        cout << ans << endl;

        for(i = 0 ; i < n ; i ++)
        {
            cout << 1 << " " ;
        }

         cout << endl;
    }
    else
    {

        cout << max(a[n-1]+a[n-2],h+a[0]+a[n-1])-min(a[1]+a[2],(h+a[0]+a[1])) << endl;

        for(i = 0 ; i < n ; i++)
        {
            if(i == m)
            {
                cout << 2 << " ";
            }
            else
                cout << 1 << " ";
        }

        cout << endl;

    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define oo 123456789

int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll n,h;
    cin>>n>>h;
    if(n==2)return cout<<"0\n 1 2\n",0;
    vector<ll>A(n);

    for(int i=0; i<n; i++)
        cin>>A[i];

    auto B = A;
    sort(B.begin(),B.end());

    ll ats1 = B[n-1]+B[n-2] - B[1] - B[0];
    ll ats2 = max(B[n-1]+B[n-2], B[0]+B[n-1]+h) - min(B[0]+B[1]+h, B[1]+B[2]);

    cout<<min(ats1,ats2)<<"\n";

    if(ats1<=ats2)
        for(int i=1; i<=n; i++)
            cout<<"1 ";
    else
        for(int i=1; i<=n; i++)
            if(A[i-1]==B[0])
            {
                B[0]=-1;
                cout<<"2 ";
            }
            else cout<<"1 ";
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> p2;
ll lo,hi,hell=pow(10,16);
void func(vector<p2> &v,ll val){
	ll n=ll(v.size());
	for(ll i=0;i<n;i++)
	for(ll j=i+1;j<n;j++)
	if(v[i].second==v[j].second){
		lo=min(lo,v[i].first+v[j].first);
		hi=max(hi,v[i].first+v[j].first);
	}
	else{
		lo=min(lo,v[i].first+v[j].first+val);
		hi=max(hi,v[i].first+v[j].first+val);
	}
}
void solve(){
	ll n,h,res=hell;
	vector<p2> g;
	cin>>n>>h;
	vector<p2> v(n);
	for(ll i=0;i<n;i++){
	    cin>>v[i].first;
	    v[i].second=1;
    }
	if(n<=6){
		for(ll msk=0;msk<pow(2,n);msk++){
			ll x=msk,cnt=0;
			while(x>0){
				if(x&1)
				v[cnt].second=1;
				else
				v[cnt].second=2;
				x>>=1;
				cnt++;
			}
			while(cnt<n)
			v[cnt++].second=2;
			lo=hell; hi=0;
			func(v,h);
			if(hi-lo<res){
				res=hi-lo;
				g=v;
			}
		}
	}
	else{
	    sort(v.begin(),v.end());
	    lo=hell; hi=0;
	    func(v,h);
	    res=hi-lo;
	    g=v;
	    v[0].second=2;
	    lo=hell; hi=0;
	    func(v,h);
	    if(hi-lo<res){
			res=hi-lo;
			g=v;
		}
    }
	cout<<res<<"\n";
	for(ll i=0;i<n;i++)
	cout<<g[i].second<<" ";
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll qc=1;
	for(ll i=1;i<=qc;i++)
	solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> p2;
ll lo,hi,hell=pow(10,16);
void func(vector<p2> &v,ll val){
	ll n=ll(v.size());
	for(ll i=0;i<n;i++)
	for(ll j=i+1;j<n;j++)
	if(v[i].second==v[j].second){
		lo=min(lo,v[i].first+v[j].first);
		hi=max(hi,v[i].first+v[j].first);
	}
	else{
		lo=min(lo,v[i].first+v[j].first+val);
		hi=max(hi,v[i].first+v[j].first+val);
	}
}
void solve(){
	ll n,h,res=hell;
	vector<p2> g;
	cin>>n>>h;
	vector<p2> v(n);
	for(ll i=0;i<n;i++){
	    cin>>v[i].first;
	    v[i].second=1;
    }
	if(n<=6){
		for(ll msk=0;msk<pow(2,n);msk++){
			ll x=msk,cnt=0;
			while(x>0){
				if(x&1)
				v[cnt].second=1;
				else
				v[cnt].second=2;
				x>>=1;
				cnt++;
			}
			while(cnt<n)
			v[cnt++].second=2;
			lo=hell; hi=0;
			func(v,h);
			if(hi-lo<res){
				res=hi-lo;
				g=v;
			}
		}
	}
	else{
	    lo=hell; hi=0;
	    func(v,h);
	    res=hi-lo;
	    g=v;
	    lo=hell;
	    ll a=0;
	    for(ll i=0;i<n;i++)
	    if(v[i].first<lo){
			lo=v[i].first;
			a=i;
		}
		v[a].second=2;
	    lo=hell; hi=0;
	    func(v,h);
	    if(hi-lo<res){
			res=hi-lo;
			g=v;
		}
    }
	cout<<res<<"\n";
	for(ll i=0;i<n;i++)
	cout<<g[i].second<<" ";
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll qc=1;
	for(ll i=1;i<=qc;i++)
	solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> p2;
ll lo,hi,hell=pow(10,16);
void func(vector<p2> &v,ll val){
	ll n=ll(v.size());
	for(ll i=0;i<n;i++)
	for(ll j=i+1;j<n;j++)
	if(v[i].second==v[j].second){
		lo=min(lo,v[i].first+v[j].first);
		hi=max(hi,v[i].first+v[j].first);
	}
	else{
		lo=min(lo,v[i].first+v[j].first+val);
		hi=max(hi,v[i].first+v[j].first+val);
	}
}
void solve(){
	ll n,h,res=hell;
	cin>>n>>h;
	vector<p2> v(n);
	vector<p2> g(n);
	for(ll i=0;i<n;i++){
	    cin>>v[i].first;
	    v[i].second=i;
	    g[i].second=1;
    }
	if(n<=6){
		for(ll msk=0;msk<pow(2,n);msk++){
			ll x=msk,cnt=0;
			while(x>0){
				if(x&1)
				v[cnt].second=1;
				else
				v[cnt].second=2;
				x>>=1;
				cnt++;
			}
			while(cnt<n)
			v[cnt++].second=2;
			lo=hell; hi=0;
			func(v,h);
			if(hi-lo<res){
				res=hi-lo;
				g=v;
			}
		}
	}
	else{
		sort(v.begin(),v.end());
	    res=v[n-1].first+v[n-2].first-v[1].first-v[0].first;
	    lo=v[2].first+v[1].first; hi=v[n-1].first+v[n-2].first;
	    for(ll i=1;i<n;i++){
			hi=max(hi,v[i].first+v[0].first+h);
			lo=min(lo,v[i].first+v[0].first+h);
		}
	    if(hi-lo<res){
			res=hi-lo;
			g[v[0].second].second=2;
		}
    }
	cout<<res<<"\n";
	for(ll i=0;i<n;i++)
	cout<<g[i].second<<" ";
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll qc=1;
	for(ll i=1;i<=qc;i++)
	solve();
}


#include <bits/stdc++.h>
using namespace std;
#define ve vector
#define fi first
#define se second
#define pa pair
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define ll long long int
#define fr(i, a, b) for (int i = a; i < b; i++)
#define frn(i, a, b) for (int i = a; i > b; i--)
#define mk make_pair
#define pb push_back
const ll mod = 1e9 + 7;
const ll inf = 1e18;
ll n, h;
ve<pa<ll, ll>> arr;
int main(void)
{
    cin >> n >> h;
    arr.resize(n);
    fr(i, 0, n)
    {
        cin >> arr[i].fi;
        arr[i].se = i;
    }
    sort(arr.begin(), arr.end());
    if (n == 2)
    {
        cout << 1 << " " << 1 << endl;
    }
    else if (n == 3)
    {
        ll dif3 = arr[1].fi + arr[2].fi;
        ll dif2 = arr[2].fi + h - arr[0].fi;
        ll dif1 = max(arr[1].fi + arr[2].fi, arr[0].fi + arr[2].fi + h) - min(arr[0].fi + arr[1].fi + h, arr[1].fi + arr[2].fi);
        if (min(dif1, min(dif2, dif3)) == dif1)
        {
            cout << dif1 << endl;
            cout << 1 << " " << 2 << " " << 2 << endl;
        }
        else if (min(dif1, min(dif2, dif3)) == dif2)
        {
            cout << dif2 << endl;
            cout << 1 << " " << 1 << " " << 2 << endl;
        }
        else
        {
            cout << dif3 << endl;
            cout << 1 << " " << 1 << " " << 1 << endl;
        }
    }
    else
    {
        ll diff1 = arr[n - 1].fi + arr[n - 2].fi - arr[0].fi - arr[1].fi;
        ll diff2 = max(arr[n - 1].fi + arr[n - 2].fi, arr[n - 1].fi + arr[0].fi + h) - min(arr[1].fi + arr[2].fi, arr[0].fi + arr[1].fi + h);
        if (diff1 < diff2)
        {
            cout << diff1 << endl;
            fr(i, 0, n)
            {
                cout << 1 << " ";
            }
        }
        else
        {
            cout << diff2 << endl;
            fr(i, 0, n)
            {
                if (i == arr[0].se)
                {
                    cout << 2 << " ";
                }
                else
                {
                    cout << 1 << " ";
                }
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ve vector
#define fi first
#define se second
#define pa pair
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define ll long long int
#define fr(i, a, b) for (int i = a; i < b; i++)
#define frn(i, a, b) for (int i = a; i > b; i--)
#define mk make_pair
#define pb push_back
const ll mod = 1e9 + 7;
const ll inf = 1e18;
ll n, h;
ve<pa<ll, ll>> arr;
int main(void)
{
    cin >> n >> h;
    arr.resize(n);
    fr(i, 0, n)
    {
        cin >> arr[i].fi;
        arr[i].se = i;
    }
    sort(arr.begin(), arr.end());
    if (n == 2)
    {
        cout << 0 << endl;
        cout << 1 << " " << 1 << endl;
    }
    else
    {
        ll diff1 = arr[n - 1].fi + arr[n - 2].fi - arr[0].fi - arr[1].fi;
        ll diff2 = max(arr[n - 1].fi + arr[n - 2].fi, arr[n - 1].fi + arr[0].fi + h) - min(arr[1].fi + arr[2].fi, arr[0].fi + arr[1].fi + h);
        if (diff1 < diff2)
        {
            cout << diff1 << endl;
            fr(i, 0, n)
            {
                cout << 1 << " ";
            }
        }
        else
        {
            cout << diff2 << endl;
            fr(i, 0, n)
            {
                if (i == arr[0].se)
                {
                    cout << 2 << " ";
                }
                else
                {
                    cout << 1 << " ";
                }
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ve vector
#define fi first
#define se second
#define pa pair
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define ll long long int
#define fr(i, a, b) for (int i = a; i < b; i++)
#define frn(i, a, b) for (int i = a; i > b; i--)
#define mk make_pair
#define pb push_back
const ll mod = 1e9 + 7;
const ll inf = 1e18;
ll n, h;
ve<pa<ll, ll>> arr;
int main(void)
{
    cin >> n >> h;
    arr.resize(n);
    fr(i, 0, n)
    {
        cin >> arr[i].fi;
        arr[i].se = i;
    }
    sort(arr.begin(), arr.end());
    if (n == 2)
    {
        cout << 0 << endl;
        cout << 1 << " " << 1 << endl;
    }
    else if (n == 3)
    {
        ll dif3 = arr[1].fi + arr[2].fi;
        ll dif2 = arr[2].fi + h - arr[0].fi;
        ll dif1 = max(arr[1].fi + arr[2].fi, arr[0].fi + arr[2].fi + h) - min(arr[0].fi + arr[1].fi + h, arr[1].fi + arr[2].fi);
        if (min(dif1, min(dif2, dif3)) == dif1)
        {
            cout << dif1 << endl;
            cout << 1 << " " << 2 << " " << 2 << endl;
        }
        else if (min(dif1, min(dif2, dif3)) == dif2)
        {
            cout << dif2 << endl;
            cout << 1 << " " << 1 << " " << 2 << endl;
        }
        else
        {
            cout << dif3 << endl;
            cout << 1 << " " << 1 << " " << 1 << endl;
        }
    }
    else
    {
        ll diff1 = arr[n - 1].fi + arr[n - 2].fi - arr[0].fi - arr[1].fi;
        ll diff2 = max(arr[n - 1].fi + arr[n - 2].fi, arr[n - 1].fi + arr[0].fi + h) - min(arr[1].fi + arr[2].fi, arr[0].fi + arr[1].fi + h);
        if (diff1 < diff2)
        {
            cout << diff1 << endl;
            fr(i, 0, n)
            {
                cout << 1 << " ";
            }
        }
        else
        {
            cout << diff2 << endl;
            fr(i, 0, n)
            {
                if (i == arr[0].se)
                {
                    cout << 2 << " ";
                }
                else
                {
                    cout << 1 << " ";
                }
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ve vector
#define fi first
#define se second
#define pa pair
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define ll long long int
#define fr(i, a, b) for (int i = a; i < b; i++)
#define frn(i, a, b) for (int i = a; i > b; i--)
#define mk make_pair
#define pb push_back
const ll mod = 1e9 + 7;
const ll inf = 1e18;
ll n, h;
ve<pa<ll, ll>> arr;
int main(void)
{
    cin >> n >> h;
    arr.resize(n);
    fr(i, 0, n)
    {
        cin >> arr[i].fi;
        arr[i].se = i;
    }
    sort(arr.begin(), arr.end());
    if (n == 2)
    {
        cout << 1 << " " << 1 << endl;
    }
    else if (n == 3)
    {
        ll dif3 = arr[1].fi + arr[2].fi;
        ll dif2 = arr[2].fi + h - arr[0].fi;
        ll dif1 = max(arr[1].fi + arr[2].fi, arr[0].fi + arr[2].fi + h) - min(arr[0].fi + arr[1].fi + h, arr[1].fi + arr[2].fi);
        if (min(dif1, min(dif2, dif3)) == dif1)
        {
            cout << 1 << " " << 2 << " " << 2 << endl;
        }
        else if (min(dif1, min(dif2, dif3)) == dif2)
        {
            cout << 1 << " " << 1 << " " << 2 << endl;
        }
        else
        {
            cout << 1 << " " << 1 << " " << 1 << endl;
        }
    }
    else
    {
        ll diff1 = arr[n - 1].fi + arr[n - 2].fi - arr[0].fi - arr[1].fi;
        ll diff2 = max(arr[n - 1].fi + arr[n - 2].fi, arr[n - 1].fi + arr[0].fi + h) - min(arr[1].fi + arr[2].fi, arr[0].fi + arr[1].fi + h);
        if (diff1 < diff2)
        {
            cout << diff1 << endl;
            fr(i, 0, n)
            {
                cout << 1 << " ";
            }
        }
        else
        {
            cout << diff2 << endl;
            fr(i, 0, n)
            {
                if (i == arr[0].se)
                {
                    cout << 2 << " ";
                }
                else
                {
                    cout << 1 << " ";
                }
            }
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mx 100002
#define CLR(x) memset(x,0,sizeof(x))
#define f first
#define pll pair<ll,ll>
#define se second
#define pb push_back
#define IO ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
using namespace std;
ll a[mx];
int main(){
    IO;
    ll n,h;
    cin>>n;
    cin>>h;
    ll mi=1e9;
    fr(i,1,n)cin>>a[i],mi=min(mi,a[i]);
    ll ii=1;
    fr(i,1,n)
        if(a[i]==i)ii=i;
    sort(a+1,a+n+1);
    ll ans;
    if(n==2){
        ans=0;
        cout<<ans<<endl;
        cout<<"1 1\n";
    }
    else{
        ll a1=a[1]+a[2];
        ll a2=a[n]+a[n-1];
        ans=a2-a1;
        a1=a[1]+a[2]+h;
        a2=a[2]+a[3];
        ll a3=a[n]+a[n-1];
        ll a4=a[1]+a[n]+h;
        ll a5=max(max(a1,a2),max(a3,a4));
        ll a6=min(min(a1,a2),min(a3,a4));
        if(ans<=(a5-a6)){
            cout<<ans<<endl;
            fr(i,1,n)cout<<"1 ";
        }
        else{
            ans=a5-a6;
            cout<<ans<<endl;
            fr(i,1,n)
                if(i==ii)
                    cout<<"2 ";
                else
                    cout<<"1 ";
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mx 100002
#define CLR(x) memset(x,0,sizeof(x))
#define f first
#define pll pair<ll,ll>
#define se second
#define pb push_back
#define IO ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
using namespace std;
ll a[mx];
int main(){
    IO;
    ll n,h;
    cin>>n;
    cin>>h;
    fr(i,1,n)cin>>a[i];
    sort(a+1,a+n+1);
    ll ans;
    if(n==2){
        ans=0;
        cout<<ans<<endl;
        cout<<"1 1\n";
    }
    else{
        ll a1=a[1]+a[2];
        ll a2=a[n]+a[n-1];
        ans=a2-a1;
        a1=a[1]+a[2]+h;
        a2=a[2]+a[3];
        ll a3=a[n]+a[n-1];
        ll a4=a[1]+a[n]+h;
        ll a5=max(max(a1,a2),max(a3,a4));
        ll a6=min(min(a1,a2),min(a3,a4));
        if(ans<=(a5-a6)){
            cout<<ans<<endl;
            fr(i,1,n)cout<<"1 ";
        }
        else{
            ans=a5-a6;
            cout<<ans<<endl;
            cout<<"1 ";
            fr(i,2,n)cout<<"2 ";
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mx 100002
#define CLR(x) memset(x,0,sizeof(x))
#define f first
#define pll pair<ll,ll>
#define se second
#define pb push_back
#define IO ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
using namespace std;
ll a[mx];
int main(){
    IO;
    ll n,h;
    cin>>n;
    cin>>h;
    fr(i,1,n)cin>>a[i];
    sort(a+1,a+n+1);
    ll ans;
    if(n==2){
        ans=0;
        cout<<ans<<endl;
        cout<<"1 1\n";
    }
    else{
        ll a1=a[1]+a[2];
        ll a2=a[n]+a[n-1];
        ans=a2-a1;
        a1=a[1]+a[2]+h;
        a2=a[2]+a[3];
        ll a3=a[n]+a[n-1];
        ll a4=a[1]+a[n]+h;
        ll a5=max(max(a1,a2),max(a3,a4));
        ll a6=min(min(a1,a2),min(a3,a4));
        if(ans<=(a5-a6)){
            cout<<ans<<endl;
            fr(i,1,n)cout<<"1 ";
        }
        else{
            if(ans==100)cout<<ans<<" "<<(a5-a6)<<endl;
            ans=a5-a6;
            cout<<ans<<endl;
            cout<<"1 ";
            fr(i,2,n)cout<<"2 ";
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mx 100002
#define CLR(x) memset(x,0,sizeof(x))
#define f first
#define pll pair<ll,ll>
#define se second
#define pb push_back
#define IO ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
using namespace std;
ll a[mx];
int main(){
    IO;
    ll n,h;
    cin>>n;
    cin>>h;
    fr(i,1,n)cin>>a[i];
    sort(a+1,a+n+1);
    ll ans;
    if(n==2){
        ans=0;
        cout<<ans<<endl;
        cout<<"1 1\n";
    }
    else{
        ll a1=a[1]+a[2];
        ll a2=a[n]+a[n-1];
        ans=a2-a1;
        a1=a[1]+a[2]+h;
        a2=a[2]+a[3];
        ll a3=a[n]+a[n-1];
        ll a4=a[1]+a[n]+h;
        ll a5=max(max(a1,a2),max(a3,a4));
        ll a6=min(min(a1,a2),min(a3,a4));
        if(n==100)
        cout<<a1<<" "<<a2<<" "<<a3<<" "<<a4<<" "<<a5<<" "<<a6<<" "<<ans<<" "<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[n-1]<<" "<<a[n]<<endl;
        if(ans<a5-a6){
            cout<<ans<<endl;
            fr(i,1,n)cout<<"1 ";
        }
        else{
            ans=min(ans,a5-a6);
            cout<<ans<<endl;
            cout<<"1 ";
            fr(i,2,n)cout<<"2 ";
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mx 100002
#define CLR(x) memset(x,0,sizeof(x))
#define f first
#define pll pair<ll,ll>
#define se second
#define pb push_back
#define IO ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
using namespace std;
ll a[mx];
int main(){
    IO;
    ll n,h;
    cin>>n;
    cin>>h;
    ll mi=1e9;
    fr(i,1,n)cin>>a[i],mi=min(mi,a[i]);
    ll ii=1;
    fr(i,1,n)
        if(a[i]==mi)ii=i;
    sort(a+1,a+n+1);
    ll ans;
    if(n==2){
        ans=0;
        cout<<ans<<endl;
        cout<<"1 1\n";
    }
    else{
        ll a1=a[1]+a[2];
        ll a2=a[n]+a[n-1];
        ans=a2-a1;
        a1=a[1]+a[2]+h;
        a2=a[2]+a[3];
        ll a3=a[n]+a[n-1];
        ll a4=a[1]+a[n]+h;
        ll a5=max(max(a1,a2),max(a3,a4));
        ll a6=min(min(a1,a2),min(a3,a4));
        if(ans<=(a5-a6)){
            cout<<ans<<endl;
            fr(i,1,n)cout<<"1 ";
        }
        else{
            ans=a5-a6;
            cout<<ans<<endl;
            fr(i,1,n)
                if(i==ii)
                    cout<<"2 ";
                else
                    cout<<"1 ";
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mx 100002
#define CLR(x) memset(x,0,sizeof(x))
#define f first
#define pll pair<ll,ll>
#define se second
#define pb push_back
#define IO ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
using namespace std;
ll a[mx];
int main(){
    IO;
    ll n,h;
    cin>>n;
    cin>>h;
    fr(i,1,n)cin>>a[i];
    sort(a+1,a+n+1);
    ll ans;
    if(n==2){
        ans=0;
        cout<<ans<<endl;
        cout<<"1 1\n";
    }
    else{
        ll a1=a[1]+a[2];
        ll a2=a[n]+a[n-1];
        ans=a2-a1;
        a1=a[1]+a[2]+h;
        a2=a[2]+a[3];
        ll a3=a[n]+a[n-1];
        ll a4=a[1]+a[n]+h;
        ll a5=max(max(a1,a2),max(a3,a4));
        ll a6=min(min(a1,a2),min(a3,a4));

        if(ans<a5-a6){
            cout<<ans<<endl;
            fr(i,1,n)cout<<"1 ";
        }
        else{
            ans=min(ans,a5-a6);
            cout<<ans<<endl;
            cout<<"1 ";
            fr(i,2,n)cout<<"2 ";
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
#define fr(i, a, b) for(ll i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mx 100002
#define CLR(x) memset(x,0,sizeof(x))
#define f first
#define pll pair<ll,ll>
#define se second
#define pb push_back
#define IO ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
using namespace std;
ll a[mx];
int main(){
    IO;
    ll n,h;
    cin>>n;
    cin>>h;
    fr(i,1,n)cin>>a[i];
    sort(a+1,a+n+1);
    ll ans;
    if(n==2){
        ans=0;
        cout<<ans<<endl;
        cout<<"1 1\n";
    }
    else{
        ll a1=a[1]+a[2];
        ll a2=a[n]+a[n-1];
        ans=a2-a1;
        a1=a[1]+a[2]+h;
        a2=a[2]+a[3];
        ll a3=a[n]+a[n-1];
        ll a4=a[1]+a[n]+h;
        ll a5=max(max(a1,a2),max(a3,a4));
        ll a6=min(min(a1,a2),min(a3,a4));
        if(ans<=a5-a6){
            cout<<ans<<endl;
            fr(i,1,n)cout<<"1 ";
        }
        else{
            ans=min(ans,a5-a6);
            cout<<ans<<endl;
            cout<<"1 ";
            fr(i,2,n)cout<<"2 ";
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mx 100002
#define CLR(x) memset(x,0,sizeof(x))
#define f first
#define pll pair<ll,ll>
#define se second
#define pb push_back
#define IO ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
using namespace std;
ll a[mx];
int main(){
    IO;
    ll n,h;
    cin>>n;
    cin>>h;
    fr(i,1,n)cin>>a[i];
    sort(a+1,a+n+1);
    ll ans;
    if(n==2){
        ans=0;
        cout<<ans<<endl;
        cout<<"1 1\n";
    }
    else{
        ll a1=a[1]+a[2];
        ll a2=a[n]+a[n-1];
        ans=a2-a1;
        a1=a[1]+a[2]+h;
        a2=a[2]+a[3];
        ll a3=a[n]+a[n-1];
        ll a4=a[1]+a[n]+h;
        ll a5=max(max(a1,a2),max(a3,a4));
        ll a6=min(min(a1,a2),min(a3,a4));
        cout<<ans<<endl;
        if(ans<a5-a6){
            fr(i,1,n)cout<<"1 ";
        }
        else{
            ans=min(ans,a5-a6);
            cout<<"1 ";
            fr(i,2,n)cout<<"2 ";
        }
    }
}


#include<bits/stdc++.h>
#define ll long long
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define REP(i,n) for(int i=0;i<(n);++i)
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define mx 100002
#define CLR(x) memset(x,0,sizeof(x))
#define f first
#define pll pair<ll,ll>
#define se second
#define pb push_back
#define IO ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define endl "\n"
using namespace std;
ll a[mx];
int main(){
    IO;
    ll n,h;
    cin>>n;
    cin>>h;
    fr(i,1,n)cin>>a[i];
    sort(a+1,a+n+1);
    ll ans;
    if(n==2){
        ans=0;
        cout<<ans<<endl;
        cout<<"1 1\n";
    }
    else{
        ll a1=a[1]+a[2];
        ll a2=a[n]+a[n-1];
        ans=a2-a1;
        a1=a[1]+a[2]+h;
        a2=a[2]+a[3];
        ll a3=a[n]+a[n-1];
        ll a4=a[1]+a[n]+h;
        ll a5=max(max(a1,a2),max(a3,a4));
        ll a6=min(min(a1,a2),min(a3,a4));
        if(ans<=(a5-a6)){
            cout<<ans<<endl;
            fr(i,1,n)cout<<"1 ";
        }
        else{
            if(n==100)cout<<ans<<" "<<(a5-a6)<<endl;
            ans=a5-a6;
            cout<<ans<<endl;
            cout<<"1 ";
            fr(i,2,n)cout<<"2 ";
        }
    }
}


#include<bits/stdc++.h>
#define lld long long int
#define ld long double
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define rep(i,a,b)	for(lld i=a;i<=b;i++)
#define repr(i,a,b) for(lld i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
lld arr[300000]={0};
lld brr[300000]={0};
bool type[300000]={0};
bool typpe[300000]={0};
lld dsu[300000];
lld dsu1[300000];
lld rfind(lld dsu[],lld ind){
    if (dsu[ind]==ind) return ind;
    dsu[ind]=rfind(dsu,dsu[ind]);
    return dsu[ind];
}
int main()
{
     ios;
     lld TESTS,n,a,b,p,m,k,q,i,x,y,z,d;
     cin>>TESTS;
     while(TESTS--)
     {
         cin>>n;
         rep(i,0,n){dsu[i]=dsu1[i]=i;arr[i]=brr[i]=0;type[i]=typpe[i]=false;}
         rep(i,0,n-1){
            cin>>a>>b;
            if (b) arr[a]++;
            else brr[a]++;
         }
         rep(i,1,n){
            if (type[arr[i]+brr[i]]){
                lld point=rfind(dsu,arr[i]+brr[i]);
                type[point]=true;
                if (point!=0) dsu[point]=point-1;
            }
            else{
            type[arr[i]+brr[i]]=true;
            if (arr[i]+brr[i]!=0)dsu[arr[i]+brr[i]]=arr[i]+brr[i]-1;
            }
            if (typpe[arr[i]]){
                lld point=rfind(dsu1,arr[i]);
                typpe[point]=true;
                if (point!=0) dsu1[point]=point-1;
            }
            else{
            typpe[arr[i]]=true;
            if (arr[i]!=0)dsu1[arr[i]]=arr[i]-1;
            }
         }
         lld ans=0,ans1=0;
         rep(i,1,n){
            if (type[i]) ans+=i;
            if (typpe[i]) ans1+=i;
         }
         cout<<ans<<" "<<ans1<<"\n";
     }
    return 0;
}


// >>> TEMPLATES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using i32 = int32_t;
using i64 = int64_t;
#define int ll
#define double ld
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define rep1(i,n) for (int i = 1; i <= (int)(n); i++)
#define repR(i,n) for (int i = (int)(n)-1; i >= 0; i--)
#define rep1R(i,n) for (int i = (int)(n); i >= 1; i--)
#define loop(i,a,B) for (int i = a; i B; i++)
#define loopR(i,a,B) for (int i = a; i B; i--)
#define all(x) (x).begin(), (x).end()
#define allR(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second
#define INF32 (numeric_limits<int32_t>::max()/2-1)
#define INF64 (numeric_limits<int64_t>::max()/2-1)
#define INF (numeric_limits<int>::max()/2-1)
#ifdef LOCAL
#include "debug.hpp"
#define dump(...) cerr << "[" << setw(3) << __LINE__ << ":" << __FUNCTION__ << "] ", dump_impl(#__VA_ARGS__, __VA_ARGS__)
#define say(x) cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] " << x << endl
#define debug if (1)
#else
#define dump(...) (void)(0)
#define say(x) (void)(0)
#define debug if (0)
#endif
template <class T> using pque_max = priority_queue<T>;
template <class T> using pque_min = priority_queue<T, vector<T>, greater<T> >;
template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type>
ostream& operator<<(ostream& os, T const& v) { bool f = true; for (auto const& x : v) os << (f ? "" : " ") << x, f = false; return os; }
template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type>
istream& operator>>(istream& is, T &v) { for (auto& x : v) is >> x; return is; }
template <class T, class S> istream& operator>>(istream& is, pair<T,S>& p) { return is >> p.first >> p.second; }
struct IOSetup { IOSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } iosetup;
template <class F> struct FixPoint : private F {
    constexpr FixPoint(F&& f) : F(forward<F>(f)) {}
    template <class... T> constexpr auto operator()(T&&... x) const { return F::operator()(*this, forward<T>(x)...); }
};
struct MakeFixPoint {
    template <class F> constexpr auto operator|(F&& f) const { return FixPoint<F>(forward<F>(f)); }
};
#define MFP MakeFixPoint()|
#define def(name, ...) auto name = MFP [&](auto &&name, __VA_ARGS__)
template <class T, size_t d> struct vec_impl {
    using type = vector<typename vec_impl<T,d-1>::type>;
    template <class... U> static type make_v(size_t n, U&&... x) { return type(n, vec_impl<T,d-1>::make_v(forward<U>(x)...)); }
};
template <class T> struct vec_impl<T,0> { using type = T; static type make_v(T const& x = {}) { return x; } };
template <class T, size_t d = 1> using vec = typename vec_impl<T,d>::type;
template <class T, size_t d = 1, class... Args> auto make_v(Args&&... args) { return vec_impl<T,d>::make_v(forward<Args>(args)...); }
template <class T> void quit(T const& x) { cout << x << endl; exit(0); }
template <class T> constexpr bool chmin(T& x, T const& y) { if (x > y) { x = y; return true; } return false; }
template <class T> constexpr bool chmax(T& x, T const& y) { if (x < y) { x = y; return true; } return false; }
template <class It> constexpr auto sumof(It b, It e) { return accumulate(b,e,typename iterator_traits<It>::value_type{}); }
template <class T> int sz(T const& x) { return x.size(); }
template <class C, class T> int lbd(C const& v, T const& x) {
    return lower_bound(v.begin(), v.end(), x)-v.begin();
}
template <class C, class T> int ubd(C const& v, T const& x) {
    return upper_bound(v.begin(), v.end(), x)-v.begin();
}
template <class C, class F> int ppt(C const& v, F f) {
    return partition_point(v.begin(), v.end(), f)-v.begin();
}
// <<<

void solve() {
    int n; cin >> n;
    vector<int> a(n),f(n);
    rep (i,n) cin >> a[i] >> f[i], a[i]--;

    vector<pair<int,int>> cnt(n);
    rep (i,n) {
        cnt[a[i]].fst++;
        if (f[i]) cnt[a[i]].snd++;
    }
    sort(all(cnt));

    pque_max<int> q;
    int x = 0, y = 0;
    rep1R (k,n) {
        while (cnt.size() && cnt.back().fst >= k) {
            q.push(cnt.back().snd);
            cnt.pop_back();
        }
        dump(k,cnt);
        if (!q.empty()) {
            int z = q.top(); q.pop();
            dump(k,z);
            x += k;
            y += min(k,z);
        }
    }

    cout << x << " " << y << "\n";

    cerr << endl;

}

int32_t main() {
    int q; cin >> q;
    while (q--) solve();
}


// >>> TEMPLATES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using i32 = int32_t;
using i64 = int64_t;
#define int ll
#define double ld
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define rep1(i,n) for (int i = 1; i <= (int)(n); i++)
#define repR(i,n) for (int i = (int)(n)-1; i >= 0; i--)
#define rep1R(i,n) for (int i = (int)(n); i >= 1; i--)
#define loop(i,a,B) for (int i = a; i B; i++)
#define loopR(i,a,B) for (int i = a; i B; i--)
#define all(x) (x).begin(), (x).end()
#define allR(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second
#define INF32 (numeric_limits<int32_t>::max()/2-1)
#define INF64 (numeric_limits<int64_t>::max()/2-1)
#define INF (numeric_limits<int>::max()/2-1)
#ifdef LOCAL
#include "debug.hpp"
#define dump(...) cerr << "[" << setw(3) << __LINE__ << ":" << __FUNCTION__ << "] ", dump_impl(#__VA_ARGS__, __VA_ARGS__)
#define say(x) cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] " << x << endl
#define debug if (1)
#else
#define dump(...) (void)(0)
#define say(x) (void)(0)
#define debug if (0)
#endif
template <class T> using pque_max = priority_queue<T>;
template <class T> using pque_min = priority_queue<T, vector<T>, greater<T> >;
template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type>
ostream& operator<<(ostream& os, T const& v) { bool f = true; for (auto const& x : v) os << (f ? "" : " ") << x, f = false; return os; }
template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type>
istream& operator>>(istream& is, T &v) { for (auto& x : v) is >> x; return is; }
template <class T, class S> istream& operator>>(istream& is, pair<T,S>& p) { return is >> p.first >> p.second; }
struct IOSetup { IOSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } iosetup;
template <class F> struct FixPoint : private F {
    constexpr FixPoint(F&& f) : F(forward<F>(f)) {}
    template <class... T> constexpr auto operator()(T&&... x) const { return F::operator()(*this, forward<T>(x)...); }
};
struct MakeFixPoint {
    template <class F> constexpr auto operator|(F&& f) const { return FixPoint<F>(forward<F>(f)); }
};
#define MFP MakeFixPoint()|
#define def(name, ...) auto name = MFP [&](auto &&name, __VA_ARGS__)
template <class T, size_t d> struct vec_impl {
    using type = vector<typename vec_impl<T,d-1>::type>;
    template <class... U> static type make_v(size_t n, U&&... x) { return type(n, vec_impl<T,d-1>::make_v(forward<U>(x)...)); }
};
template <class T> struct vec_impl<T,0> { using type = T; static type make_v(T const& x = {}) { return x; } };
template <class T, size_t d = 1> using vec = typename vec_impl<T,d>::type;
template <class T, size_t d = 1, class... Args> auto make_v(Args&&... args) { return vec_impl<T,d>::make_v(forward<Args>(args)...); }
template <class T> void quit(T const& x) { cout << x << endl; exit(0); }
template <class T> constexpr bool chmin(T& x, T const& y) { if (x > y) { x = y; return true; } return false; }
template <class T> constexpr bool chmax(T& x, T const& y) { if (x < y) { x = y; return true; } return false; }
template <class It> constexpr auto sumof(It b, It e) { return accumulate(b,e,typename iterator_traits<It>::value_type{}); }
template <class T> int sz(T const& x) { return x.size(); }
template <class C, class T> int lbd(C const& v, T const& x) {
    return lower_bound(v.begin(), v.end(), x)-v.begin();
}
template <class C, class T> int ubd(C const& v, T const& x) {
    return upper_bound(v.begin(), v.end(), x)-v.begin();
}
template <class C, class F> int ppt(C const& v, F f) {
    return partition_point(v.begin(), v.end(), f)-v.begin();
}
// <<<

void solve() {
    int n; cin >> n;
    vector<int> a(n),f(n);
    rep (i,n) cin >> a[i] >> f[i], a[i]--;

    int ans = [&]() {
        vector<int> cnt(n);
        rep (i,n) cnt[a[i]]++;
        sort(allR(cnt));

        int val = INF;
        rep (i,n) {
            chmin(cnt[i], val-1);
            val = cnt[i];
        }
        int ans = 0;
        rep (i,n) if (cnt[i] > 0) ans += cnt[i];
        return ans;
    }();

    vector<pair<int,int>> cnt(n);
    rep (i,n) {
        cnt[a[i]].fst++;
        if (f[i] == 0) cnt[a[i]].snd++;
    }
    dump(cnt);

    auto check = [&](int x) {
        dump(x);
        pque_max<pair<int,int>> pq;
        vector<pair<int,int>> tmp;
        rep (i,n) pq.push(cnt[i]);
        while (x--) {
            auto p = pq.top(); pq.pop();
            dump(p);
            if (p.snd == 0) {
                tmp.eb(p);
                x++;
                continue;
            }
            p.fst--, p.snd--;
            assert(p.fst >= 0 && p.snd >= 0);
            pq.push(p);
        }
        for (auto p : tmp) pq.push(p);

        int val = INF;
        int sum = 0;
        rep (i,n) {
            auto p = pq.top(); pq.pop();
            chmin(p.fst, val-1);
            if (p.fst < 0) break;
            val = p.fst;
            sum += p.fst;
        }
        assert(sum <= ans);
        return sum;
    };

    int total_zero = count(all(f),0);
    dump(total_zero);
    int l = 0, r = total_zero+1;
    while (l+1 < r) {
        int mid = (l+r)/2;
        dump(l,r,mid);
        int val = check(mid);
        dump(mid,val);
        (val == ans ? l : r) = mid;
    }
    dump(l,r);
    cout << ans << " " << ans-(total_zero-l) << "\n";

}

int32_t main() {
    int q; cin >> q;
    while (q--) solve();
}


// >>> TEMPLATES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using i32 = int32_t;
using i64 = int64_t;
#define int ll
#define double ld
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define rep1(i,n) for (int i = 1; i <= (int)(n); i++)
#define repR(i,n) for (int i = (int)(n)-1; i >= 0; i--)
#define rep1R(i,n) for (int i = (int)(n); i >= 1; i--)
#define loop(i,a,B) for (int i = a; i B; i++)
#define loopR(i,a,B) for (int i = a; i B; i--)
#define all(x) (x).begin(), (x).end()
#define allR(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second
#define INF32 (numeric_limits<int32_t>::max()/2-1)
#define INF64 (numeric_limits<int64_t>::max()/2-1)
#define INF (numeric_limits<int>::max()/2-1)
#ifdef LOCAL
#include "debug.hpp"
#define dump(...) cerr << "[" << setw(3) << __LINE__ << ":" << __FUNCTION__ << "] ", dump_impl(#__VA_ARGS__, __VA_ARGS__)
#define say(x) cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] " << x << endl
#define debug if (1)
#else
#define dump(...) (void)(0)
#define say(x) (void)(0)
#define debug if (0)
#endif
template <class T> using pque_max = priority_queue<T>;
template <class T> using pque_min = priority_queue<T, vector<T>, greater<T> >;
template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type>
ostream& operator<<(ostream& os, T const& v) { bool f = true; for (auto const& x : v) os << (f ? "" : " ") << x, f = false; return os; }
template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type>
istream& operator>>(istream& is, T &v) { for (auto& x : v) is >> x; return is; }
template <class T, class S> istream& operator>>(istream& is, pair<T,S>& p) { return is >> p.first >> p.second; }
struct IOSetup { IOSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } iosetup;
template <class F> struct FixPoint : private F {
    constexpr FixPoint(F&& f) : F(forward<F>(f)) {}
    template <class... T> constexpr auto operator()(T&&... x) const { return F::operator()(*this, forward<T>(x)...); }
};
struct MakeFixPoint {
    template <class F> constexpr auto operator|(F&& f) const { return FixPoint<F>(forward<F>(f)); }
};
#define MFP MakeFixPoint()|
#define def(name, ...) auto name = MFP [&](auto &&name, __VA_ARGS__)
template <class T, size_t d> struct vec_impl {
    using type = vector<typename vec_impl<T,d-1>::type>;
    template <class... U> static type make_v(size_t n, U&&... x) { return type(n, vec_impl<T,d-1>::make_v(forward<U>(x)...)); }
};
template <class T> struct vec_impl<T,0> { using type = T; static type make_v(T const& x = {}) { return x; } };
template <class T, size_t d = 1> using vec = typename vec_impl<T,d>::type;
template <class T, size_t d = 1, class... Args> auto make_v(Args&&... args) { return vec_impl<T,d>::make_v(forward<Args>(args)...); }
template <class T> void quit(T const& x) { cout << x << endl; exit(0); }
template <class T> constexpr bool chmin(T& x, T const& y) { if (x > y) { x = y; return true; } return false; }
template <class T> constexpr bool chmax(T& x, T const& y) { if (x < y) { x = y; return true; } return false; }
template <class It> constexpr auto sumof(It b, It e) { return accumulate(b,e,typename iterator_traits<It>::value_type{}); }
template <class T> int sz(T const& x) { return x.size(); }
template <class C, class T> int lbd(C const& v, T const& x) {
    return lower_bound(v.begin(), v.end(), x)-v.begin();
}
template <class C, class T> int ubd(C const& v, T const& x) {
    return upper_bound(v.begin(), v.end(), x)-v.begin();
}
template <class C, class F> int ppt(C const& v, F f) {
    return partition_point(v.begin(), v.end(), f)-v.begin();
}
// <<<

void solve() {
    int n; cin >> n;
    vector<int> a(n),f(n);
    rep (i,n) cin >> a[i] >> f[i], a[i]--;

    int ans = [&]() {
        vector<int> cnt(n);
        rep (i,n) cnt[a[i]]++;
        sort(allR(cnt));

        int val = INF;
        rep (i,n) {
            chmin(cnt[i], val-1);
            val = cnt[i];
        }
        int ans = 0;
        rep (i,n) if (cnt[i] > 0) ans += cnt[i];
        return ans;
    }();

    vector<pair<int,int>> cnt(n);
    rep (i,n) {
        cnt[a[i]].fst++;
        if (f[i] == 0) cnt[a[i]].snd++;
    }
    dump(cnt);

    auto check = [&](int x) {
        dump(x);
        pque_max<pair<int,int>> pq;
        vector<pair<int,int>> tmp;
        rep (i,n) pq.push(cnt[i]);
        while (x--) {
            auto p = pq.top(); pq.pop();
            dump(p);
            if (p.snd == 0) {
                tmp.eb(p);
                x++;
                continue;
            }
            p.fst--, p.snd--;
            assert(p.fst >= 0 && p.snd >= 0);
            pq.push(p);
        }
        for (auto p : tmp) pq.push(p);

        int val = INF;
        int sum = 0;
        rep (i,n) {
            auto p = pq.top(); pq.pop();
            chmin(p.fst, val-1);
            if (p.fst < 0) break;
            val = p.fst;
            sum += p.fst;
        }
        assert(sum <= ans);
        return sum;
    };

    int total_zero = count(all(f),0);
    dump(total_zero);
    int l = 0, r = total_zero+1;
    while (l+1 < r) {
        int mid = (l+r)/2;
        dump(l,r,mid);
        int val = check(mid);
        dump(mid,val);
        (val == ans ? l : r) = mid;
    }
    dump(l,r);
    assert(check(l) == ans);
    cout << ans << " " << ans-(total_zero-l) << "\n";

}

int32_t main() {
    int q; cin >> q;
    while (q--) solve();
}


// >>> TEMPLATES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using i32 = int32_t;
using i64 = int64_t;
#define int ll
#define double ld
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define rep1(i,n) for (int i = 1; i <= (int)(n); i++)
#define repR(i,n) for (int i = (int)(n)-1; i >= 0; i--)
#define rep1R(i,n) for (int i = (int)(n); i >= 1; i--)
#define loop(i,a,B) for (int i = a; i B; i++)
#define loopR(i,a,B) for (int i = a; i B; i--)
#define all(x) (x).begin(), (x).end()
#define allR(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second
#define INF32 (numeric_limits<int32_t>::max()/2-1)
#define INF64 (numeric_limits<int64_t>::max()/2-1)
#define INF (numeric_limits<int>::max()/2-1)
#ifdef LOCAL
#include "debug.hpp"
#define dump(...) cerr << "[" << setw(3) << __LINE__ << ":" << __FUNCTION__ << "] ", dump_impl(#__VA_ARGS__, __VA_ARGS__)
#define say(x) cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] " << x << endl
#define debug if (1)
#else
#define dump(...) (void)(0)
#define say(x) (void)(0)
#define debug if (0)
#endif
template <class T> using pque_max = priority_queue<T>;
template <class T> using pque_min = priority_queue<T, vector<T>, greater<T> >;
template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type>
ostream& operator<<(ostream& os, T const& v) { bool f = true; for (auto const& x : v) os << (f ? "" : " ") << x, f = false; return os; }
template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type>
istream& operator>>(istream& is, T &v) { for (auto& x : v) is >> x; return is; }
template <class T, class S> istream& operator>>(istream& is, pair<T,S>& p) { return is >> p.first >> p.second; }
struct IOSetup { IOSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } iosetup;
template <class F> struct FixPoint : private F {
    constexpr FixPoint(F&& f) : F(forward<F>(f)) {}
    template <class... T> constexpr auto operator()(T&&... x) const { return F::operator()(*this, forward<T>(x)...); }
};
struct MakeFixPoint {
    template <class F> constexpr auto operator|(F&& f) const { return FixPoint<F>(forward<F>(f)); }
};
#define MFP MakeFixPoint()|
#define def(name, ...) auto name = MFP [&](auto &&name, __VA_ARGS__)
template <class T, size_t d> struct vec_impl {
    using type = vector<typename vec_impl<T,d-1>::type>;
    template <class... U> static type make_v(size_t n, U&&... x) { return type(n, vec_impl<T,d-1>::make_v(forward<U>(x)...)); }
};
template <class T> struct vec_impl<T,0> { using type = T; static type make_v(T const& x = {}) { return x; } };
template <class T, size_t d = 1> using vec = typename vec_impl<T,d>::type;
template <class T, size_t d = 1, class... Args> auto make_v(Args&&... args) { return vec_impl<T,d>::make_v(forward<Args>(args)...); }
template <class T> void quit(T const& x) { cout << x << endl; exit(0); }
template <class T> constexpr bool chmin(T& x, T const& y) { if (x > y) { x = y; return true; } return false; }
template <class T> constexpr bool chmax(T& x, T const& y) { if (x < y) { x = y; return true; } return false; }
template <class It> constexpr auto sumof(It b, It e) { return accumulate(b,e,typename iterator_traits<It>::value_type{}); }
template <class T> int sz(T const& x) { return x.size(); }
template <class C, class T> int lbd(C const& v, T const& x) {
    return lower_bound(v.begin(), v.end(), x)-v.begin();
}
template <class C, class T> int ubd(C const& v, T const& x) {
    return upper_bound(v.begin(), v.end(), x)-v.begin();
}
template <class C, class F> int ppt(C const& v, F f) {
    return partition_point(v.begin(), v.end(), f)-v.begin();
}
// <<<

void solve() {
    int n; cin >> n;
    vector<int> a(n),f(n);
    rep (i,n) cin >> a[i] >> f[i], a[i]--;

    vector<pair<int,int>> cnt(n);
    rep (i,n) {
        cnt[a[i]].fst++;
        if (f[i]) cnt[a[i]].snd++;
    }
    sort(all(cnt));

    pque_max<int> q;
    int x = 0, y = 0;
    rep1R (k,n) {
        while (cnt.size() && cnt.back().fst >= k) {
            q.push(cnt.back().snd);
            cnt.pop_back();
        }
        dump(k,cnt);
        if (!q.empty()) {
            int z = q.top(); q.pop();
            dump(k,z);
            x += k;
            y += min(k,z);
        }
    }

    cout << x << " " << y << "\n";

    debug { cerr << endl; }

}

int32_t main() {
    int q; cin >> q;
    while (q--) solve();
}


#include<bits/stdc++.h>
using namespace std;
#define C            1000000007
#define MOD          998244353
#define int          long long
#define D            double
#define S            second
#define F            first
#define pb           push_back
#define ff           fflush(stdout)
#define ppb          pop_back()
#define B(c)         (c).begin()
#define E(c)         (c).end()
#define all(c)       (c).begin(),(c).end()
#define rall(c)      (c).rbegin(),(c).rend()
#define SORT(v)       sort(ALL(v))
#define DB            cerr<<"move"<<endl;
#define DV(x)        cerr<<x<<endl;
#define inputarr(a,n)  for(int i=0;i<n;++i) cin>>a[i]
#define printarr(a,n)  for(int i=0;i<n;++i) cout<<a[i]<<" "
#define lb           lower_bound
#define ub           upper_bound
#define si(c)        (int)((c).size())
#define gcd(a,b)      __gcd(a,b)
#define lcm(a,b)     (a*(b/gcd(a,b)))
#define sqr(a)       ((a) * (a))
#define optimize()   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef pair<int,int>                         pii;
typedef pair<int,pii>                         pipii;
typedef vector<vector<int> >                  vvi;
typedef map<int,int>                          mp;
typedef map<string,int>                       msi;
typedef priority_queue<int, vector<int>, greater<int> > pq;
typedef vector<int>::iterator                  vit;
typedef set<int>::iterator                     sit;
typedef map<int,int>::iterator                 mit;
typedef multiset<int>::iterator                msit;
inline  bool checkBit(int n, int i) { return n&((int)1<<i); }
inline  int setBit(int n, int i) { return n|((int)1<<i);; }
inline  int resetBit(int n, int i) { return n&(~((int)1<<i)); }
inline  bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline  bool isLeapYear(int year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline  void normal(int &a) { a = (a+MOD)%MOD; }
inline  int modMul(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline  int modAdd(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline  int modSub(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline  int modPow(int b, int p) { int r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline  int modInverse(int a) { return modPow(a, MOD-2); }
inline  int modDiv(int a,int b) { return modMul(a, modInverse(b)); }
/*const int N=5e5+1;
int prime[N];*/
int ans=0;
int fact(int n){
	if(n==0)return 0;
	return (n==1?1:(n*fact(n-1))%C);
}/*
void P(){
for(int i=2;i<=N;i++){
	    if(!prime[i]){
		for(int j=2*i;j<=N;j+=i){
		prime[j]=i;}
		}
	}
}
int ctoi(char c){
	return (int)(c-'0');
}
void find_subset(int n){
	for(int i=1;i<((int)1<<n);i++){
		for(int j=0;j<n;j++){
			if(checkBit(i,j)){
				//do what you want;
			}
		}
	}
}*/

void solve(){
   int n,a,e;cin>>n;
   mp m,eat;
   for(int i=0;i<n;i++){
   	  cin>>a>>e;
   	  m[a]++;
   	  if(e)eat[a]++;
   }
   map<int,int>ans;
   vector<pii>v;
   for(mit it=B(m);it!=E(m);it++){
   	   v.pb({m[(*it).F],eat[(*it).F]});
   }
   sort(rall(v));
   int k=v[0].F;
   ans[v[0].F]=min(v[0].S,v[0].F);
   bool flag=1;
   multiset<int>s;
   for(int i=1;i<si(v);i++){if(k>1){
   flag=1;
        if(si(s)&&v[i].F==k-1){
        	msit it=E(s);it--;
        	ans[k-1]=min(k-1,*it);
		}
   	    while(i<si(v)&&v[i].F>=k-1){
   	    	flag=0;
   	    	s.insert(v[i].S);
   	    	ans[k-1]=min(max(ans[k-1],v[i].S),k-1);
   	    	i++;
		}
		if(flag){
			int l=v[i].F,l1=v[i].S;
			while(k-1>v[i].F&&si(s)){
				msit it=E(s);it--;
				ans[k-1]=min(max(*it,ans[k-1]),k-1);
				k--;s.erase(it);
			}
			while(i<si(v)&&l==v[i].F)s.insert(v[i].S),i++;i--;
			msit it=E(s);it--;
			ans[v[i].F]=min(max(*it,ans[v[i].F]),v[i].F);k=v[i].F;
			s.erase(it);
		}
		else if(si(s)){
			msit it=E(s);it--;
			s.erase(it);k--;i--;
		}}
   }
   while(si(s)&&k>1){
   	   msit it=E(s);it--;
   	   ans[k-1]=min(max(*it,ans[k-1]),k-1);k--;s.erase(it);
   }
   int sum1=0,sum2=0;
   for(mit it=B(ans);it!=E(ans);it++){
   	   sum1+=(*it).F,sum2+=(*it).S;
   }
   cout<<sum1<<" "<<sum2<<endl;
}
signed main(){
    optimize();
	int t=1;
	//P();
    cin>>t;
	while(t--){
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define C            1000000007
#define MOD          998244353
#define int          long long
#define D            double
#define S            second
#define F            first
#define pb           push_back
#define ff           fflush(stdout)
#define ppb          pop_back()
#define B(c)         (c).begin()
#define E(c)         (c).end()
#define all(c)       (c).begin(),(c).end()
#define rall(c)      (c).rbegin(),(c).rend()
#define SORT(v)       sort(ALL(v))
#define DB            cerr<<"move"<<endl;
#define DV(x)        cerr<<x<<endl;
#define inputarr(a,n)  for(int i=0;i<n;++i) cin>>a[i]
#define printarr(a,n)  for(int i=0;i<n;++i) cout<<a[i]<<" "
#define lb           lower_bound
#define ub           upper_bound
#define si(c)        (int)((c).size())
#define gcd(a,b)      __gcd(a,b)
#define lcm(a,b)     (a*(b/gcd(a,b)))
#define sqr(a)       ((a) * (a))
#define optimize()   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef pair<int,int>                         pii;
typedef pair<int,pii>                         pipii;
typedef vector<vector<int> >                  vvi;
typedef map<int,int>                          mp;
typedef map<string,int>                       msi;
typedef priority_queue<int, vector<int>, greater<int> > pq;
typedef vector<int>::iterator                  vit;
typedef set<int>::iterator                     sit;
typedef map<int,int>::iterator                 mit;
typedef multiset<int>::iterator                msit;
inline  bool checkBit(int n, int i) { return n&((int)1<<i); }
inline  int setBit(int n, int i) { return n|((int)1<<i);; }
inline  int resetBit(int n, int i) { return n&(~((int)1<<i)); }
inline  bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline  bool isLeapYear(int year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline  void normal(int &a) { a = (a+MOD)%MOD; }
inline  int modMul(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline  int modAdd(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline  int modSub(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline  int modPow(int b, int p) { int r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline  int modInverse(int a) { return modPow(a, MOD-2); }
inline  int modDiv(int a,int b) { return modMul(a, modInverse(b)); }
/*const int N=5e5+1;
int prime[N];*/
int ans=0;
int fact(int n){
	if(n==0)return 0;
	return (n==1?1:(n*fact(n-1))%C);
}/*
void P(){
for(int i=2;i<=N;i++){
	    if(!prime[i]){
		for(int j=2*i;j<=N;j+=i){
		prime[j]=i;}
		}
	}
}
int ctoi(char c){
	return (int)(c-'0');
}
void find_subset(int n){
	for(int i=1;i<((int)1<<n);i++){
		for(int j=0;j<n;j++){
			if(checkBit(i,j)){
				//do what you want;
			}
		}
	}
}*/

void solve(){
   int n,a,e;cin>>n;
   mp m,eat;
   for(int i=0;i<n;i++){
   	  cin>>a>>e;
   	  m[a]++;
   	  if(e)eat[a]++;
   }
   map<int,int>ans;
   vector<pii>v;
   for(mit it=B(m);it!=E(m);it++){
   	   v.pb({m[(*it).F],eat[(*it).F]});
   }
   sort(rall(v));
   int k=v[0].F;
   ans[v[0].F]=min(v[0].S,v[0].F);
   bool flag=1;
   multiset<int>s;
   for(int i=1;i<si(v);i++){if(k>1){
   flag=1;
        if(si(s)&&v[i].F==k-1){
        	msit it=E(s);it--;
        	ans[k-1]=min(k-1,*it);
		}
   	    while(v[i].F>=k-1){
   	    	flag=0;
   	    	s.insert(v[i].S);
   	    	ans[k-1]=min(max(ans[k-1],v[i].S),k-1);
   	    	i++;
		}
		if(flag){
			int l=v[i].F,l1=v[i].S;
			while(k-1>v[i].F&&si(s)){
				msit it=E(s);it--;
				ans[k-1]=min(max(*it,ans[k]),k);
				k--;s.erase(it);
			}
			while(i<si(v)&&l==v[i].F)s.insert(v[i].S),i++;i--;
			msit it=E(s);it--;
			ans[v[i].F]=min(max(*it,ans[v[i].F]),v[i].F);k=v[i].F;
			s.erase(it);
		}
		else if(si(s)){
			msit it=E(s);it--;
			s.erase(it);k--;i--;
		}}
   }
   while(si(s)&&k-1){
   	   msit it=E(s);it--;
   	   ans[k-1]=min(max(*it,ans[k-1]),k-1);k--;s.erase(it);
   }
   int sum1=0,sum2=0;
   for(mit it=B(ans);it!=E(ans);it++){
   	   sum1+=(*it).F,sum2+=(*it).S;
   }
   cout<<sum1<<" "<<sum2<<endl;
}
signed main(){
    optimize();
	int t=1;
	//P();
    cin>>t;
	while(t--){
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define C            1000000007
#define MOD          998244353
#define int          long long
#define D            double
#define S            second
#define F            first
#define pb           push_back
#define ff           fflush(stdout)
#define ppb          pop_back()
#define B(c)         (c).begin()
#define E(c)         (c).end()
#define all(c)       (c).begin(),(c).end()
#define rall(c)      (c).rbegin(),(c).rend()
#define SORT(v)       sort(ALL(v))
#define DB            cerr<<"move"<<endl;
#define DV(x)        cerr<<x<<endl;
#define inputarr(a,n)  for(int i=0;i<n;++i) cin>>a[i]
#define printarr(a,n)  for(int i=0;i<n;++i) cout<<a[i]<<" "
#define lb           lower_bound
#define ub           upper_bound
#define si(c)        (int)((c).size())
#define gcd(a,b)      __gcd(a,b)
#define lcm(a,b)     (a*(b/gcd(a,b)))
#define sqr(a)       ((a) * (a))
#define optimize()   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef pair<int,int>                         pii;
typedef pair<int,pii>                         pipii;
typedef vector<vector<int> >                  vvi;
typedef map<int,int>                          mp;
typedef map<string,int>                       msi;
typedef priority_queue<int, vector<int>, greater<int> > pq;
typedef vector<int>::iterator                  vit;
typedef set<int>::iterator                     sit;
typedef map<int,int>::iterator                 mit;
typedef multiset<int>::iterator                msit;
inline  bool checkBit(int n, int i) { return n&((int)1<<i); }
inline  int setBit(int n, int i) { return n|((int)1<<i);; }
inline  int resetBit(int n, int i) { return n&(~((int)1<<i)); }
inline  bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline  bool isLeapYear(int year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline  void normal(int &a) { a = (a+MOD)%MOD; }
inline  int modMul(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline  int modAdd(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline  int modSub(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline  int modPow(int b, int p) { int r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline  int modInverse(int a) { return modPow(a, MOD-2); }
inline  int modDiv(int a,int b) { return modMul(a, modInverse(b)); }
/*const int N=5e5+1;
int prime[N];*/
int ans=0;
int fact(int n){
	if(n==0)return 0;
	return (n==1?1:(n*fact(n-1))%C);
}/*
void P(){
for(int i=2;i<=N;i++){
	    if(!prime[i]){
		for(int j=2*i;j<=N;j+=i){
		prime[j]=i;}
		}
	}
}
int ctoi(char c){
	return (int)(c-'0');
}
void find_subset(int n){
	for(int i=1;i<((int)1<<n);i++){
		for(int j=0;j<n;j++){
			if(checkBit(i,j)){
				//do what you want;
			}
		}
	}
}*/

void solve(){
   int n,a,e;cin>>n;
   mp m,eat;
   for(int i=0;i<n;i++){
   	  cin>>a>>e;
   	  m[a]++;
   	  if(e)eat[a]++;
   }
   map<int,int>ans;
   vector<pii>v;
   for(mit it=B(m);it!=E(m);it++){
   	   v.pb({m[(*it).F],eat[(*it).F]});
   }
   sort(rall(v));
   int k=v[0].F;
   ans[v[0].F]=v[0].S;
   bool flag=1;
   multiset<int>s;
   for(int i=1;i<si(v);i++){if(k>1){
   flag=1;
        if(si(s)&&v[i].F==k-1){
        	msit it=E(s);it--;
        	ans[k-1]=min(k-1,*it);
		}
   	    while(i<si(v)&&v[i].F>=k-1){
   	    	flag=0;
   	    	s.insert(v[i].S);
   	    	ans[k-1]=min(max(ans[k-1],v[i].S),k-1);
   	    	i++;
		}
		if(flag){
			int l=v[i].F,l1=v[i].S;
			while(k-1>v[i].F&&si(s)){
				msit it=E(s);it--;
				ans[k-1]=min(max(*it,ans[k]),k);
				k--;s.erase(it);
			}
			while(i<si(v)&&l==v[i].F)s.insert(v[i].S),i++;i--;
			msit it=E(s);it--;
			ans[v[i].F]=min(max(*it,ans[v[i].F]),v[i].F);k=v[i].F;
			s.erase(it);
		}
		else if(si(s)){
			msit it=E(s);it--;
			s.erase(it);k--;i--;
		}}
   }
   while(si(s)&&k>1){
   	   msit it=E(s);it--;
   	   ans[k-1]=min(max(*it,ans[k-1]),k-1);k--;s.erase(it);
   }
   int sum1=0,sum2=0;
   for(mit it=B(ans);it!=E(ans);it++){
   	   sum1+=(*it).F,sum2+=(*it).S;
   }
   cout<<sum1<<" "<<sum2<<endl;
}
signed main(){
    optimize();
	int t=1;
	//P();
    cin>>t;
	while(t--){
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define C            1000000007
#define MOD          998244353
#define int          long long
#define D            double
#define S            second
#define F            first
#define pb           push_back
#define ff           fflush(stdout)
#define ppb          pop_back()
#define B(c)         (c).begin()
#define E(c)         (c).end()
#define all(c)       (c).begin(),(c).end()
#define rall(c)      (c).rbegin(),(c).rend()
#define SORT(v)       sort(ALL(v))
#define DB            cerr<<"move"<<endl;
#define DV(x)        cerr<<x<<endl;
#define inputarr(a,n)  for(int i=0;i<n;++i) cin>>a[i]
#define printarr(a,n)  for(int i=0;i<n;++i) cout<<a[i]<<" "
#define lb           lower_bound
#define ub           upper_bound
#define si(c)        (int)((c).size())
#define gcd(a,b)      __gcd(a,b)
#define lcm(a,b)     (a*(b/gcd(a,b)))
#define sqr(a)       ((a) * (a))
#define optimize()   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef pair<int,int>                         pii;
typedef pair<int,pii>                         pipii;
typedef vector<vector<int> >                  vvi;
typedef map<int,int>                          mp;
typedef map<string,int>                       msi;
typedef priority_queue<int, vector<int>, greater<int> > pq;
typedef vector<int>::iterator                  vit;
typedef set<int>::iterator                     sit;
typedef map<int,int>::iterator                 mit;
typedef multiset<int>::iterator                msit;
inline  bool checkBit(int n, int i) { return n&((int)1<<i); }
inline  int setBit(int n, int i) { return n|((int)1<<i);; }
inline  int resetBit(int n, int i) { return n&(~((int)1<<i)); }
inline  bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline  bool isLeapYear(int year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline  void normal(int &a) { a = (a+MOD)%MOD; }
inline  int modMul(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline  int modAdd(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline  int modSub(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline  int modPow(int b, int p) { int r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline  int modInverse(int a) { return modPow(a, MOD-2); }
inline  int modDiv(int a,int b) { return modMul(a, modInverse(b)); }
/*const int N=5e5+1;
int prime[N];*/
int ans=0;
int fact(int n){
	if(n==0)return 0;
	return (n==1?1:(n*fact(n-1))%C);
}/*
void P(){
for(int i=2;i<=N;i++){
	    if(!prime[i]){
		for(int j=2*i;j<=N;j+=i){
		prime[j]=i;}
		}
	}
}
int ctoi(char c){
	return (int)(c-'0');
}
void find_subset(int n){
	for(int i=1;i<((int)1<<n);i++){
		for(int j=0;j<n;j++){
			if(checkBit(i,j)){
				//do what you want;
			}
		}
	}
}*/

void solve(){
   int n,a,e;cin>>n;
   mp m,eat;
   for(int i=0;i<n;i++){
   	  cin>>a>>e;
   	  m[a]++;
   	  if(e)eat[a]++;
   }
   map<int,int>ans;
   vector<pii>v;
   for(mit it=B(m);it!=E(m);it++){
   	   v.pb({m[(*it).F],eat[(*it).F]});
   }
   sort(rall(v));
   int k=v[0].F;
   ans[v[0].F]=v[0].S;
   bool flag=1;
   multiset<int>s;
   for(int i=1;i<si(v);i++){if(k>1){
   flag=1;
        if(si(s)&&v[i].F==k-1){
        	msit it=E(s);it--;
        	ans[k-1]=min(k-1,*it);
		}
   	    while(v[i].F>=k-1){
   	    	flag=0;
   	    	s.insert(v[i].S);
   	    	ans[k-1]=min(max(ans[k-1],v[i].S),k-1);
   	    	i++;
		}
		if(flag){
			int l=v[i].F,l1=v[i].S;
			while(k-1>v[i].F&&si(s)){
				msit it=E(s);it--;
				ans[k-1]=min(max(*it,ans[k]),k);
				k--;s.erase(it);
			}
			while(i<si(v)&&l==v[i].F)s.insert(v[i].S),i++;i--;
			msit it=E(s);it--;
			ans[v[i].F]=min(max(*it,ans[v[i].F]),v[i].F);k=v[i].F;
			s.erase(it);
		}
		else if(si(s)){
			msit it=E(s);it--;
			s.erase(it);k--;i--;
		}}
   }
   while(si(s)&&k-1){
   	   msit it=E(s);it--;
   	   ans[k-1]=min(max(*it,ans[k-1]),k-1);k--;s.erase(it);
   }
   int sum1=0,sum2=0;
   for(mit it=B(ans);it!=E(ans);it++){
   	   sum1+=(*it).F,sum2+=(*it).S;
   }
   cout<<sum1<<" "<<sum2<<endl;
}
signed main(){
    optimize();
	int t=1;
	//P();
    cin>>t;
	while(t--){
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define C            1000000007
#define MOD          998244353
#define int          long long
#define D            double
#define S            second
#define F            first
#define pb           push_back
#define ff           fflush(stdout)
#define ppb          pop_back()
#define B(c)         (c).begin()
#define E(c)         (c).end()
#define all(c)       (c).begin(),(c).end()
#define rall(c)      (c).rbegin(),(c).rend()
#define SORT(v)       sort(ALL(v))
#define DB            cerr<<"move"<<endl;
#define DV(x)        cerr<<x<<endl;
#define inputarr(a,n)  for(int i=0;i<n;++i) cin>>a[i]
#define printarr(a,n)  for(int i=0;i<n;++i) cout<<a[i]<<" "
#define lb           lower_bound
#define ub           upper_bound
#define si(c)        (int)((c).size())
#define gcd(a,b)      __gcd(a,b)
#define lcm(a,b)     (a*(b/gcd(a,b)))
#define sqr(a)       ((a) * (a))
#define optimize()   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef pair<int,int>                         pii;
typedef pair<int,pii>                         pipii;
typedef vector<vector<int> >                  vvi;
typedef map<int,int>                          mp;
typedef map<string,int>                       msi;
typedef priority_queue<int, vector<int>, greater<int> > pq;
typedef vector<int>::iterator                  vit;
typedef set<int>::iterator                     sit;
typedef map<int,int>::iterator                 mit;
typedef multiset<int>::iterator                msit;
inline  bool checkBit(int n, int i) { return n&((int)1<<i); }
inline  int setBit(int n, int i) { return n|((int)1<<i);; }
inline  int resetBit(int n, int i) { return n&(~((int)1<<i)); }
inline  bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline  bool isLeapYear(int year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline  void normal(int &a) { a = (a+MOD)%MOD; }
inline  int modMul(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline  int modAdd(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline  int modSub(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline  int modPow(int b, int p) { int r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline  int modInverse(int a) { return modPow(a, MOD-2); }
inline  int modDiv(int a,int b) { return modMul(a, modInverse(b)); }
/*const int N=5e5+1;
int prime[N];*/
int ans=0;
int fact(int n){
	if(n==0)return 0;
	return (n==1?1:(n*fact(n-1))%C);
}/*
void P(){
for(int i=2;i<=N;i++){
	    if(!prime[i]){
		for(int j=2*i;j<=N;j+=i){
		prime[j]=i;}
		}
	}
}
int ctoi(char c){
	return (int)(c-'0');
}
void find_subset(int n){
	for(int i=1;i<((int)1<<n);i++){
		for(int j=0;j<n;j++){
			if(checkBit(i,j)){
				//do what you want;
			}
		}
	}
}*/

void solve(){
   int n,a,e;cin>>n;
   mp m,eat;
   for(int i=0;i<n;i++){
   	  cin>>a>>e;
   	  m[a]++;
   	  if(e)eat[a]++;
   }
   map<int,int>ans;
   vector<pii>v;
   for(mit it=B(m);it!=E(m);it++){
   	   v.pb({m[(*it).F],eat[(*it).F]});
   }
   sort(rall(v));
   int k=v[0].F;
   ans[v[0].F]=v[0].S;
   bool flag=1;
   multiset<int>s;
   for(int i=1;i<si(v);i++){flag=1;
        if(si(s)&&v[i].F==k-1){
        	msit it=E(s);it--;
        	ans[k-1]=min(k-1,*it);
		}
   	    while(v[i].F>=k-1){
   	    	flag=0;
   	    	s.insert(v[i].S);
   	    	ans[k-1]=min(max(ans[k-1],v[i].S),k-1);
   	    	i++;
		}
		if(flag){
			int l=v[i].F;
			while(k>v[i].F&&si(s)){
				msit it=E(s);it--;
				ans[k]=min(max(*it,ans[k]),k);
				k--;s.erase(it);
			}
			while(i<si(v)&&l==v[i].F)s.insert(v[i].S),i++;i--;
			msit it=E(s);it--;
			ans[v[i].F]=min(max(*it,ans[v[i].F]),v[i].F);
			s.erase(it);
		}
		else if(si(s)){
			msit it=E(s);it--;
			s.erase(it);k--;i--;
		}
   }
   int sum1=0,sum2=0;
   for(mit it=B(ans);it!=E(ans);it++){
   	   sum1+=(*it).F,sum2+=(*it).S;
   }
   cout<<sum1<<" "<<sum2<<endl;
}
signed main(){
    optimize();
	int t=1;
	//P();
    cin>>t;
	while(t--){
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define C            1000000007
#define MOD          998244353
#define int          long long
#define D            double
#define S            second
#define F            first
#define pb           push_back
#define ff           fflush(stdout)
#define ppb          pop_back()
#define B(c)         (c).begin()
#define E(c)         (c).end()
#define all(c)       (c).begin(),(c).end()
#define rall(c)      (c).rbegin(),(c).rend()
#define SORT(v)       sort(ALL(v))
#define DB            cerr<<"move"<<endl;
#define DV(x)        cerr<<x<<endl;
#define inputarr(a,n)  for(int i=0;i<n;++i) cin>>a[i]
#define printarr(a,n)  for(int i=0;i<n;++i) cout<<a[i]<<" "
#define lb           lower_bound
#define ub           upper_bound
#define si(c)        (int)((c).size())
#define gcd(a,b)      __gcd(a,b)
#define lcm(a,b)     (a*(b/gcd(a,b)))
#define sqr(a)       ((a) * (a))
#define optimize()   ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef pair<int,int>                         pii;
typedef pair<int,pii>                         pipii;
typedef vector<vector<int> >                  vvi;
typedef map<int,int>                          mp;
typedef map<string,int>                       msi;
typedef priority_queue<int, vector<int>, greater<int> > pq;
typedef vector<int>::iterator                  vit;
typedef set<int>::iterator                     sit;
typedef map<int,int>::iterator                 mit;
typedef multiset<int>::iterator                msit;
inline  bool checkBit(int n, int i) { return n&((int)1<<i); }
inline  int setBit(int n, int i) { return n|((int)1<<i);; }
inline  int resetBit(int n, int i) { return n&(~((int)1<<i)); }
inline  bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
inline  bool isLeapYear(int year) { return (year%400==0) || (year%4==0 && year%100!=0); }
inline  void normal(int &a) { a = (a+MOD)%MOD; }
inline  int modMul(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline  int modAdd(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline  int modSub(int a, int b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline  int modPow(int b, int p) { int r = 1; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline  int modInverse(int a) { return modPow(a, MOD-2); }
inline  int modDiv(int a,int b) { return modMul(a, modInverse(b)); }
/*const int N=5e5+1;
int prime[N];*/
int ans=0;
int fact(int n){
	if(n==0)return 0;
	return (n==1?1:(n*fact(n-1))%C);
}/*
void P(){
for(int i=2;i<=N;i++){
	    if(!prime[i]){
		for(int j=2*i;j<=N;j+=i){
		prime[j]=i;}
		}
	}
}
int ctoi(char c){
	return (int)(c-'0');
}
void find_subset(int n){
	for(int i=1;i<((int)1<<n);i++){
		for(int j=0;j<n;j++){
			if(checkBit(i,j)){
				//do what you want;
			}
		}
	}
}*/

void solve(){
   int n,a,e;cin>>n;
   mp m,eat;
   for(int i=0;i<n;i++){
   	  cin>>a>>e;
   	  m[a]++;
   	  if(e)eat[a]++;
   }
   map<int,int>ans;
   vector<pii>v;
   for(mit it=B(m);it!=E(m);it++){
   	   v.pb({m[(*it).F],eat[(*it).F]});
   }
   sort(rall(v));
   int k=v[0].F;
   ans[v[0].F]=min(v[0].S,v[0].F);
   bool flag=1;
   multiset<int>s;
   for(int i=1;i<si(v);i++){if(k>1){
   flag=1;
        if(si(s)&&v[i].F==k-1){
        	msit it=E(s);it--;
        	ans[k-1]=min(k-1,*it);
		}
   	    while(i<si(v)&&v[i].F>=k-1){
   	    	flag=0;
   	    	s.insert(v[i].S);
   	    	ans[k-1]=min(max(ans[k-1],v[i].S),k-1);
   	    	i++;
		}
		if(flag){
			int l=v[i].F,l1=v[i].S;
			while(k-1>v[i].F&&si(s)){
				msit it=E(s);it--;
				ans[k-1]=min(max(*it,ans[k]),k);
				k--;s.erase(it);
			}
			while(i<si(v)&&l==v[i].F)s.insert(v[i].S),i++;i--;
			msit it=E(s);it--;
			ans[v[i].F]=min(max(*it,ans[v[i].F]),v[i].F);k=v[i].F;
			s.erase(it);
		}
		else if(si(s)){
			msit it=E(s);it--;
			s.erase(it);k--;i--;
		}}
   }
   while(si(s)&&k>1){
   	   msit it=E(s);it--;
   	   ans[k-1]=min(max(*it,ans[k-1]),k-1);k--;s.erase(it);
   }
   int sum1=0,sum2=0;
   for(mit it=B(ans);it!=E(ans);it++){
   	   sum1+=(*it).F,sum2+=(*it).S;
   }
   cout<<sum1<<" "<<sum2<<endl;
}
signed main(){
    optimize();
	int t=1;
	//P();
    cin>>t;
	while(t--){
		solve();
	}
}