// ===================================
//   author: M_sea
//   website: http://m-sea-blog.com/
// ===================================
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#define re register
#define int long long
using namespace std;

inline int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
    return X*w;
}

inline bool cmp1(pair<int,int> a,pair<int,int> b) {
    return a.first<b.first;
}
inline bool cmp2(pair<int,int> a,pair<int,int> b) {
    return a.second>b.second;
}

const int N=200000+10;

int n,s;
pair<int,int> a[N];
vector<pair<int,int> > vec;

inline bool check(int mid) {
    int cnt1=0,cnt2=0,rest=s; vec.clear();
    for (re int i=1;i<=n;++i) {
        if (a[i].second<mid) ++cnt1,rest-=a[i].first;
        else if (a[i].first>mid) ++cnt2,rest-=a[i].first;
        else vec.push_back(a[i]);
    }
    if (cnt1>n/2||cnt2>n/2) return 0;
    sort(vec.begin(),vec.end());
    for (auto i:vec) {
        if (cnt1<n/2) ++cnt1,rest-=i.first;
        else ++cnt2,rest-=mid;
    }
    return rest>=0;
}

signed main() {
    int T=read();
    while (T--) {
        n=read(),s=read();
        for (re int i=1;i<=n;++i) {
            int l=read(),r=read();
            a[i]=make_pair(l,r);
        }
        sort(a+1,a+n+1,cmp1); int L=a[(n+1)>>1].first;
        sort(a+1,a+n+1,cmp2); int R=a[(n+1)>>1].second;
        while (L<R) {
            int mid=(L+R+1)>>1;
            if (check(mid)) L=mid;
            else R=mid-1;
        }
        printf("%I64d\n",L);
    }
    return 0;
}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    int i=n;
  //  printf("x=%d\n",x);
    while(nd[i].l>x&&cnt)
    {
        sm+=nd[i].l;
        cnt--;
        i--;
    }
 //   if(cnt<1) return false;

    for(;i>=1;i--)
    {
       // if(cnt==0) break;
        if(nd[i].l<=x&&nd[i].r>=x)
        {
            if(cnt)
            {
                sm+=x;
                cnt--;
            }
            else sm+=nd[i].l;

        }
        else sm+=nd[i].l;
    }

    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    int i=n;
    while(nd[i].l>x)
    {
        sm+=nd[i].l;
        cnt--;
        i--;
    }
    if(cnt<1) return false;
    for(;i>=1;i--)
    {
        if(nd[i].l<=x&&nd[i].r>=x)
        {
            if(cnt)
            {
                sm+=m;
                cnt--;
            }
            else
                break;
        }
    }
    for(;i>=1;i--)
        sm+=nd[i].l;

    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    int i=n;
    //printf("x=%d\n",x);
    while(nd[i].l>x)
    {
        sm+=nd[i].l;
        cnt--;
        i--;
    }
    if(cnt<1) return true;
    //printf("ok\n");
    for(;i>=1;i--)
    {
       // if(cnt==0) break;
        if(nd[i].l<=x&&nd[i].r>=x)
        {
            if(cnt)
            {
                sm+=x;
                cnt--;
            }
            else break;

        }
    }
    for(;i>=1;i--)
        sm+=nd[i].l;

    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    int i=n;
    while(nd[i].l>x)
    {
        sm+=nd[i].l;
        cnt--;
        i--;
    }
    if(cnt<1) return false;
    for(;i>=1;i--)
    {
        if(cnt==0) break;
        if(nd[i].l<=x&&nd[i].r>=x)
        {
                sm+=x;
                cnt--;
        }
    }
    for(;i>=1;i--)
        sm+=nd[i].l;

    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    int i=n;
    while(nd[i].l>x)
    {
        sm+=nd[i].l;
        cnt--;
        i--;
    }
    if(cnt<1) return false;
    for(;i>=1;i--)
    {
       // if(cnt==0) break;
        if(nd[i].l<=x&&nd[i].r>=x)
        {
            if(cnt)
            {
                sm+=x;
                cnt--;
            }
            else break;

        }
    }
    for(;i>=1;i--)
        sm+=nd[i].l;

    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    int i=n;
  //  printf("x=%d\n",x);
    while(nd[i].l>x&&cnt>=2)
    {
        sm+=nd[i].l;
        cnt--;
        i--;
    }
 //   if(cnt<1) return false;

    for(;i>=1;i--)
    {
       // if(cnt==0) break;
        if(nd[i].l<=x&&nd[i].r>=x)
        {
            if(cnt)
            {
                sm+=x;
                cnt--;
            }
            else sm+=nd[i].l;

        }
        else sm+=nd[i].l;
    }

    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    int i=n;
    printf("x=%d\n",x);
    while(nd[i].l>x)
    {
        sm+=nd[i].l;
        cnt--;
        i--;
    }
 //   if(cnt<1) return false;

    for(;i>=1;i--)
    {
       // if(cnt==0) break;
        if(nd[i].l<=x&&nd[i].r>=x)
        {
            if(cnt)
            {
                sm+=x;
                cnt--;
            }
            else sm+=nd[i].l

        }
        else sm+=nd[i].l;
    }

    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    int i=n;
    while(nd[i].l>x)
    {
        sm+=nd[i].l;
        cnt--;
        i--;
    }
    if(cnt<0) return false;
    for(;i>=1;i--)
    {
        if(nd[i].l<=x&&nd[i].r>=x)
        {
            sm+=x; cnt--;
            if(cnt==0) break;
        }
    }

    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    for(int i=n;i>=1;i--)
    {
        if(nd[i].l<=x&&nd[i].r>=x)
        {
            sm+=x;
            cnt--;
        }
        else
        {
            sm+=nd[i].l;
            if(cnt&&nd[i].l>=x)
            cnt--;
        }
    }
    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    int i=n;
  //  printf("x=%d\n",x);
    while(nd[i].l>x)
    {
        sm+=nd[i].l;
        cnt--;
        i--;
    }
 //   if(cnt<1) return false;

    for(;i>=1;i--)
    {
       // if(cnt==0) break;
        if(nd[i].l<=x&&nd[i].r>=x)
        {
            if(cnt)
            {
                sm+=x;
                cnt--;
            }
            else sm+=nd[i].l;

        }
        else sm+=nd[i].l;
    }

    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    for(int i=n;i>=1;i--)
    {
        if(cnt&&nd[i].l<=x&&nd[i].r>=x)
        {
            sm+=x;
            cnt--;
        }
        else
        {
            sm+=nd[i].l;
            if(cnt&&nd[i].l>=x) cnt--;
        }
    }

    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    int i=n;
    printf("x=%d\n",x);
    while(nd[i].l>x)
    {
        sm+=nd[i].l;
        cnt--;
        i--;
    }
 //   if(cnt<1) return false;

    for(;i>=1;i--)
    {
       // if(cnt==0) break;
        if(nd[i].l<=x&&nd[i].r>=x)
        {
            if(cnt)
            {
                sm+=x;
                cnt--;
            }
            else sm+=nd[i].l;

        }
        else sm+=nd[i].l;
    }

    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include <cstdio>
#include <queue>
#include <vector>
#include<iostream>
#include<map>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<bits/stdc++.h>

#define rep(i,a,b)  for(register  int i(a); i <= b; ++i)
#define ll long long
#define inf 1e15
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define PI acos(-1)
#define fi first
#define se second
#define pb push_back

using namespace std;

const int N=1e6+10;
const ll mod=1e9+7;

inline ll read()
{
    ll X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

ll n,m,k;

ll ar[N],br[N];
ll ans;
struct node
{
    ll l,r;
    const bool operator<(const node &a)const
    {
        return l<a.l;
    }
}nd[N];
bool check(ll x)
{
    int cnt=n/2+1;  ll sm=0;
    int i=n;
    while(nd[i].l>x)
    {
        sm+=nd[i].l;
        cnt--;
        i--;
    }
    if(cnt<1) return false;
    for(;i>=1;i--)
    {
        if(cnt==0) break;
        if(nd[i].l<=x&&nd[i].r>=x)
        {
            if(cnt)
            {
                sm+=m;
                cnt--;
            }
        }
    }
    for(;i>=1;i--)
        sm+=nd[i].l;

    if(cnt) return false;
    return sm<=m;
}

int main()
{
    // gbtb;
    // freopen("input.txt","r",stdin);

    //while(~scanf("%d %d",&x,&y))

      int T;T=read();while(T--)
    {
        n=read(); m=read();
        rep(i,1,n)
        {
            nd[i].l=read(); nd[i].r=read();
        }
        sort(nd+1,nd+1+n);
        ll l=0; ll r=m;
        while(l<=r)
        {
            ll md=l+r>>1;
            if(check(md))
            {
                l=md+1;
                ans=md;
            }
            else
            r=md-1;
        }
        cout<<ans<<endl;

    }
    return 0;

}


#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<string>
#include<queue>
using namespace std;
typedef long long ll;
#define pb push_back
#define mk make_pair
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repp(a,b,c) for(int a=b;a<=c;a++)
#define mem(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const int maxn=2e5+10;
void useiostream()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
struct peo
{
	int l,r;
}P[maxn];
bool cmp(peo a,peo b)
{
	return a.l<b.l;
}
bool check(int len,int n,int now,ll Sum)
{
	for(int i=len;i<=n;i++)
	{
		Sum-=max(now,P[i].l);
		if(Sum<0)return false;
		else return true;
	}
}
int main()
{
	useiostream();
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		ll sum;
		cin>>n>>sum;
		repp(i,1,n)
		{
			cin>>P[i].l>>P[i].r;
		}
		sort(P+1,P+1+n,cmp);
		int len=n/2;
		for(int i=1;i<=len;i++)
		{
			sum-=P[i].l;
		}
		int Min=inf;
		int Max=0;
		for(int i=len+1;i<=n;i++)
		{
			Min=min(Min,P[i].r);
			Max=max(Max,P[i].r);
		}
		int L=P[n].l;
		int R=Min;
		while(L<R)
		{
			int m=((L+R)>>1);
			if(check(len,n,m,sum))
			{
				L=m+1;
			}
			else R=m-1;
		}
		cout<<L<<"\n";
	}
}
/*

100
222211110000
0000
12345
54321
*/


#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<string>
#include<queue>
using namespace std;
typedef long long ll;
#define pb push_back
#define mk make_pair
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repp(a,b,c) for(int a=b;a<=c;a++)
#define mem(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const int maxn=2e5+10;
void useiostream()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
struct peo
{
	int l,r;
}P[maxn];
ll max(ll a,ll b)
{
	return a>b?a:b;
}
bool cmp(peo a,peo b)
{
	if(a.l==b.l)return a.r>b.r;
	return a.l<b.l;
}
bool check(int len,int n,ll now,ll Sum)
{
	int count=0;
	for(int i=n;i>=1&&count<len;i--)
	{
		if(P[i].r>=now)
		{
			Sum-=max(now-P[i].l,0);
			count++;
		}
		if(Sum<0)return false;
	}
	if(count==len)return true;
	else return false;
}
int main()
{
	useiostream();
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		ll sum;
		cin>>n>>sum;
		repp(i,1,n)
		{
			cin>>P[i].l>>P[i].r;
		}
		sort(P+1,P+1+n,cmp);
		int len=(n+1)/2;
		for(int i=1;i<=n;i++)
		{
			sum-=P[i].l;
		}
		int Min=inf;
		int Max=0;
		for(int i=1;i<=n;i++)
		{
			Min=min(Min,P[i].r);
			Max=max(Max,P[i].r);
		}
		ll L=1;
		ll R=Max;
		while(L<R)
		{
			ll m=((L+R+1)>>1);
			if(check(len,n,m,sum))
			{
				L=m+1;
			}
			else R=m-1;
		}
		cout<<L<<"\n";
	}
}
/*

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

1
5 26
4 4
2 4
6 8
5 6
2 7
*/


