You are the head of a large enterprise. n people work at you, and n is odd (i. e. n is not divisible by 2).

You have to distribute salaries to your employees. Initially, you have s dollars for it, and the i-th employee should get a salary from li to ri dollars. You have to distribute salaries in such a way that the median salary is maximum possible.

To find the median of a sequence of odd length, you have to sort it and take the element in the middle position after sorting. For example:

the median of the sequence [5,1,10,17,6] is 6,
the median of the sequence [1,2,1] is 1.
It is guaranteed that you have enough money to pay the minimum salary, i.e l1+l2+⋯+ln≤s.

Note that you don’t have to spend all your s dollars on salaries.

You have to answer t test cases.

Input
The first line contains one integer t (1≤t≤2⋅105) — the number of test cases.

The first line of each query contains two integers n and s (1≤n<2⋅105, 1≤s≤2⋅1014) — the number of employees and the amount of money you have. The value n is not divisible by 2.

The following n lines of each query contain the information about employees. The i-th line contains two integers li and ri (1≤li≤ri≤109).

It is guaranteed that the sum of all n over all queries does not exceed 2⋅105.

It is also guaranteed that you have enough money to pay the minimum salary to each employee, i. e. ∑i=1nli≤s.

Output
For each test case print one integer — the maximum median salary that you can obtain.

Example
inputCopy

3
3 26
10 12
1 4
10 11
1 1337
1 1000000000
5 26
4 4
2 4
6 8
5 6
2 7
outputCopy
11
1337
6
Note
In the first test case, you can distribute salaries as follows: sal1=12,sal2=2,sal3=11 (sali is the salary of the i-th employee). Then the median salary is 11.

In the second test case, you have to pay 1337 dollars to the only employee.

In the third test case, you can distribute salaries as follows: sal1=4,sal2=3,sal3=6,sal4=6,sal5=7. Then the median salary is 6.


考虑到中位数最大,于是我们可以二分。但是每个人的工资必须在那个区间,所以我们不能直接二分。

我们按照L排序之后,每次二分的check值为mid,因为我们按照L从小到大排序,所以我们每次从后面开始找满足R大于等于mid的数量,此时必然是最优的。


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10;
int T,n,s,mx,ts;
struct node{
	int l,r;
}t[N];
int cmp(node a,node b){
	return a.l<b.l;
}
inline int check(int mid){
	int cnt=0; s=ts;
	for(int i=n;i>=1;i--){
		if(t[i].l>=mid){
			cnt++; continue;
		}
		if(t[i].r>=mid){
			if(s>=(mid-t[i].l))	cnt++,s-=(mid-t[i].l);
			else	return cnt>(n/2);
		}
	}
	return cnt>(n/2);
}
int bsearch(){
	int l=1,r=mx;
	while(l<r){
		int mid=l+r+1>>1;
		if(check(mid))	l=mid;
		else	r=mid-1;
	}
	return l;
}
signed main(){
	cin>>T;
	while(T--){
		scanf("%lld %lld",&n,&s); mx=s;
		for(int i=1;i<=n;i++)	scanf("%lld %lld",&t[i].l,&t[i].r),s-=t[i].l;
		sort(t+1,t+1+n,cmp); ts=s;
		printf("%lld\n",bsearch());
	}
	return 0;
}