#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<string>
#include<queue>
using namespace std;
typedef long long ll;
#define pb push_back
#define mk make_pair
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repp(a,b,c) for(int a=b;a<=c;a++)
#define mem(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const int maxn=2e5+10;
void useiostream()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
struct peo
{
	int l,r;
}P[maxn];
ll max(ll a,ll b)
{
	return a>b?a:b;
}
bool cmp(peo a,peo b)
{
	if(a.l==b.l)return a.r>b.r;
	return a.l<b.l;
}
bool check(int len,int n,ll now,ll Sum)
{
	for(int i=len;i<=n;i++)
	{
		Sum-=max(now,P[i].l);
		if(Sum<0)return false;
		else return true;
	}
}
int main()
{
	useiostream();
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		ll sum;
		cin>>n>>sum;
		repp(i,1,n)
		{
			cin>>P[i].l>>P[i].r;
		}
		sort(P+1,P+1+n,cmp);
		int len=n/2;
		for(int i=1;i<=len;i++)
		{
			sum-=P[i].l;
		}
		int Min=inf;
		int Max=0;
		for(int i=len+1;i<=n;i++)
		{
			Min=min(Min,P[i].r);
			Max=max(Max,P[i].r);
		}
		ll L=P[len+1].l;
		ll R=Min;
		ll last=-1;
		while(L<R)
		{
			ll m=((L+R)>>1);
			if(m==last)
			{
				break;
			}
			if(check(len,n,m,sum))
			{
				L=m+1;
			}
			else R=m-1;
			last=m;
		}
		cout<<L<<"\n";
	}
}
/*

100
222211110000
0000
12345
54321
*/


#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<string>
#include<queue>
using namespace std;
typedef long long ll;
#define pb push_back
#define mk make_pair
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repp(a,b,c) for(int a=b;a<=c;a++)
#define mem(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const int maxn=2e5+10;
void useiostream()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
struct peo
{
	int l,r;
}P[maxn];
ll max(ll a,ll b)
{
	return a>b?a:b;
}
bool cmp(peo a,peo b)
{
	if(a.l==b.l)return a.r>b.r;
	return a.l<b.l;
}
bool check(int len,int n,ll now,ll Sum)
{
	for(int i=len;i<=n;i++)
	{
		Sum-=max(now,P[i].l);
		if(Sum<0)return false;
		else return true;
	}
}
int main()
{
	useiostream();
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		ll sum;
		cin>>n>>sum;
		repp(i,1,n)
		{
			cin>>P[i].l>>P[i].r;
		}
		sort(P+1,P+1+n,cmp);
		int len=n/2;
		for(int i=1;i<=len;i++)
		{
			sum-=P[i].l;
		}
		int Min=inf;
		int Max=0;
		for(int i=len+1;i<=n;i++)
		{
			Min=min(Min,P[i].r);
			Max=max(Max,P[i].r);
		}
		ll L=P[len+1].l;
		ll R=Min;
		ll last=-1;
		while(L<R)
		{
			ll m=((L+R)>>1);
			if(m==last)
			{
				break;
			}
			if(check(len,n,m,sum))
			{
				L=m;
			}
			else R=m-1;
			last=m;
		}
		cout<<L<<"\n";
	}
}
/*

100
222211110000
0000
12345
54321
*/


#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<string>
#include<queue>
using namespace std;
typedef long long ll;
#define pb push_back
#define mk make_pair
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repp(a,b,c) for(int a=b;a<=c;a++)
#define mem(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const int maxn=2e5+10;
void useiostream()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
struct peo
{
	int l,r;
}P[maxn];
ll max(ll a,ll b)
{
	return a>b?a:b;
}
bool cmp(peo a,peo b)
{
	if(a.l==b.l)return a.r>b.r;
	return a.l<b.l;
}
bool check(int len,int n,ll now,ll Sum)
{
	for(int i=len;i<=n;i++)
	{
		Sum-=max(now,P[i].l);
		if(Sum<0)return false;
		else return true;
	}
}
int main()
{
	useiostream();
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		ll sum;
		cin>>n>>sum;
		repp(i,1,n)
		{
			cin>>P[i].l>>P[i].r;
		}
		sort(P+1,P+1+n,cmp);
		int len=n/2;
		for(int i=1;i<=len;i++)
		{
			sum-=P[i].l;
		}
		int Min=inf;
		int Max=0;
		for(int i=len+1;i<=n;i++)
		{
			Min=min(Min,P[i].r);
			Max=max(Max,P[i].r);
		}
		ll L=P[len+1].l;
		ll R=Min;
		while(L<R)
		{
			ll m=((L+R)>>1);
			if(check(len,n,m,sum))
			{
				L=m+1;
			}
			else R=m-1;
		}
		cout<<L<<"\n";
	}
}
/*

100
222211110000
0000
12345
54321
*/


#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<string>
#include<queue>
using namespace std;
typedef long long ll;
#define pb push_back
#define mk make_pair
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repp(a,b,c) for(int a=b;a<=c;a++)
#define mem(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const int maxn=2e5+10;
void useiostream()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
struct peo
{
	int l,r;
}P[maxn];
bool cmp(peo a,peo b)
{
	if(a.l==b.l)return a.r>b.r;
	return a.l<b.l;
}
bool check(int len,int n,int now,ll Sum)
{
	for(int i=len;i<=n;i++)
	{
		Sum-=max(now,P[i].l);
		if(Sum<0)return false;
		else return true;
	}
}
int main()
{
	useiostream();
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		ll sum;
		cin>>n>>sum;
		repp(i,1,n)
		{
			cin>>P[i].l>>P[i].r;
		}
		sort(P+1,P+1+n,cmp);
		int len=n/2;
		for(int i=1;i<=len;i++)
		{
			sum-=P[i].l;
		}
		int Min=inf;
		int Max=0;
		for(int i=len+1;i<=n;i++)
		{
			Min=min(Min,P[i].r);
			Max=max(Max,P[i].r);
		}
		int L=P[n].l;
		int R=Min;
		while(L<R)
		{
			int m=((L+R)>>1);
			if(check(len,n,m,sum))
			{
				L=m+1;
			}
			else R=m-1;
		}
		cout<<L<<"\n";
	}
}
/*

100
222211110000
0000
12345
54321
*/


#include<iostream>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<string>
#include<queue>
using namespace std;
typedef long long ll;
#define pb push_back
#define mk make_pair
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repp(a,b,c) for(int a=b;a<=c;a++)
#define mem(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const int maxn=2e5+10;
void useiostream()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
struct peo
{
	int l,r;
}P[maxn];
ll max(ll a,ll b)
{
	return a>b?a:b;
}
bool cmp(peo a,peo b)
{
	if(a.l==b.l)return a.r>b.r;
	return a.l<b.l;
}
bool check(int len,int n,ll now,ll Sum)
{
	int count=0;
	for(int i=n;i>=1&&count<len;i--)
	{
		if(P[i].r>=now)
		{
			Sum-=max(now-P[i].l,0);
			count++;
		}
		if(Sum<0)return false;
	}
	if(count==len)return true;
	else return false;
}
int main()
{
	useiostream();
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		ll sum;
		cin>>n>>sum;
		repp(i,1,n)
		{
			cin>>P[i].l>>P[i].r;
		}
		sort(P+1,P+1+n,cmp);
		int len=(n+1)/2;
		for(int i=1;i<=n;i++)
		{
			sum-=P[i].l;
		}
		int Min=inf;
		int Max=0;
		for(int i=1;i<=n;i++)
		{
			Min=min(Min,P[i].r);
			Max=max(Max,P[i].r);
		}
		ll L=1;
		ll R=Max;
		while(L<R)
		{
			ll m=((L+R+1)>>1);
			if(check(len,n,m,sum))
			{
				L=m;
			}
			else R=m-1;
		}
		cout<<L<<"\n";
	}
}
/*

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

1
1 1337
1 1000000000

1
5 26
4 4
2 4
6 8
5 6
2 7
*/


#include<bits/stdc++.h>
#define ll long long
#define fst first
#define scd second
using namespace std;
const int maxn = 2e5+5;
pair<ll, ll>a[maxn];
int n;
ll s;

ll check(ll x){
    int cnt = n/2+1;
    ll sum = 0;
    for(int i = 1; i <= n; i++){
        if(a[i].fst <= x && a[i].scd >= x && cnt > 0){
            sum += x;
            cnt--;
        }
        else{
            sum += a[i].first;
            if(a[i].fst > x) cnt--;
        }
    }
    if(cnt){
        return s+1;
    }
    else{
        return sum;
    }
}

int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n >> s;
        for(int i = 1; i <= n; i++){
            cin >> a[i].fst >> a[i].scd;
        }
        sort(a+1, a+n+1);
        reverse(a+1, a+n+1);
        ll l = 0, r = s, mid, ans = 0;
        while(l <= r){
            mid = (l+r)/2;
            ll ck = check(mid);
            if(ck <= s){
                ans = mid;
                l = mid+1;
            }
            else{
                r = mid-1;
            }
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define db double
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const int maxn = 2e5+5;
pair<ll, ll>a[maxn];
int n;
ll s;
int check(ll x){
    vector<int>vecl, vecr, vecm;
    for(int i = 0; i < n; i++){
        if(a[i].se < x) vecl.pb(i);
        else if(a[i].fi > x) vecr.pb(i);
        else vecm.pb(i);
    }
    ll sum = 0;
    int cntl = 0, cntr = 0;
    for(int i = 0; i < vecl.size(); i++){
        sum += a[vecl[i]].fi;
        cntl++;
    }
    for(int i = 0; i < vecr.size(); i++){
        sum += a[vecr[i]].fi;
        cntr++;
    }
    int m = n/2;
    if(cntl > m) return 2;
    if(cntr > m) return 1;
    for(int i = 0; i < vecm.size(); i++){
        if(cntl < m){
            sum += a[vecm[i]].fi;
            cntl++;
        }
        else{
            sum += x;
            cntr++;
        }
    }
    //2��С 1���� 0ok
    if(sum > s) return 2;
    return 0;
}

int main(){
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        cin >> n >> s;
        for(int i = 0; i < n; i++){
            cin >> a[i].fi >> a[i].se;
        }
        sort(a, a+n);
        ll l = 0, r = s, mid, ans = 0;
        while(l <= r){
            mid = (l+r)/2;
            int ck = check(mid);
            if(ck == 0){
                ans = mid;
                l = mid + 1;
            }
            else if(ck == 1) l = mid + 1;
            else r = mid - 1;
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define db double
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const int maxn = 2e5+5;
pair<ll, ll>a[maxn];
int n;
ll s;
int check(ll x){
    vector<int>vecl, vecr, vecm;
    for(int i = 0; i < n; i++){
        if(a[i].se < x) vecl.pb(i);
        else if(a[i].fi > x) vecr.pb(i);
        else vecm.pb(i);
    }
    ll sum = 0;
    int cntl = 0, cntr = 0;
    for(int i = 0; i < vecl.size(); i++){
        sum += a[vecl[i]].fi;
        cntl++;
    }
    for(int i = 0; i < vecr.size(); i++){
        sum += a[vecr[i]].se;
        cntr++;
    }
    int m = n/2;
    for(int i = 0; i < vecm.size(); i++){
        if(cntl < m){
            sum += a[vecm[i]].fi;
            cntl++;
        }
        else{
            sum += x;
            cntr++;
        }
    }
    //2��С 1���� 0ok
    if(sum > s) return 2;
    if(cntl > m) return 2;
    else if(cntl == m) return 0;
    else return 1;
}

int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n >> s;
        for(int i = 0; i < n; i++){
            cin >> a[i].fi >> a[i].se;
        }
        ll l = 0, r = s, mid, ans = 0;
        while(l <= r){
            mid = (l+r)/2;
            int ck = check(mid);
            if(ck == 0){
                ans = mid;
                l = mid + 1;
            }
            else if(ck == 1) l = mid + 1;
            else r = mid - 1;
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define db double
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const int maxn = 2e5+5;
pair<ll, ll>a[maxn];
int n;
ll s;
int check(ll x){
    vector<int>vecl, vecr, vecm;
    for(int i = 0; i < n; i++){
        if(a[i].se < x) vecl.pb(i);
        else if(a[i].fi > x) vecr.pb(i);
        else vecm.pb(i);
    }
    ll sum = 0;
    int cntl = 0, cntr = 0;
    for(int i = 0; i < vecl.size(); i++){
        sum += a[vecl[i]].fi;
        cntl++;
    }
    for(int i = 0; i < vecr.size(); i++){
        sum += a[vecr[i]].fi;
        cntr++;
    }
    int m = n/2;
    if(cntl > m) return 2;
    if(cntr > m) return 1;
    for(int i = 0; i < vecm.size(); i++){
        if(cntl < m){
            sum += a[vecm[i]].fi;
            cntl++;
        }
        else{
            sum += x;
            cntr++;
        }
    }
    //2��С 1���� 0ok
    if(sum > s) return 2;
    return 0;
}

int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n >> s;
        for(int i = 0; i < n; i++){
            cin >> a[i].fi >> a[i].se;
        }
        sort(a, a+n);
        ll l = 0, r = s, mid, ans = 0;
        while(l <= r){
            mid = (l+r)/2;
            int ck = check(mid);
            if(ck == 0){
                ans = mid;
                l = mid + 1;
            }
            else if(ck == 1) l = mid + 1;
            else r = mid - 1;
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define ll long long
#define fst first
#define scd second
#define pb push_back
using namespace std;
const int maxn = 2e5+5;
pair<ll, ll>a[maxn];
vector<pair<ll, ll> > vecl, vecr, vecm;
int n;
ll s;

int check(ll x){//0,1 x���� 2 x��С
    vecl.clear(), vecr.clear(), vecm.clear();
    for(int i = 1; i <= n; i++){
        if(a[i].scd < x) vecl.pb(a[i]);
        else if(a[i].fst > x) vecr.pb(a[i]);
        else vecm.pb(a[i]);
    }
    //cout << vecl.size() << " " << vecm.size() << " " << vecr.size() << endl;
    if(vecm.size() == 0){
        if(vecl.size() > vecr.size()) return 2;
        else return 1;
    }
    ll sum = 0;
    int cnt1 = 0, cnt2 = 0;
    for(int i = 0; i < vecl.size(); i++){
        sum += vecl[i].fst;
    }
    cnt1 += vecl.size();
    for(int i = 0; i < vecr.size(); i++){
        sum += vecr[i].fst;
    }
    cnt2 += vecr.size();

    if(cnt1 > n/2) return 2;
    if(cnt2 > n/2) return 1;
    for(int i = 0; i < vecm.size(); i++){
        if(cnt1 < n/2){
            sum += vecm[i].fst;
            cnt1++;
        }
        else{
            sum += x;
            cnt2++;
        }
    }
    //cout << "cnt1 = " << cnt1 << endl;
    //cout << "cnt2 = " << cnt2 << endl;
    //cout << "sum = " << sum << endl;
    if(sum > s) return 2;
    return 0;
}

int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n >> s;
        for(int i = 1; i <= n; i++){
            cin >> a[i].fst >> a[i].scd;
        }
        sort(a+1, a+1+n);
        ll sum = 0;
        /*for(int i = 1; i <= n; i++){
            sum += a[i].first;
            cout << sum << " ";
        }
        cout << endl;
        */
        ll l = 0, r = s, mid, ans = 0;
        while(l <= r){
            mid = (l+r)/2;
            ll ck = check(mid);
            //cout << "mid = " << mid << endl;
            //cout << "ck = " << ck << endl;
            if(ck == 0){
                ans = mid;
                l = mid+1;
            }
            else if(ck == 2) r = mid-1;
            else l = mid+1;
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define db double
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const int maxn = 2e5+5;
pair<ll, ll>a[maxn];
int n;
ll s;
int check(ll x){
    vector<int>vecl, vecr, vecm;
    for(int i = 0; i < n; i++){
        if(a[i].se < x) vecl.pb(i);
        else if(a[i].fi > x) vecr.pb(i);
        else vecm.pb(i);
    }
    ll sum = 0;
    int cntl = 0, cntr = 0;
    for(int i = 0; i < vecl.size(); i++){
        sum += a[vecl[i]].fi;
        cntl++;
    }
    for(int i = 0; i < vecr.size(); i++){
        sum += a[vecr[i]].se;
        cntr++;
    }
    int m = n/2;
    for(int i = 0; i < vecm.size(); i++){
        if(cntl < m){
            sum += a[vecl[i]].fi;
            cntl++;
        }
        else{
            sum += x;
            cntr++;
        }
    }
    //2��С 1���� 0ok
    if(sum > s) return 2;
    if(cntl > m) return 2;
    else if(cntl == m) return 0;
    else return 1;
}

int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n >> s;
        for(int i = 0; i < n; i++){
            cin >> a[i].fi >> a[i].se;
        }
        ll l = 0, r = s, mid, ans = 0;
        while(l <= r){
            mid = (l+r)/2;
            int ck = check(mid);
            if(ck == 0){
                ans = mid;
                l = mid + 1;
            }
            else if(ck == 1) l = mid + 1;
            else r = mid - 1;
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define ll long long
#define fst first
#define scd second
#define pb push_back
using namespace std;
const int maxn = 2e5+5;
pair<ll, ll>a[maxn];
vector<pair<ll, ll> > vecl, vecr, vecm;
int n;
ll s;

int check(ll x){//0,1 x���� 2 x��С
    vecl.clear(), vecr.clear(), vecm.clear();
    for(int i = 1; i <= n; i++){
        if(a[i].scd < x) vecl.pb(a[i]);
        else if(a[i].fst > x) vecr.pb(a[i]);
        else vecm.pb(a[i]);
    }
    //cout << vecl.size() << " " << vecm.size() << " " << vecr.size() << endl;
    if(vecm.size() == 0){
        if(vecl.size() > vecr.size()) return 2;
        else return 1;
    }
    ll sum = 0;
    int cnt1 = 0, cnt2 = 0;
    for(int i = 0; i < vecl.size(); i++){
        sum += vecl[i].fst;
    }
    cnt1 += vecl.size();
    for(int i = 0; i < vecr.size(); i++){
        sum += vecr[i].fst;
    }
    cnt2 += vecr.size();

    if(cnt1 > n/2) return 2;
    if(cnt2 > n/2) return 1;
    for(int i = 0; i < vecm.size(); i++){
        if(cnt1 < n/2){
            sum += vecm[i].fst;
            cnt1++;
        }
        else{
            sum += x;
            cnt2++;
        }
    }
    //cout << "cnt1 = " << cnt1 << endl;
    //cout << "cnt2 = " << cnt2 << endl;
    //cout << "sum = " << sum << endl;
    if(sum > s) return 2;
    return 0;
}

int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n >> s;
        for(int i = 1; i <= n; i++){
            cin >> a[i].fst >> a[i].scd;
        }
        sort(a+1, a+1+n);
        ll sum = 0;
        for(int i = 1; i <= n; i++){
            sum += a[i].first;
            cout << sum << " ";
        }
        cout << endl;
        ll l = 0, r = s, mid, ans = 0;
        while(l <= r){
            mid = (l+r)/2;
            ll ck = check(mid);
            //cout << "mid = " << mid << endl;
            //cout << "ck = " << ck << endl;
            if(ck == 0){
                ans = mid;
                l = mid+1;
            }
            else if(ck == 2) r = mid-1;
            else l = mid+1;
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define db double
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const int maxn = 2e5+5;
pair<ll, ll>a[maxn];
int n;
ll s;
int check(ll x){
    vector<int>vecl, vecr, vecm;
    for(int i = 0; i < n; i++){
        if(a[i].se < x) vecl.pb(i);
        else if(a[i].fi > x) vecr.pb(i);
        else vecm.pb(i);
    }
    ll sum = 0;
    int cntl = 0, cntr = 0;
    for(int i = 0; i < vecl.size(); i++){
        sum += a[vecl[i]].fi;
        cntl++;
    }
    for(int i = 0; i < vecr.size(); i++){
        sum += a[vecr[i]].fi;
        cntr++;
    }
    int m = n/2;
    if(cntl > m) return 2;
    if(cntr > m) return 1;
    for(int i = 0; i < vecm.size(); i++){
        if(cntl < m){
            sum += a[vecm[i]].fi;
            cntl++;
        }
        else{
            sum += x;
            cntr++;
        }
    }
    //2��С 1���� 0ok
    if(sum > s) return 2;
    return 0;
}

int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n >> s;
        for(int i = 0; i < n; i++){
            cin >> a[i].fi >> a[i].se;
        }
        ll l = 0, r = s, mid, ans = 0;
        while(l <= r){
            mid = (l+r)/2;
            int ck = check(mid);
            if(ck == 0){
                ans = mid;
                l = mid + 1;
            }
            else if(ck == 1) l = mid + 1;
            else r = mid - 1;
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define db double
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const int maxn = 2e5+5;
pair<ll, ll>a[maxn];
int n;
ll s;
int check(ll x){
    vector<int>vecl, vecr, vecm;
    for(int i = 0; i < n; i++){
        if(a[i].se < x) vecl.pb(i);
        else if(a[i].fi > x) vecr.pb(i);
        else vecm.pb(i);
    }
    ll sum = 0;
    int cntl = 0, cntr = 0;
    for(int i = 0; i < vecl.size(); i++){
        sum += a[vecl[i]].fi;
        cntl++;
    }
    for(int i = 0; i < vecr.size(); i++){
        sum += a[vecr[i]].fi;
        cntr++;
    }
    int m = n/2;
    if(cntl > m) return 2;
    if(cntr > m) return 1;
    for(int i = 0; i < vecm.size(); i++){
        if(cntl < m){
            sum += a[vecm[i]].fi;
            cntl++;
        }
        else{
            sum += x;
            cntr++;
        }
    }
    //2��С 1���� 0ok
    if(sum > s) return 2;
    return 0;
}

int main(){
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        cin >> n >> s;
        for(int i = 0; i < n; i++){
            cin >> a[i].fi >> a[i].se;
        }
        sort(a, a+n);
        ll l = a[n/2].fi, r = a[n-1].se, mid, ans = 0;
        while(l <= r){
            mid = (l+r)/2;
            int ck = check(mid);
            if(ck == 0){
                ans = mid;
                l = mid + 1;
            }
            else if(ck == 1) l = mid + 1;
            else r = mid - 1;
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
#define ll long long
#define fst first
#define scd second
using namespace std;
const int maxn = 2e5+5;
pair<ll, ll>a[maxn];
int n;
ll s;

int check(ll x){
    int cnt1 = 0, cnt2 = 0;
    int m = n/2+1;
    ll sum = 0;
    for(int i = 1; i <= m; i++){
        if(a[i].fst > x) return 1;
        if(i == m) break;
        sum += a[i].fst;
    }
    for(int i = m; i <= n; i++){
        if(a[i].scd < x) return 2;
    }
    sum += x*m;
    if(sum > s) return 2;
    return 0;
}

bool cmp(pair<ll, ll>A, pair<ll, ll>B){
    if(A.scd == B.scd) return A.fst < B.fst;
    return A.scd < B.scd;
}

int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> n >> s;
        for(int i = 1; i <= n; i++){
            cin >> a[i].fst >> a[i].scd;
        }
        sort(a+1, a+n+1, cmp);
        ll l = a[1].fst, r = a[n].scd, mid, ans = 0;
        while(l <= r){
            mid = (l+r)/2;
            int ck = check(mid);
            if(ck == 0){
                ans = mid;
                l = mid + 1;
            }
            else if(ck == 1) l = mid + 1;
            else r = mid - 1;
        }
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+1;
vector<pair<int,int>>v;
int T,n,l,r;
ll s;
bool cmp(const pair<int,int>&a,const pair<int,int>&b)
{
	return a.first==b.first?a.second>b.second:a.first>b.first;
}
bool check(ll x)//看看是否有n/2+1个数大于x
{
	ll cnt=0,sum=0;
	for(int i=0;i<v.size();++i)
	{
		if(v[i].first<=x&&x<=v[i].second)
		{
			if(cnt<n/2+1) cnt++,sum+=x;
			else sum+=v[i].first;
		}
		else if(v[i].first>x) cnt++,sum+=v[i].first;
		else sum+=v[i].first;
	}
	return cnt==(n/2+1)&&sum<=s;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		v.clear();
		scanf("%d%lld",&n,&s);
		for(int i=1;i<=n;++i) {
			scanf("%d%d",&l,&r);
			v.push_back({l,r});
		}
		sort(v.begin(),v.end(),cmp);
		ll l=v[n/2].first,r=s,ans=l;
		while(l<=r)
		{
			ll mid=(l+r)>>1;
			if(check(mid)) l=mid+1,ans=mid;
			else r=mid-1;
		}
		printf("%lld\n",ans);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+1;
vector<pair<int,int>>v;
int T,n,l,r;
ll s;
bool cmp(const pair<int,int>&a,const pair<int,int>&b)
{
	return a.first==b.first?a.second>b.second:a.first>b.first;
}
bool check(int x)//看看是否有n/2+1个数大于x
{
	ll cnt=0,sum=0;
	for(int i=0;i<v.size();++i)
	{
		if(v[i].first<=x&&x<=v[i].second)
		{
			if(cnt<n/2+1) cnt++,sum+=x;
			else sum+=v[i].first;
		}
		else if(v[i].first>x) cnt++,sum+=v[i].first;
		else sum+=v[i].first;
	}
	return cnt==(n/2+1)&&sum<=s;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		v.clear();
		scanf("%d%lld",&n,&s);
		for(int i=1;i<=n;++i) {
			scanf("%d%d",&l,&r);
			v.push_back({l,r});
		}
		sort(v.begin(),v.end(),cmp);
		int l=v[n/2+1].first,r=s;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if(check(mid)) l=mid+1;
			else r=mid-1;
		}
		printf("%d\n",r);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+1;
vector<pair<int,int>>v;
int T,n,l,r;
ll s;
bool cmp(const pair<int,int>&a,const pair<int,int>&b)
{
	return a.first==b.first?a.second>b.second:a.first>b.first;
}
bool check(ll x)//看看是否有n/2+1个数大于x
{
	ll cnt=0,sum=0;
	for(int i=0;i<v.size();++i)
	{
		if(v[i].first<=x&&x<=v[i].second)
		{
			if(cnt<n/2+1) cnt++,sum+=x;
			else sum+=v[i].first;
		}
		else if(v[i].first>x) cnt++,sum+=v[i].first;
		else sum+=v[i].first;
	}
	return cnt==(n/2+1)&&sum<=s;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		v.clear();
		scanf("%d%lld",&n,&s);
		for(int i=1;i<=n;++i) {
			scanf("%d%d",&l,&r);
			v.push_back({l,r});
		}
		sort(v.begin(),v.end(),cmp);
		ll l=v[n/2].first,r=s;
		while(l<=r)
		{
			ll mid=(l+r)>>1;
			if(check(mid)) l=mid+1;
			else r=mid-1;
		}
		printf("%lld\n",r);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+1;
vector<pair<int,int>>v;
int T,n,l,r;
ll s;
bool cmp(const pair<int,int>&a,const pair<int,int>&b)
{
	return a.first==b.first?a.second>b.second:a.first>b.first;
}
bool check(int x)
{
	int cnt=0;
	ll sum=s;
	for(int i=0;i<v.size();++i)
	{
		if(v[i].first>=x) cnt++;
		else if(v[i].second>=x&&sum>=x-v[i].first) cnt++,sum-=x-v[i].first;
	}
	return cnt>=n/2+1;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		v.clear();
		scanf("%d%lld",&n,&s);
		for(int i=1;i<=n;++i) {
			scanf("%d%d",&l,&r);
			v.push_back({l,r});s-=l;
		}
		sort(v.begin(),v.end(),cmp);
		int l=1,r=1e9;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if(check(mid)) l=mid+1;
			else r=mid-1;
		}
		printf("%d\n",r);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+1;
vector<pair<int,int>>v;
int T,n,l,r;
ll s;
bool cmp(const pair<int,int>&a,const pair<int,int>&b)
{
	return a.first==b.first?a.second>b.second:a.first>b.first;
}
bool check(int x)//看看是否有n/2+1个数大于x
{
	ll cnt=0,sum=0;
	for(int i=0;i<v.size();++i)
	{
		if(v[i].first<=x&&x<=v[i].second)
		{
			if(cnt<n/2+1) cnt++,sum+=x;
			else sum+=v[i].first;
		}
		else if(v[i].first>x) cnt++,sum+=v[i].first;
		else sum+=v[i].first;
	}
	return cnt==(n/2+1)&&sum<=s;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		v.clear();
		scanf("%d%lld",&n,&s);
		for(int i=1;i<=n;++i) {
			scanf("%d%d",&l,&r);
			v.push_back({l,r});
		}
		sort(v.begin(),v.end(),cmp);
		int l=1,r=1e9;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if(check(mid)) l=mid+1;
			else r=mid-1;
		}
		printf("%d\n",r);
	}
}


/* LittleFall : Hello! */
#include <bits/stdc++.h>
using namespace std; using ll = long long; inline ll read();
const int M = 500016, MOD = 1000000007;

ll n, s;
pair<ll, ll> save[M];
/*
问：想达到中位数m是否不会超过s
需要满足三个条件：
1. 有n/2个小于等于m的（一定满足，因为从save[n/2+1].first开始)
2. 有n/2个大于等于m的
3. 最小的和不会超过s
*/
bool check(ll m)
{
	ll cnt = n/2+1, sum = 0;
	//从大到小排序
	for(int i=1; i<=n; ++i)
	{
		//碰到能包裹m的区间，就设为m，大于等于它的区间又少了一个
		if(save[i].first<=m && save[i].second>=m)
		{
			if(cnt>0)
			{
				sum += m;
				--cnt;
			}
			else
				sum += save[i].first;
		}
		else if(save[i].first>m)
		{
			sum += save[i].first;
			--cnt;
		}
		else
		{
			sum += save[i].first;
		}
	}
	return cnt==0 && sum<=s;
}
int main(void)
{
	#ifdef _LITTLEFALL_
	freopen("in.txt","r",stdin);
    #endif

	int T = read();
	while(T--)
	{
		n = read(), s = read();
		for(int i=1; i<=n; ++i)
		{
			save[i].first = read();
			save[i].second = read();
		}
		sort(save+1, save+n+1, greater<pair<ll,ll>>());

		ll l = save[n/2+1].first, r = s, ans = l;
		while(l<=r)
		{
			ll m = (l+r)>>1;
			if(check(m))
			{
				l = m+1;
				ans = m;
			}
			else
				r = m-1;
		}
		cout << ans << "\n";
	}

    return 0;
}

inline ll read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+1;
vector<pair<int,int>>v;
int T,n,l,r;
ll s;
bool cmp(const pair<int,int>&a,const pair<int,int>&b)
{
	return a.first==b.first?a.second>b.second:a.first>b.first;
}
bool check(int x)//看看是否有n/2+1个数大于x
{
	ll cnt=0,sum=0;
	for(int i=0;i<v.size();++i)
	{
		if(v[i].first<=x&&x<=v[i].second)
		{
			if(cnt<n/2+1) cnt++,sum+=x;
			else sum+=v[i].first;
		}
		else if(v[i].first>x) cnt++,sum+=v[i].first;
		else sum+=v[i].first;
	}
	return cnt==(n/2+1)&&sum<=s;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		v.clear();
		scanf("%d%lld",&n,&s);
		for(int i=1;i<=n;++i) {
			scanf("%d%d",&l,&r);
			v.push_back({l,r});
		}
		sort(v.begin(),v.end(),cmp);
		int l=v[n/2+1].first,r=s,ans=l;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if(check(mid)) l=mid+1,ans=mid;
			else r=mid-1;
		}
		printf("%d\n",ans);
	}
}


/* LittleFall : Hello! */
#include <bits/stdc++.h>
using namespace std; using ll = long long; inline ll read();
const int M = 500016, MOD = 1000000007;

ll n, s;
vector<pair<ll, ll>> save;
bool cmp(const pair<int,int>&a,const pair<int,int>&b)
{
	return a.first==b.first?a.second>b.second:a.first>b.first;
}
bool check(ll m)
{
	ll cnt = n/2+1, sum = 0;
	//从大到小排序
	for(int i=0; i<save.size(); ++i)
	{
		//碰到能包裹m的区间，就设为m，大于等于它的区间又少了一个
		if(save[i].first<=m && save[i].second>=m)
		{
			if(cnt>0)
			{
				sum += m;
				--cnt;
			}
			else
				sum += save[i].first;
		}
		else if(save[i].first>m)
		{
			sum += save[i].first;
			--cnt;
		}
		else
		{
			sum += save[i].first;
		}
	}
	return cnt==0 && sum<=s;
}
int main(void)
{
	#ifdef _LITTLEFALL_
	freopen("in.txt","r",stdin);
    #endif

	int T = read();
	while(T--)
	{
		save.clear();
		n = read(), s = read();
		for(int i=1; i<=n; ++i)
		{
			ll x = read();
			ll y = read();
			save.push_back({x,y});
		}
		sort(save.begin(),save.end(),cmp);
		ll l = save[n/2].first, r = s, ans = l;
		while(l<=r)
		{
			ll m = (l+r)>>1;
			if(check(m))
			{
				l = m+1;
				ans = m;
			}
			else
				r = m-1;
		}
		cout << ans << "\n";
	}

    return 0;
}

inline ll read(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+1;
vector<pair<int,int>>v;
int T,n,l,r;
ll s;
bool cmp(const pair<int,int>&a,const pair<int,int>&b)
{
	return a.first==b.first?a.second>b.second:a.first>b.first;
}
bool check(int x)
{
	int cnt=0;
	ll sum=s;
	for(int i=v.size()-1;i>=0;--i)
	{
		if(v[i].first>=x) cnt++;
		else if(v[i].second>=x&&sum>=x-v[i].first) cnt++,sum-=x-v[i].first;
	}
	return cnt>=n/2+1;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		v.clear();
		scanf("%d%lld",&n,&s);
		for(int i=1;i<=n;++i) {
			scanf("%d%d",&l,&r);
			v.push_back({l,r});s-=l;
		}
		sort(v.begin(),v.end(),cmp);
		int l=1,r=1e9;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if(check(mid)) l=mid+1;
			else r=mid-1;
		}
		printf("%d\n",r);
	}
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int n,s;

struct mystr
{

	int l,r;

}a[200010];

bool cmp(mystr a,mystr b)
{

	if(a.l==b.l) return a.r<b.r;
	else return a.l<b.l;

}

int check(int m)
{
	int sum=0,count=(n+1)/2;
	for(int i=1;i<=n;i++)
	{
		//cout<<a[i].l<<"@"<<m<<endl;
		if(a[i].l>m){sum+=a[i].l;count--;}
		if(a[i].r<m)
		{
			sum+=a[i].l;
		}
		if(a[i].l<=m&&m<=a[i].r)
		{
			count--;
			sum+=m;
		}
	}
//	cout<<count<<'!'<<m<<endl;
	if(count>0)
		return s+111;
	else
		return sum;

}

int main()
{
	//freopen("D.in","r",stdin);
	int T;
	cin>>T;
	//cout<<T;
	for(int k=1;k<=T;k++)
	{
		cin>>n>>s;
		//cout<<n<<s;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i].l>>a[i].r;
		}
		sort(a+1,a+n+1,cmp);
	//	for(int i=1;i<=n;i++)
	//	{
	//		cout<<a[i].l<<a[i].r;
	//	}

		if(n==1)
		{
			if(s>a[1].r)
				cout<<a[1].r<<endl;
			else
				cout<<s<<endl;
			continue;
		}
		int l=a[n/2].l+1,r=s,mid=(l+r)/2;
		while(l<=r)
		{
			mid=(l+r)/2;
			if(check(mid)<=s)
			{
				l=mid+1;
			}
			else
			{
				r=mid-1;
			}
		//	cout<<l<<' '<<r<<endl;
		}
		cout<<l-1<<endl;
		//cout<<endl;
	//	int k;
	//	cin>>k;
	}

	return 0;
}
/*
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

*/


#pragma GCC optimize("Ofast")
#include<cstdio>
#include<queue>
#include<algorithm>
const int N=2e5+10;
std::priority_queue<int>q;
int t,n;long long s;
struct node{
	int l,r;
	inline void read(){scanf("%d%d",&l,&r);}
	inline bool operator < (const node &x) const
	{
		return l<x.l;
	}
}a[N];
bool pd(int x,int y)
{
	int i,m=x-(n>>1)-1;
	for(i=1;i<x;i++)
	if(a[i].r>=y)
	q.push(a[i].l);
	if(q.size()<m){while(!q.empty())q.pop();return false;}
	long long ans=0;
	for(i=1;i<=m;i++)
	ans+=y-q.top(),
	q.pop();
	while(!q.empty())q.pop();
	if(ans>s)return false;
	return true;
}
int main()
{
	int i,l,r,m,x;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%lld",&n,&s);
		for(i=1;i<=n;i++)
		a[i].read(),s-=a[i].l;
		std::sort(a+1,a+1+n);
		l=(n>>1)+2;r=n;
		while(l<r)
		{
			m=(l+r+1)>>1;
			if(pd(m,a[m].l))l=m;
			else r=m-1;
		}
		x=r;l=a[x].l;r=x!=n?a[x+1].l-1:1e9;
		while(l<r)
		{
			m=(l+r+1)>>1;
			if(pd(x+1,m))l=m;
			else r=m-1;
		}
		printf("%d\n",l);
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int n,s;

struct mystr
{

	int l,r;

}a[200010];

bool cmp(mystr a,mystr b)
{

	if(a.l==b.l) return a.r<b.r;
	else return a.l<b.l;

}

int check(int m)
{
	int sum=0,count=(n)/2+1;
	for(int i=1;i<=n;i++)
	{
		//cout<<a[i].l<<"@"<<m<<endl;

		if(a[i].l<=m&&m<=a[i].r&&count)
		{
			count--;
			sum+=m;
		}
		else
		{
			if(a[i].l>m){sum+=a[i].l;count--;}
			if(a[i].r<m)
			{
				sum+=a[i].l;
			}
		}
	}
//	cout<<count<<'!'<<m<<endl;
	if(count>0)
		return s+111;
	else
		return sum;

}

int main()
{
	//freopen("D.in","r",stdin);
	int T;
	cin>>T;
	//cout<<T;
	for(int k=1;k<=T;k++)
	{
		cin>>n>>s;
		//cout<<n<<s;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i].l>>a[i].r;
		}
		sort(a+1,a+n+1,cmp);
	//	for(int i=1;i<=n;i++)
	//	{
	//		cout<<a[i].l<<a[i].r;
	//	}

	/*	if(n==1)
		{
			if(s>a[1].r)
				cout<<a[1].r<<endl;
			else
				cout<<s<<endl;
			continue;
		}*/
		int l=a[(n-1)/2].l+1,r=s,mid=(l+r)/2;
		while(l<=r)
		{
			mid=(l+r)/2;
			if(check(mid)<=s)
			{
				l=mid+1;
			}
			else
			{
				r=mid-1;
			}
		//	cout<<l<<' '<<r<<endl;
		}
		cout<<l-1<<endl;
		//cout<<endl;
	//	int k;
	//	cin>>k;
	}

	return 0;
}
/*
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

*/


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 10;
struct data {
  int l, r;
} a[N], b[N];

bool cmp(data A, data B) {
  return A.l == B.l ? A.r < B.r : A.l < B.l;
}

int n;
ll lmt;

bool check(int x) {
  int res = 0;
  ll sum = 0;
  for(int i = 1; i <= n; ++i) {
    if(a[i].l <= x && x <= a[i].r) {
      b[++res] = a[i];
    } else if(a[i].l > x) {
      b[++res] = a[i];
    } else {
      sum += a[i].l;
    }
  }
  if(res <= n / 2) return false;
  sort(b + 1, b + 1 + res, cmp);
  int yu = n / 2 - (n - res);
  for(int i = 1; i <= yu; ++i) {
    if(b[i].l > x) return true;
    sum += b[i].l;
  }
  for(int i = yu + 1; i <= res; ++i) {
    sum += x;
  }
  return sum <= lmt;
}

int main() {
  int T;
  scanf("%d", &T);
  for(int o = 1; o <= T; ++o) {
    scanf("%d%lld", &n, &lmt);
    for(int i = 1; i <= n; ++i) {
      scanf("%d%d", &a[i].l, &a[i].r);
    }
    int l = 0, r = 1e9, ans = 0;
    while(l <= r) {
      int mid = (l + r) >> 1;
      if(check(mid)) ans = mid, l = mid + 1;
      else r = mid - 1;
    }
    // cout << check(1) << endl;
    printf("%d\n", ans);
  }
  return 0;
}


#include <stdio.h>
#include <cstring>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
struct node
{
	int l, r;
	bool operator < (struct node a)
	{
		return l < a.l;
	}
}a[200009];
typedef long long ll;
int T, n, l, r, mid;
ll sum;
bool check(int x)
{
	vector <struct node> ve;
	int num = 0;
	ll res = 0;
	for(int i = 1; i <= n; i++)
		if(a[i].r<x)
		{
			res += a[i].l;
			num++;
		}
		else if(a[i].l>x)
		{
			res += a[i].l;
		}else ve.push_back(a[i]);

	if(num>=n/2+1) return false;

	int size = ve.size();

	for(int i = 0; i < size; i++)
	if(num<n/2)
	{
		res += a[i].l;
		num++;
	}else res += x;

	return res <= sum;
}
bool cmp(struct node a, struct node b)
{
	return a.r  < b.r;
}
int main()
{
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d %lld", &n, &sum);
		for(int i = 1; i <= n; i++)
		scanf("%d %d", &a[i].l, &a[i].r);

		sort(a+1, a+1+n);
		l = a[n/2+1].l;
		sort(a+1, a+1+n, cmp);
		r = a[n/2+1].r;
		sort(a+1, a+1+n);
		while(l<=r)
		{
			mid = (l+r) / 2;
			if(check(mid))
			{
		//		printf("%d true\n", mid);
				if(!check(mid+1)) break;
				l = mid+1;
			}else
			{
		//		printf("%d false\n", mid);
				r = mid - 1;
			}
		}

		printf("%d\n", mid);
	}
}


#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define int long long
struct node{
	int l,r;
}p[100005];
int n,s,sum,u,mid;
int x,w[100005];
bool cmp(node a,node b) {
	if(a.l<=b.l) return false;
	else return true;
}
int g(int x){
	int ans=0;
	for(int i=1;i<=x;i++){
		if(s-(mid-p[w[i]].l)>=0)ans++,s-=(mid-p[w[i]].l);
	}
	return ans;
}
int f(int minn,int maxn){
	if(minn==maxn)return minn;
	s=u;
	x=0;
	sum=0;
	memset(w,0,sizeof(w));
	mid=minn+maxn>>1;
	for(int i=1;i<=n;i++){
		if(p[i].l>=mid){
			sum++;
			s-=p[i].l;
		}
		else if(p[i].r<mid){
			s-=p[i].l;
		}
		else{
			s-=p[i].l;
			x++;
			w[x]=i;
		}
	}
	sum+=g(x);
	if(sum>=n/2+1)return f(mid+1,maxn);
	else return f(minn,mid-1);
}
signed main(){
	int t;
	scanf("%lld",&t);
	while(t--){
		int minn=1,maxn,sum=0;
		scanf("%lld%lld",&n,&s);
		for(int i=1;i<=n;i++){
			scanf("%lld%lld",&p[i].l,&p[i].r);
			maxn=max(maxn,p[i].r);
		}
		u=s;
		sort(p+1,p+n+1,cmp);
		printf("%lld\n",f(minn,maxn));
	}
	return 0;
}


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

int n;
    int s;
struct node
{
    int l;
    int r;
}p[100005];

bool cmp(node a,node b)
{
    return a.l<b.l;
}

bool judge(ll x)
{
    int m[100005],k=0;
    ll sum=0;
    int a=0,b=0,c=0;
    for(int i=0;i<n;i++)
    {
        if(p[i].r<x)sum+=p[i].l,a++;
        else if(p[i].l>x)sum+=p[i].l,b++;
        else m[k++]=i,c++;
    }
    if(a>n/2||b>n/2)return false;
    for(int i=0;i<n/2-a;i++)sum+=p[m[i]].l;
    sum+=x*(n/2+1-b);
    if(sum<=s)return true;
    else return false;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>s;
        for(int i=0;i<n;i++)
            cin>>p[i].l>>p[i].r;
        sort(p,p+n,cmp);
        ll l=0,r=1e9+5;
        while(l<=r)
        {
        ll mid =(l+r)/2;
        if(judge(mid))l=mid+1;
        else r=mid-1;
        }
        cout<<l-1<<endl;
    }
}


#include <stdio.h>
#include <cstring>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
struct node
{
	int l, r;
	bool operator < (struct node a)
	{
		return l < a.l;
	}
}a[200009];
typedef long long ll;
int T, n, l, r, mid;
ll sum;
bool check(int x)
{
	vector <struct node> ve;
	set <int> st;
	ll res = 0;
	int id = 0;
	a[0].l = 0;

	for(int i = 1; i <= n; i++)
		if(a[i].r>=x && a[id].l < a[i].l)
		id = i;

	if(!id) return false;
	res += x;
	for(int i = 1; i <= n; i++)
	{
		if(id==i) continue;
		ve.push_back(a[i]);
	}
	sort(ve.begin(), ve.end());
	for(int i = 1; i <= n-1; i++)
	{
		if(i > n/2)
		{
			if(ve[i-1].r<x)
			{
				if(st.empty()) return false;
				res -= *(--st.end()); st.erase(--st.end());
				res += ve[i-1].l;
				res += x;
			}else
			{
				res += max(ve[i-1].l, x);
			}
		}else
		{
			if(ve[i-1].l>x) return false;
			res += ve[i-1].l;
			if(ve[i-1].r >= x) st.insert(ve[i-1].l);
		}
	}
	if(res <= sum) return true;
	return false;
}
int main()
{
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d %lld", &n, &sum);
		for(int i = 1; i <= n; i++)
		scanf("%d %d", &a[i].l, &a[i].r);

		l = 0; r = 1e9;
		while(l<=r)
		{
			mid = (l+r) / 2;
			if(check(mid))
			{
				if(!check(mid+1)) break;
				l = mid+1;
			}else r = mid - 1;
		}
		printf("%d\n", mid);
	}
}


#include <stdio.h>
#include <cstring>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
struct node
{
	int l, r;
	bool operator < (struct node a)
	{
		return l < a.l;
	}
}a[200009];
typedef long long ll;
int T, n, l, r, mid;
ll sum;
bool check(int x)
{
	vector <struct node> ve;
	int numl, numr;
	numl = numr = 0;
	ll res = 0;
	for(int i = 1; i <= n; i++)
		if(a[i].r<x)
		{
			res += a[i].l;
			numl++;
		}
		else if(a[i].l>x)
		{
			res += a[i].l;
			numr++;
		}else ve.push_back(a[i]);

	if(numl>=n/2+1) return false;
	if(numr>=n/2+1) return true;

	int size = ve.size();
	for(int i = 0; i < size; i++)
	{
		if(numl<n/2)
		{
			res += a[i].l;
			numl++;
		}else res += x;
	}
	return res <= sum;
}
int main()
{
	scanf("%d", &T);
	while(T--)
	{
		scanf("%d %lld", &n, &sum);
		for(int i = 1; i <= n; i++)
		scanf("%d %d", &a[i].l, &a[i].r);

		l = 0; r = 1e9;
		while(l<=r)
		{
			mid = (l+r) / 2;
			if(check(mid))
			{
		//		printf("%d true\n", mid);
				if(!check(mid+1)) break;
				l = mid+1;
			}else
			{
		//		printf("%d false\n", mid);
				r = mid - 1;
			}
		}
		printf("%d\n", mid);
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t,n,s,ans,mx;
struct cxk{
	ll l,r;
}a[200005];
ll cmp(cxk a,cxk b)
{
	return a.l<b.l;
}
bool check(ll mid)
{
	int cnt1=0,cnt2=0;
    for(int i=1;i<=n;i++){
        if(a[i].l>mid)cnt2++;
        if(a[i].r<mid)cnt1++;
    }
    if(cnt1>n/2)return 0;

    if(1.0*mid*(n/2+1)>s) return 0;

    ll tot=0,cnt=cnt1;
    for(int i=1;i<=n;i++){
        if(a[i].l<=mid && a[i].r>=mid){
            ++cnt;
            if(cnt<=n/2)
                tot+=a[i].l;
            else if(cnt>=n/2+1)
                tot+=mid;
        }
        else tot+=a[i].l;
    }

    if(tot>s)return 0;
    return 1;
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>t;
	while(t--)
	{
		cin>>n>>s;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i].l>>a[i].r;
			mx=max(mx,a[i].r);
		}
		sort(a+1,a+n+1,cmp);
		ll l=a[(n+1)/2].l;
		ll r=mx;
		ans=a[(n+1)/2].l;
		while(l<=r)
		{
			ll mid=(l+r)>>1;
			if(check(mid))
			{
				ans=mid;
				l=mid+1;
			}
			else
				r=mid-1;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<queue>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#define lnt long long
#define rnt register int
using namespace std;
inline void read(int &x)
{
	char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	x=c-'0';
	while((c=getchar())>='0'&&c<='9')
		x=(x<<3)+(x<<1)+c-'0';
}
int T,n;
lnt s;
struct node{
	int l,r;
	bool operator <(const node & o)const
	{
		return r<o.r;
	}
	node(int ll,int rr)
	{
		l=ll,r=rr;
	}
	node(){}
}d[200020];
struct nd{
	int l,r;
	bool operator <(const nd & o)const
	{
		return l>o.l;
	}
	nd(int ll,int rr)
	{
		l=ll,r=rr;
	}
	nd(){}
};
priority_queue <nd> q;
inline int check(int t)
{
	int p=lower_bound(d,d+n,node(0,t))-d;
	if(p>(n>>1)) return 0;
	lnt sal=0;
	for(rnt i=0;i<p;i=-~i) sal+=d[i].l;
	for(rnt i=p;i<n;i=-~i) q.push(nd(d[i].l,d[i].r));
	for(rnt i=p;i<n>>1;i=-~i)
		sal+=q.top().l,q.pop();
	for(rnt i=n>>1;i<n;i=-~i)
		sal+=max(q.top().l,t),q.pop();
	if(sal<=s) return 1;
	else return 0;
}
int main()
{
	read(T);
	for(;T;--T)
	{
		read(n);scanf("%lld",&s);
		for(rnt i=0;i<n;i=-~i)
			read(d[i].l),read(d[i].r);
		sort(d,d+n);
		int l=1,r=1e9;
		while(l<r)
		{
			int mid=(l+r+1)>>1;
			if(check(mid)) l=mid;
			else r=mid-1;
		}
		printf("%d\n",l);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define Re register
typedef pair<long long, long long> AK;
typedef long long ll;
const int maxx=2e5+5;
ll n;
ll s;
ll ans;
int T;
AK a[maxx];
bool operator >(AK a, AK b)
{
    return a.first>b.first;
}
bool check(ll num)
{
    ll nee=(n>>1)+1;
    ll sum=0;
    for(Re int i=1;i<=n;++i)
    {
        if (nee&&a[i].first<=num&&num<=a[i].second)
        {
            sum+=num;
            nee--;
        }
        else
        {
            sum+=a[i].first;
            if (a[i].first>=num&&nee) nee--;
            }
    }
    if (nee) return 0;
    return sum<=s;
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>T;
    while (T--)
    {
        ans=0;
        cin>>n>>s;
        for(Re int i=1;i<=n;i++) cin>>a[i].first>>a[i].second;
        sort(a+1,a+n+1,greater<AK>());
        ll l=a[(n>>1)+1].first,r=s;
        ans=a[(n>>1)+1].first;
        while (l<=r)//(l<r)不可以
        {
            ll mid=(l+r)>>1;
            if (check(mid))
            {
                ans=mid;
                l=mid+1;

            }
            else r=mid-1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include <stdio.h>
#include <set>
#include <string.h>
using namespace std;
char s[300009], res[300009];
set <int> :: iterator it;
set <int> st[2];
int main()
{
	int T, len;
	scanf("%d", &T);
	while(T--)
	{
		scanf("%s", s+1);
		len = strlen(s+1);
		st[0].clear(); st[1].clear();
		for(int i = 1; i <= len; i++)
		st[(s[i]-'0')%2].insert(i);

		for(int i = 1; i <= len; i++)
		{
			if(st[0].empty())
			{
				res[i] = s[*st[1].begin()];
				st[1].erase(st[1].begin());
				continue;
			}
			if(st[1].empty())
			{
				res[i] = s[*st[0].begin()];
				st[0].erase(st[0].begin());
				continue;
			}

 			if(s[*st[0].begin()] < s[*st[1].begin()])
			{
				res[i] = s[*st[0].begin()];
				st[0].erase(st[0].begin());
			}else{
				res[i] = s[*st[1].begin()];
				st[1].erase(st[1].begin());
			}
		}
		res[len+1] = '\0';

		printf("%s\n", res+1);
	}
}


#pragma GCC optimize("Ofast")
#include<cstdio>
#include<queue>
#include<algorithm>
const int N=2e5+10;
std::priority_queue<int>q;
int t,n;long long s;
struct node{
	int l,r;
	inline void read(){scanf("%d%d",&l,&r);}
	inline bool operator < (const node &x) const
	{
		return l<x.l;
	}
}a[N];
bool pd(int x,int y)
{
	int i,m=x-(n>>1)-1;
	for(i=1;i<x;i++)
	if(a[i].r>=y)
	q.push(a[i].l);
	if(q.size()<m){while(!q.empty())q.pop();return false;}
	long long ans=0;
	for(i=1;i<=m;i++)
	ans+=y-q.top(),
	q.pop();
	while(!q.empty())q.pop();
	if(ans>s)return false;
	return true;
}
int main()
{
	int i,l,r,m,x;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%lld",&n,&s);
		for(i=1;i<=n;i++)
		a[i].read(),s-=a[i].l;
		std::sort(a+1,a+1+n);
		l=(n>>1)+1;r=n;
		while(l<r)
		{
			m=(l+r+1)>>1;
			if(pd(m,a[m].l))l=m;
			else r=m-1;
		}
		x=l;l=a[x].l;r=(x!=n?a[x+1].l-1:1e9);
		while(l<r)
		{
			m=(l+r+1)>>1;
			if(pd(x+1,m))l=m;
			else r=m-1;
		}
		printf("%d\n",l);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,n,s,ans;
struct cxk{
	int l,r;
}a[200005];
int cmp(cxk a,cxk b)
{
	return a.l<b.l;
}
bool check(int x)
{
	int cnt=0,sum=0;
	int t=(n+1)/2;
	for(int i=1;i<=n;i++)
	{
		if(a[i].r>=x&&a[i].l<=x)
		{
			cnt++;
			sum+=x;
		}
		else
		{
			sum+=a[i].l;
			if(a[i].l>=x)
				cnt++;
		}
	}
	if(cnt>=t&&sum<=s) return true;
	else return false;
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>s;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i].l>>a[i].r;
		}
		sort(a+1,a+n+1,cmp);
		int l=a[(n+1)/2].l;
		int r=s;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if(check(mid))
			{
				ans=mid;
				l=mid+1;
			}
			else
				r=mid-1;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long
#define N 200010
using namespace std;

inline int read()
{
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	return x*f;
}

struct node{
	int l,r;
}A[N];

int n,top,stk[N];
ll s;

inline bool check(int x)
{
	ll sum;int a,b,flag;a=b=sum=0;
	for(int i=1;i<=n;++i){
		if (A[i].r<x){
			sum+=(ll)A[i].l,++a;
		}
        else if (A[i].l>x){
        	sum+=(ll)A[i].l,++b;
        }
        else stk[++top]=i;
	}
	if(a*2>n) return 0;
	if(b*2>n) return 1;
	for(int i=1;i<=top;++i){
		if((n/2)<=a+i){
			flag=i;break;
		}
		sum+=(ll)A[i].l;
	}
	sum+=(ll)x*(top-flag+1);
	if(sum<=s) return true;
	return false;
}

inline bool cmp(const node a,const node b)
{
	if(a.l!=b.l) return a.l<b.l;
	return a.r<b.r;
}

int main()
{
	int T=read(),l,r,mid,ans;
	while(T--){
		n=read();scanf("%lld",&s);top=0,l=INF,r=0;
		for(int i=1;i<=n;++i){
			A[i].l=read(),A[i].r=read();
			if(A[i].r>r) r=A[i].r;
			if(A[i].l<l) l=A[i].l;
		}
		sort(A+1,A+1+n,cmp);
		while(l<=r){
			mid=(l+r)>>1,top=0;
			if(check(mid)){
				ans=mid;l=mid+1;
			}
			else r=mid-1;
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

namespace zzc
{
    const int maxn = 2e5+5;
    int t,n,l,r,mid,ans;
	long long s;
    bool vis[maxn];
	struct staf
	{
		int l,r;
	}a[maxn];

	bool cmp(staf a,staf b)
	{
		return a.l==b.l?a.r<b.r:a.l<b.l;
	}

	bool check(int x)
	{
		long long res=0;
		int cnt1=0,cnt2=0;
		memset(vis,false,sizeof(vis));
		for(int i=1;i<=n;i++)
		{
			if(a[i].r<x)
			{
				if((cnt1<<1)>=(n-1))
				{
					return false;
				}
				cnt1++;
				vis[i]=true;
				res+=a[i].l;
			}
			else if(a[i].l>x)
			{
				if((cnt2<<1)>=(n-1)) continue;
				cnt2++;
				vis[i]=true;
				res+=a[i].l;
			}
		}
		for(int i=1;i<=n;i++)
		{
			if(vis[i]) continue;
			if((cnt1<<1)>=(n-1))
			{
				res+=max(a[i].l,x);
			}
			else
			{
				res+=min(a[i].l,x);
			}
		}
		return res<=s;
	}

	void work()
	{
		scanf("%d",&t);
		while(t--)
		{
			l=0;r=0;
			scanf("%d%d",&n,&s);
			for(int i=1;i<=n;i++)
			{
				scanf("%d%d",&a[i].l,&a[i].r);
				r=max(r,a[i].r);
			}
			sort(a+1,a+n+1,cmp);
			while(l<=r)
			{
				mid=(l+r)>>1;
				if(check(mid))
				{
					ans=mid;
					l=mid+1;
				}
				else r=mid-1;
			}
			printf("%d\n",ans);
		}
	}

}

int main()
{
	zzc::work();
	return 0;
 }


#include<bits/stdc++.h>

using namespace std;

namespace zzc
{
    const int maxn = 2e5+5;
    int t,n,l,r,mid,ans;
	long long s;
    bool vis[maxn];
	struct staf
	{
		int l,r;
	}a[maxn];

	bool cmp(staf a,staf b)
	{
		return a.l==b.l?a.r<b.r:a.l<b.l;
	}

	bool check(int x)
	{
		long long res=0;
		int cnt1=0,cnt2=0;
		memset(vis,false,sizeof(vis));
		for(int i=1;i<=n;i++)
		{
			if(a[i].r<x)
			{
				if((cnt1<<1)>=(n-1))
				{
					return false;
				}
				cnt1++;
				vis[i]=true;
				res+=a[i].l;
			}
			else if(a[i].l>x)
			{
				cnt2++;
				vis[i]=true;
				res+=a[i].l;
			}
		}
		for(int i=1;i<=n;i++)
		{
			if(vis[i]) continue;
			if((cnt1<<1)>=(n-1))
			{
				res+=max(a[i].l,x);
			}
			else
			{
				res+=min(a[i].l,x);
			}
		}
		return res<=s;
	}

	void work()
	{
		scanf("%d",&t);
		while(t--)
		{
			l=0;r=0;
			scanf("%d%d",&n,&s);
			for(int i=1;i<=n;i++)
			{
				scanf("%d%d",&a[i].l,&a[i].r);
				r=max(r,a[i].r);
			}
			sort(a+1,a+n+1,cmp);
			while(l<=r)
			{
				mid=(l+r)>>1;
				if(check(mid))
				{
					ans=mid;
					l=mid+1;
				}
				else r=mid-1;
			}
			printf("%d\n",ans);
		}
	}

}

int main()
{
	zzc::work();
	return 0;
 }


#include<bits/stdc++.h>

using namespace std;

namespace zzc
{
    const int maxn = 2e5+5;
    long long t,n,l,r,mid,ans;
	long long s;
    bool vis[maxn];
	struct staf
	{
		long long l,r;
	}a[maxn];

	bool cmp(staf a,staf b)
	{
		return a.l==b.l?a.r<b.r:a.l<b.l;
	}

	bool check(int x)
	{
		long long res=0;
		int cnt1=0,cnt2=0;
		memset(vis,false,sizeof(vis));
		for(int i=1;i<=n;i++)
		{
			if(a[i].r<x)
			{
				cnt1++;
				vis[i]=true;
				res+=a[i].l;
			}
			else if(a[i].l>x)
			{
				cnt2++;
				vis[i]=true;
				res+=a[i].l;
			}
		}
		if((cnt1<<1)>n||(cnt2<<1)>n) return false;
		for(int i=1;i<=n;i++)
		{
			if(vis[i]) continue;
			if((cnt1<<1)>=(n-1)) break;
			res+=a[i].l;
			cnt1++;
		}
		res+=(((n-1)>>1)-cnt2+1)*x;
		return res<=s;
	}

	void work()
	{
		scanf("%lld",&t);
		while(t--)
		{
			l=0;r=0;
			scanf("%lld%lld",&n,&s);
			for(int i=1;i<=n;i++)
			{
				scanf("%lld%lld",&a[i].l,&a[i].r);
				r=max(r,a[i].r);
			}
			sort(a+1,a+n+1,cmp);
			l=a[((n-1)>>1)+1].l;
			while(l<=r)
			{
				mid=(l+r)>>1;
				if(check(mid))
				{
					ans=mid;
					l=mid+1;
				}
				else r=mid-1;
			}
			printf("%lld\n",ans);
		}
	}

}

int main()
{
	zzc::work();
	return 0;
 }

 /*
 1
19 1175
44 87
68 100
93 98
76 79
10 58
74 99
87 97
23 75
62 73
100 100
71 76
73 87
76 82
44 84
54 90
2 82
67 99
85 97
66 88
 */


#include<bits/stdc++.h>

using namespace std;

namespace zzc
{
    const int maxn = 2e5+5;
    long long t,n,l,r,mid,ans,s;
	long long id[maxn];
    bool vis[maxn];
	struct staf
	{
		long long l,r;
	}a[maxn];

	bool cmp(staf a,staf b)
	{
		return a.l==b.l?a.r<b.r:a.l<b.l;
	}

	bool check(int x)
	{
		long long res=0;
		int cnt1=0,cnt2=0,cnt3=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i].r<x)
			{
				cnt1++;
				vis[i]=true;
				res+=a[i].l;
			}
			else if(a[i].l>x)
			{
				cnt2++;
				vis[i]=true;
				res+=a[i].l;
			}
			else id[++cnt3]=i;
		}
		if((cnt1<<1)>n||(cnt2<<1)>n) return false;
		for(int i=1;i+cnt1<=n/2;i++)
		{
			res+=a[id[i]].l;
		}
		res+=(((n-1)>>1)-cnt2+1)*x;
		return res<=s;
	}

	void work()
	{
		scanf("%lld",&t);
		while(t--)
		{
			l=0;r=0;
			scanf("%lld%lld",&n,&s);
			for(int i=1;i<=n;i++)
			{
				scanf("%lld%lld",&a[i].l,&a[i].r);
				r=max(r,a[i].r);
			}
			sort(a+1,a+n+1,cmp);
			l=a[((n-1)>>1)+1].l;
			while(l<=r)
			{
				mid=(l+r)>>1;
				if(check(mid))
				{
					ans=mid;
					l=mid+1;
				}
				else r=mid-1;
			}
			printf("%lld\n",ans);
		}
	}

}

int main()
{
	zzc::work();
	return 0;
 }

 /*
 1
19 1175
44 87
68 100
93 98
76 79
10 58
74 99
87 97
23 75
62 73
100 100
71 76
73 87
76 82
44 84
54 90
2 82
67 99
85 97
66 88
 */


#include<bits/stdc++.h>

using namespace std;

namespace zzc
{
    const int maxn = 2e5+5;
    long long t,n,l,r,mid,ans,s;
	long long id[maxn];
    bool vis[maxn];
	struct staf
	{
		long long l,r;
	}a[maxn];

	bool cmp(staf a,staf b)
	{
		return a.l==b.l?a.r<b.r:a.l<b.l;
	}

	bool check(int x)
	{
		long long res=0;
		int cnt1=0,cnt2=0,cnt3=0;
		memset(vis,false,sizeof(vis));
		for(int i=1;i<=n;i++)
		{
			if(a[i].r<x)
			{
				cnt1++;
				vis[i]=true;
				res+=a[i].l;
			}
			else if(a[i].l>x)
			{
				cnt2++;
				vis[i]=true;
				res+=a[i].l;
			}
			else id[++cnt3]=i;
		}
		if((cnt1<<1)>n||(cnt2<<1)>n) return false;
		for(int i=1;i+cnt1<=n/2;i++)
		{
			res+=a[id[i]].l;
		}
		res+=(((n-1)>>1)-cnt2+1)*x;
		return res<=s;
	}

	void work()
	{
		scanf("%lld",&t);
		while(t--)
		{
			l=0;r=0;
			scanf("%lld%lld",&n,&s);
			for(int i=1;i<=n;i++)
			{
				scanf("%lld%lld",&a[i].l,&a[i].r);
				r=max(r,a[i].r);
			}
			sort(a+1,a+n+1,cmp);
			l=a[((n-1)>>1)+1].l;
			while(l<=r)
			{
				mid=(l+r)>>1;
				if(check(mid))
				{
					ans=mid;
					l=mid+1;
				}
				else r=mid-1;
			}
			printf("%lld\n",ans);
		}
	}

}

int main()
{
	zzc::work();
	return 0;
 }

 /*
 1
19 1175
44 87
68 100
93 98
76 79
10 58
74 99
87 97
23 75
62 73
100 100
71 76
73 87
76 82
44 84
54 90
2 82
67 99
85 97
66 88
 */


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

pair<int,int> pp[200000];
int main()
{
    int t;
    cin >> t;
    int s,n;
    while (t--) {
        cin >> n >> s;
        int mm = 0;
        for (int i = 0 ; i < n; i++) {
            cin >> pp[i].first >> pp[i].second;
            if (mm < pp[i].second) {
                mm = pp[i].second;
            }
        }
        //cout << mm << endl;
        int L = 0, R = mm;
        int ans = 0;
        while(L <= R) {
             int M = (L+R)/2;
             vector<int> larger;
             vector<int> lower;
            //cout << L << ' ' << M << ' ' << R << endl;
             for (int i = 0; i < n; i++) {
                 if (pp[i].second < M) {
                     lower.push_back(pp[i].first);
                 } else {
                     larger.push_back(pp[i].first);
                 }
             }
             if (lower.size() > (n / 2)) {
                 R = M -1;
             } else {
                 int total = 0;
                 sort(lower.begin(), lower.end());
                 for (int i = 0; i < n / 2 && i < lower.size(); i++) {
                     total += lower[i];
                 }
                 for (int i = n / 2; i < lower.size(); i++) {
                     total += M;
                 }
                 for (int i = 0; i < larger.size(); i++) {
                     if (M < larger[i]) {
                         total += larger[i];
                     } else {
                         total += M;
                     }
                 }
                 if (total > s) {
                     R = M - 1;
                 } else if (total == s) {
                     ans = M;
                     break;
                 } else {
                     ans = M;
                     L = M + 1;
                 }
             }
        }
        cout << ans << endl;
    }
    return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

pair<long long int,long long int> pp[200000];
int main()
{
    int t;
    cin >> t;
    long long int s,n;
    while (t--) {
        cin >> n >> s;
        long long int mm = 0;
        for (int i = 0 ; i < n; i++) {
            cin >> pp[i].first >> pp[i].second;
            if (mm < pp[i].second) {
                mm = pp[i].second;
            }
        }
        //cout << mm << endl;
        long long int L = 0, R = mm;
        long long int ans = 0;
        while(L <= R) {
             long long int M = (L+R)/2;
             vector<long long int> larger;
             vector<long long int> lower;
            //cout << L << ' ' << M << ' ' << R << endl;
             for (int i = 0; i < n; i++) {
                 if (pp[i].second < M) {
                     lower.push_back(pp[i].first);
                 } else {
                     larger.push_back(pp[i].first);
                 }
             }
             if (lower.size() > (n / 2)) {
                 R = M -1;
             } else {
                 long long int total = 0;
                 sort(lower.begin(), lower.end());

                 for (int i = 0; i <= n / 2 && i < lower.size(); i++) {
                     total += lower[i];
                 }
                 for (int i = 0; i < larger.size(); i++) {
                     if (M < larger[i]) {
                         total += larger[i];
                     } else {
                         total += M;
                     }
                 }
                 if (total > s) {
                     R = M - 1;
                 } else if (total == s) {
                     if (ans < M) {
                     ans = M;
                     }
                     L = M + 1;
                 } else {
                     if (ans < M) {
                     ans = M;
                     }
                     L = M + 1;
                 }
             }
        }
        cout << ans << endl;
    }
    return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

pair<long long int,long long int> pp[200000];
int main()
{
    int t;
    cin >> t;
    long long int s,n;
    while (t--) {
        cin >> n >> s;
        long long int mm = 0;
        for (int i = 0 ; i < n; i++) {
            cin >> pp[i].first >> pp[i].second;
            if (mm < pp[i].second) {
                mm = pp[i].second;
            }
        }
        //cout << mm << endl;
        long long int L = 0, R = mm;
        long long int ans = 0;
        while(L <= R) {
             long long int M = (L+R)/2;
             vector<long long int> larger;
             vector<long long int> lower;
            //cout << L << ' ' << M << ' ' << R << endl;
             for (int i = 0; i < n; i++) {
                 if (pp[i].second < M) {
                     lower.push_back(pp[i].first);
                 } else {
                     larger.push_back(pp[i].first);
                 }
             }
             if (lower.size() > (n / 2)) {
                 R = M -1;
             } else {
                 long long int total = 0;
                 sort(lower.begin(), lower.end());

                 for (int i = 0; i < n / 2 && i < lower.size(); i++) {
                     total += lower[i];
                 }
                 for (int i = n / 2; i < lower.size(); i++) {
                     total += M;
                 }
                 for (int i = 0; i < larger.size(); i++) {
                     if (M < larger[i]) {
                         total += larger[i];
                     } else {
                         total += M;
                     }
                 }
                 if (total > s) {
                     R = M - 1;
                 } else if (total == s) {
                     ans = M;
                     L = M + 1;
                 } else {
                     if (ans < M) {
                     ans = M;
                     }
                     L = M + 1;
                 }
             }
        }
        cout << ans << endl;
    }
    return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

pair<int,int> pp[200000];
int main()
{
    int t;
    cin >> t;
    int s,n;
    while (t--) {
        cin >> n >> s;
        int mm = 0;
        for (int i = 0 ; i < n; i++) {
            cin >> pp[i].first >> pp[i].second;
            if (mm < pp[i].second) {
                mm = pp[i].second;
            }
        }
        cout << mm << endl;
        int L = 0, R = mm;
        int ans = 0;
        while(L <= R) {
             int M = (L+R)/2;
             vector<int> larger;
             vector<int> lower;
            //cout << L << ' ' << M << ' ' << R << endl;
             for (int i = 0; i < n; i++) {
                 if (pp[i].second < M) {
                     lower.push_back(pp[i].first);
                 } else {
                     larger.push_back(pp[i].first);
                 }
             }
             if (lower.size() > (n / 2)) {
                 R = M -1;
             } else {
                 int total = 0;
                 sort(lower.begin(), lower.end());
                 for (int i = 0; i < n / 2 && i < lower.size(); i++) {
                     total += lower[i];
                 }
                 for (int i = n / 2; i < lower.size(); i++) {
                     total += M;
                 }
                 for (int i = 0; i < larger.size(); i++) {
                     if (M < larger[i]) {
                         total += larger[i];
                     } else {
                         total += M;
                     }
                 }
                 if (total > s) {
                     R = M - 1;
                 } else if (total == s) {
                     ans = M;
                     break;
                 } else {
                     ans = M;
                     L = M + 1;
                 }
             }
        }
        cout << ans << endl;
    }
    return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

pair<long long int,long long int> pp[200000];
int main()
{
    int t;
    cin >> t;
    long long int s,n;
    while (t--) {
        cin >> n >> s;
        long long int mm = 0;
        for (int i = 0 ; i < n; i++) {
            cin >> pp[i].first >> pp[i].second;
            if (mm < pp[i].second) {
                mm = pp[i].second;
            }
        }
        //cout << mm << endl;
        long long int L = 0, R = mm;
        long long int ans = 0;
        while(L <= R) {
             long long int M = (L+R)/2;
             vector<long long int> larger;
             vector<long long int> lower;
            //cout << L << ' ' << M << ' ' << R << endl;
             for (int i = 0; i < n; i++) {
                 if (pp[i].second < M) {
                     lower.push_back(pp[i].first);
                 } else {
                     larger.push_back(pp[i].first);
                 }
             }
             if (lower.size() > (n / 2)) {
                 R = M -1;
             } else {
                 long long int total = 0;
                 sort(lower.begin(), lower.end());
                 for (int i = 0; i < n / 2 && i < lower.size(); i++) {
                     total += lower[i];
                 }
                 for (int i = n / 2; i < lower.size(); i++) {
                     total += M;
                 }
                 for (int i = 0; i < larger.size(); i++) {
                     if (M < larger[i]) {
                         total += larger[i];
                     } else {
                         total += M;
                     }
                 }
                 if (total > s) {
                     R = M - 1;
                 } else if (total == s) {
                     ans = M;
                     break;
                 } else {
                     if (ans < M) {
                     ans = M;
                     }
                     L = M + 1;
                 }
             }
        }
        cout << ans << endl;
    }
    return 0;
}


/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

pair<long long int,long long int> pp[200000];
int main()
{
    int t;
    cin >> t;
    long long int s,n;
    while (t--) {
        cin >> n >> s;
        long long int mm = 0;
        for (int i = 0 ; i < n; i++) {
            cin >> pp[i].first >> pp[i].second;
            if (mm < pp[i].second) {
                mm = pp[i].second;
            }
        }
        //cout << mm << endl;
        long long int L = 0, R = mm;
        long long int ans = 0;
        while(L <= R) {
             long long int M = (L+R)/2;
             vector<long long int> larger;
             vector<long long int> lower;
            //cout << L << ' ' << M << ' ' << R << endl;
             for (int i = 0; i < n; i++) {
                 if (pp[i].second < M) {
                     lower.push_back(pp[i].first);
                 } else {
                     larger.push_back(pp[i].first);
                 }
             }
             if (lower.size() > (n / 2)) {
                 R = M -1;
             } else {
                 long long int total = 0;
                 sort(lower.begin(), lower.end());

                 for (int i = 0; i < n / 2 && i < lower.size(); i++) {
                     total += lower[i];
                 }
                 for (int i = n / 2; i < lower.size(); i++) {
                     total += M;
                 }
                 for (int i = 0; i < larger.size(); i++) {
                     if (M < larger[i]) {
                         total += larger[i];
                     } else {
                         total += M;
                     }
                 }
                 if (total > s) {
                     R = M - 1;
                 } else if (total == s) {
                     if (ans < M) {
                     ans = M;
                     }
                     L = M + 1;
                 } else {
                     if (ans < M) {
                     ans = M;
                     }
                     L = M + 1;
                 }
             }
        }
        cout << ans << endl;
    }
    return 0;
}