// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define vi vector<ll>
#define vb vector<bool>
#define pii pair<ll,ll>
#define mod 1000000007
#define pll pair<ll,ll>
#define rep(i,n) for(ll i=0;i<n;i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define per1(i,n) for(ll i=n;i>0;i--)
#define prll(a,n) for(ll i=0;i<n;i++){ cout<<a[i]<<" "; }
#define ain(a) for(auto &i:a) cin>>i
#define mms(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
// cout << fixed << setprecision(0) << pi <<" "<<npi<<endl;

ll max_ll(ll a,ll b,ll c){
	return max(a,max(b,c));
}

ll min_ll(ll a,ll b,ll c){
	return min(a,min(b,c));
}

bool sortBySec(pair<ll,ll> a,pair<ll,ll> b){
    if(a.second==b.second)
		return a.first<b.first;
    return (a.second < b.second);
}

long long binexpmod(long long a, long long b){
    long long res =1;
    while(b>0){
        if(b&1){
            res = res * a % mod;
        }
        a = a * a % mod;
        b>>=1;
    }
    return(res);
}

ll lcm(ll a,ll b){
	ll x=__gcd(a,b);
	a/=x;
	b/=x;
	ll sum=a*b*x;
	return sum;
}
void solve(){

    const int mnx = 100005;
    vi dp(mnx,0);
    vi div[mnx];
    for(ll i=2 ; i*i<mnx ; i++){
        for(ll j=i; j<mnx ; j+=i){
            div[j].push_back(i);
        }
    }

    ll n,res=0;
	cin>>n;
    rep(i,n){
        ll t;
        cin>>t;
        ll mx=0;
        for(auto &p:div[t]){
            mx = max(mx, dp[p]);
        }
        for(auto &p:div[t]){
            dp[p] = mx+1;
        }
    }
    for(ll i=2;i<mnx-4;i++){
        res = max(res, dp[i]);
    }
	cout<<res<<endl;
	return;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t=1;
	// cin>>t;
	while(t--)
	{
		solve();
	}
	return 0;
}


// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define vi vector<ll>
#define vb vector<bool>
#define pii pair<ll,ll>
#define mod 1000000007
#define pll pair<ll,ll>
#define rep(i,n) for(ll i=0;i<n;i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define per1(i,n) for(ll i=n;i>0;i--)
#define prll(a,n) for(ll i=0;i<n;i++){ cout<<a[i]<<" "; }
#define ain(a) for(auto &i:a) cin>>i
#define mms(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
// cout << fixed << setprecision(0) << pi <<" "<<npi<<endl;

// ll max_ll(ll a,ll b,ll c){
// 	return max(a,max(b,c));
// }

// ll min_ll(ll a,ll b,ll c){
// 	return min(a,min(b,c));
// }

// bool sortBySec(pair<ll,ll> a,pair<ll,ll> b){
//     if(a.second==b.second)
// 		return a.first<b.first;
//     return (a.second < b.second);
// }

// long long binexpmod(long long a, long long b){
//     long long res =1;
//     while(b>0){
//         if(b&1){
//             res = res * a % mod;
//         }
//         a = a * a % mod;
//         b>>=1;
//     }
//     return(res);
// }

// ll lcm(ll a,ll b){
// 	ll x=__gcd(a,b);
// 	a/=x;
// 	b/=x;
// 	ll sum=a*b*x;
// 	return sum;
// }

bool prime[100005];
vi primes;
void sieve(ll n)
{
    memset(prime, true, sizeof(prime));
    for (ll p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (ll i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }
    for(ll i=2;i<=n;i++){
        if(prime[i]) primes.push_back(i);
    }
}
void solve(){
    ll n,res=0;
	cin>>n;
    vi a(n);
    ain(a);
    vi fac[n];
    vi piri(primes.size(),-1);
    // rep(p,primes.size()){
    //     ll par=0;
    //     bool f=true;
    //     rep(i,n){
    //         if(a[i]%primes[p] ==0){
    //             // v[p].push_back(par);
    //             if(f){
    //                 fac[i].push_back(i);
    //                 par = i;
    //                 f = false;
    //             }
    //             else{
    //                 fac[i].push_back(par);
    //                 par=i;
    //             }
    //         }
    //     }
    // }

    rep(i,n){
        if(a[i]==1) continue;
        ll s = sqrt(a[i]);
        s++;
        for(ll p = 0 ; p<primes.size() ; ++p){

            if(primes[p]>s){
                if(a[i]<4) continue;
                else break;
            }
            if(a[i]%primes[p] ==0){
                if(piri[p]==-1){
                    fac[i].push_back(i);
                    piri[p] = i;
                }
                else{
                    fac[i].push_back(piri[p]);
                    piri[p] = i;
                }
            }
        }
        // for (auto x:fac[i]) cout<<"fac["<<i<<"] = "<<x<<endl;
    }
    vi dp(n,1);
    for(ll i=0; i<n; ++i){
        for(auto j:fac[i]){
            if(j==i) continue;
            dp[i] = max(dp[i] , dp[j]+1);
        }
    }
    rep(i,n){
        res = max(res,dp[i]);
    }
	cout<<res<<endl;
	return;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t=1;
	// cin>>t;
    sieve(100000);
	while(t--)
	{
		solve();
	}
	return 0;
}


// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define vi vector<ll>
#define vb vector<bool>
#define pii pair<ll,ll>
#define mod 1000000007
#define pll pair<ll,ll>
#define rep(i,n) for(ll i=0;i<n;i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define per1(i,n) for(ll i=n;i>0;i--)
#define prll(a,n) for(ll i=0;i<n;i++){ cout<<a[i]<<" "; }
#define ain(a) for(auto &i:a) cin>>i
#define mms(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
// cout << fixed << setprecision(0) << pi <<" "<<npi<<endl;

// ll max_ll(ll a,ll b,ll c){
// 	return max(a,max(b,c));
// }

// ll min_ll(ll a,ll b,ll c){
// 	return min(a,min(b,c));
// }

// bool sortBySec(pair<ll,ll> a,pair<ll,ll> b){
//     if(a.second==b.second)
// 		return a.first<b.first;
//     return (a.second < b.second);
// }

// long long binexpmod(long long a, long long b){
//     long long res =1;
//     while(b>0){
//         if(b&1){
//             res = res * a % mod;
//         }
//         a = a * a % mod;
//         b>>=1;
//     }
//     return(res);
// }

// ll lcm(ll a,ll b){
// 	ll x=__gcd(a,b);
// 	a/=x;
// 	b/=x;
// 	ll sum=a*b*x;
// 	return sum;
// }

bool prime[100005];
vi primes;
void sieve(ll n)
{
    memset(prime, true, sizeof(prime));
    for (ll p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (ll i=p*p; i<=n; i += p)
                prime[i] = false;
        }
    }
    for(ll i=2;i<=n;i++){
        if(prime[i]) primes.push_back(i);
    }
}
void solve(){
    ll n,res=0;
	cin>>n;
    vi a(n);
    ain(a);
    vi fac[n];
    rep(p,primes.size()){
        ll par=0;
        bool f=true;
        rep(i,n){
            if(a[i]%primes[p] ==0){
                // v[p].push_back(par);
                if(f){
                    fac[i].push_back(i);
                    par = i;
                    f = false;
                }
                else{
                    fac[i].push_back(par);
                    par=i;
                }
            }
        }
    }
    vi dp(n,1);
    for(ll i=0; i<n; ++i){
        for(auto j:fac[i]){
            if(j==i) continue;
            dp[i] = max(dp[i] , dp[j]+1);
        }
    }
    rep(i,n){
        res = max(res,dp[i]);
    }
	cout<<res<<endl;
	return;
}
int32_t main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t=1;
	// cin>>t;
    sieve(sqrt(100000));
	while(t--)
	{
		solve();
	}
	return 0;
}


// Problem : D. Good Sequences
// Contest : Codeforces - Codeforces Round #162 (Div. 2)
// URL : https://codeforces.com/problemset/problem/265/D
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define vi vector<int>
#define vstr vector<string>
#define vdb vector<double>
#define vbool vector<bool>
#define vvi vector<vector<int> >
#define mii map<int,int>
#define pb push_back
#define pii pair<int,int>
#define vpair vector<pii >
#define mkp make_pair
#define scan(a,n) for(int i =0 ; i<n ; i++) cin>>a[i]
#define print(a,n) for(int i = 0 ; i < n ; i++) {cout<<a[i]<<' ';}cout<<'\n'
#define mem(a,v) memset(a,v,sizeof(a))
#define loop(i,a,b) for (int i = a; i < b; i++)
#define loope(i,a,b) for (int i = a; i <= b; i++)
#define bloop(i,a,b) for(int i = a;i>=b;i--)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION std::cout.unsetf ( std::ios::fixed );std::cout.precision(9)
#define PI 3.14159265
#define S second
#define F first
#define CHECK cout<<"CHEDEDWB"<<endl
#define CHECK1(a) cout<<a<<endl
#define CHECK2(a,b) cout<<a<<' '<<b<<endl;
#define br '\n'
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(),x.end()
using namespace std;
int mod = 1e9+7;
int inf = 1e18;
int m_inf = INT_MIN;
// int extendedgcd(int a,int b,int &x,int &y){
	// if(a == 0){
		// x = 0; y = 1;
		// return b;
	// }
	// int x1,y1;
	// int d = extendedgcd(b%a,a,x1,y1);
	// x = y1 - (b/a)*x1;y = x1;
	// return d;
// }
// int expmod(int a,int b) {
// 	if(b == 1) return a;
// 	if(b == 0) return 1;
// 	int m1 = expmod(a,b/2)%mod;
// 	if(b%2) return ((m1*m1)%mod*a%mod)%mod;
// 	return (m1*m1)%mod;
// }
// int power(int a,int b) {
// 	if(b == 1) return a;
// 	if(b == 0) return 1;
// 	int m1 = power(a,b/2);
// 	if(b%2) return m1*m1*a;
// 	return m1*m1;
// }
// int logn(int n, int r)
// {
//     return (n > r - 1) ? 1 + logn(n / r, r) : 0;
// }

// int nCr(int n,int r)
// {
	// r = min(r,n-r);
	// if(r<0)
		// return 0;
	// if(r == 0)
		// return 1;
	// int ans = 1;
	// for(int i = 1;i<=r;i++)
	// {
		// ans = ans*(n-i+1)/i;
	// }
	// return ans;
//
// }
// vi factmod(int n)
// {
	// vi ans(n+1);
	// ans[0] = 1;
	// loop(i,1,n+1)
	// {
		// ans[i] = (ans[i-1]*i)%mod;
	// }
	// return ans;
// }
// int nCrmod(int n,int r,vi &fact)
// {
	// int x1,y1;
	// extendedgcd(fact[r],mod,x1,y1);
	// int x2,y2;
	// extendedgcd(fact[n-r],mod,x2,y2);
	// int ans = (fact[n]*((x1+mod)%mod))%mod;
	// ans = (ans*((x2+mod)%mod))%mod;
	// return ans;
// }
// void dfs(vector<vector<int> > &adj,int root,bool vis[])
// {
// 	vis[root] = true;
// 	for(int i = 0;i<adj[root].size();i++){
// 		int v = adj[root][i];
// 		if(!vis[v])
// 			dfs(adj,v,vis);
// 	}
// }
vector<int> sieve(int n)
{
	vector<int> primes;
	bool isPrime[n+1] = {false};
	isPrime[1] = isPrime[0] = false;
	vi spf(n+1);
	spf[0] = 0;
	spf[1] = 1;
	for(int i = 2;i<=n;i++)
	{
		isPrime[i] = true;
		spf[i] = i;
	}
	for(int i = 2;i*i<=n;i++)
	{
		if(isPrime[i])
		{
			for(int j = i*i;j<=n;j+=i)
			{
				isPrime[j] = false;
				if(spf[j] == j)
				{
					spf[j] = i;
				}
			}
		}
	}
	// for(int i = 2;i<=n;i++)
	// {
		// if(isPrime[i])
		// {
			// primes.push_back(i);
		// }
	// }
	return spf;
}
int32_t main()
{
	FastIO; PRECISION;
	int t = 1;
	// cin>>t;
	vi spf = sieve(1e5+10);
	// print(spf,10);
	// CHECK;
	while(t--)
	{
		int n;
		cin>>n;
		vi arr(n);
		scan(arr,n);
		vi dp(1e5+1,0);
		int len = 0;
		loop(i,0,n)
		{
			int temp = arr[i];
			int ans = m_inf;
			while(temp>1)
			{
				int z = spf[temp];
				ans = max(dp[z]+1,ans);
				while(temp%z == 0)
				{
					temp/=z;
				}
			}
			temp = arr[i];
			while(temp>1)
			{
				int z = spf[temp];
				dp[z] = ans;
				while(temp%z == 0)
				{
					temp/=z;
				}
			}
			len = max(len,ans);
		}
		cout<<len<<'\n';
	}
}


// Problem : D. Good Sequences
// Contest : Codeforces - Codeforces Round #162 (Div. 2)
// URL : https://codeforces.com/problemset/problem/265/D
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define int long long
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define vi vector<int>
#define vstr vector<string>
#define vdb vector<double>
#define vbool vector<bool>
#define vvi vector<vector<int> >
#define mii map<int,int>
#define pb push_back
#define pii pair<int,int>
#define vpair vector<pii >
#define mkp make_pair
#define scan(a,n) for(int i =0 ; i<n ; i++) cin>>a[i]
#define print(a,n) for(int i = 0 ; i < n ; i++) {cout<<a[i]<<' ';}cout<<'\n'
#define mem(a,v) memset(a,v,sizeof(a))
#define loop(i,a,b) for (int i = a; i < b; i++)
#define loope(i,a,b) for (int i = a; i <= b; i++)
#define bloop(i,a,b) for(int i = a;i>=b;i--)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION std::cout.unsetf ( std::ios::fixed );std::cout.precision(9)
#define PI 3.14159265
#define S second
#define F first
#define CHECK cout<<"CHEDEDWB"<<endl
#define CHECK1(a) cout<<a<<endl
#define CHECK2(a,b) cout<<a<<' '<<b<<endl;
#define br '\n'
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(),x.end()
using namespace std;
int mod = 1e9+7;
int inf = 1e18;
int m_inf = INT_MIN;
// int extendedgcd(int a,int b,int &x,int &y){
	// if(a == 0){
		// x = 0; y = 1;
		// return b;
	// }
	// int x1,y1;
	// int d = extendedgcd(b%a,a,x1,y1);
	// x = y1 - (b/a)*x1;y = x1;
	// return d;
// }
// int expmod(int a,int b) {
// 	if(b == 1) return a;
// 	if(b == 0) return 1;
// 	int m1 = expmod(a,b/2)%mod;
// 	if(b%2) return ((m1*m1)%mod*a%mod)%mod;
// 	return (m1*m1)%mod;
// }
// int power(int a,int b) {
// 	if(b == 1) return a;
// 	if(b == 0) return 1;
// 	int m1 = power(a,b/2);
// 	if(b%2) return m1*m1*a;
// 	return m1*m1;
// }
// int logn(int n, int r)
// {
//     return (n > r - 1) ? 1 + logn(n / r, r) : 0;
// }

// int nCr(int n,int r)
// {
	// r = min(r,n-r);
	// if(r<0)
		// return 0;
	// if(r == 0)
		// return 1;
	// int ans = 1;
	// for(int i = 1;i<=r;i++)
	// {
		// ans = ans*(n-i+1)/i;
	// }
	// return ans;
//
// }
// vi factmod(int n)
// {
	// vi ans(n+1);
	// ans[0] = 1;
	// loop(i,1,n+1)
	// {
		// ans[i] = (ans[i-1]*i)%mod;
	// }
	// return ans;
// }
// int nCrmod(int n,int r,vi &fact)
// {
	// int x1,y1;
	// extendedgcd(fact[r],mod,x1,y1);
	// int x2,y2;
	// extendedgcd(fact[n-r],mod,x2,y2);
	// int ans = (fact[n]*((x1+mod)%mod))%mod;
	// ans = (ans*((x2+mod)%mod))%mod;
	// return ans;
// }
// void dfs(vector<vector<int> > &adj,int root,bool vis[])
// {
// 	vis[root] = true;
// 	for(int i = 0;i<adj[root].size();i++){
// 		int v = adj[root][i];
// 		if(!vis[v])
// 			dfs(adj,v,vis);
// 	}
// }
vector<int> sieve(int n)
{
	vector<int> primes;
	bool isPrime[n+1] = {false};
	isPrime[1] = isPrime[0] = false;
	vi spf(n+1);
	spf[0] = 0;
	spf[1] = 1;
	for(int i = 2;i<=n;i++)
	{
		isPrime[i] = true;
		spf[i] = i;
	}
	for(int i = 2;i*i<=n;i++)
	{
		if(isPrime[i])
		{
			for(int j = i*i;j<=n;j+=i)
			{
				isPrime[j] = false;
				if(spf[j] == j)
				{
					spf[j] = i;
				}
			}
		}
	}
	// for(int i = 2;i<=n;i++)
	// {
		// if(isPrime[i])
		// {
			// primes.push_back(i);
		// }
	// }
	return spf;
}
int32_t main()
{
	FastIO; PRECISION;
	int t = 1;
	// cin>>t;
	vi spf = sieve(1e5+10);
	// print(spf,10);
	// CHECK;
	while(t--)
	{
		int n;
		cin>>n;
		vi arr(n);
		scan(arr,n);
		vi dp(1e5+1,0);
		int len = 1;
		loop(i,0,n)
		{
			int temp = arr[i];
			int ans = m_inf;
			while(temp>1)
			{
				int z = spf[temp];
				ans = max(dp[z]+1,ans);
				while(temp%z == 0)
				{
					temp/=z;
				}
			}
			temp = arr[i];
			while(temp>1)
			{
				int z = spf[temp];
				dp[z] = ans;
				while(temp%z == 0)
				{
					temp/=z;
				}
			}
			len = max(len,ans);
		}
		cout<<len<<'\n';
	}
}


/// Code By t_bone_289 (Tushar Singh)
/// An Immature Programmer.... I'm still developing !!
#include<bits/stdc++.h>
using namespace std;
#define pll pair<ll,ll>
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define endl '\n'
#define M 1000000007
#define error(x) cout << #x << " = " << x << endl;
#define pi 3.14159265358979323846
typedef long long ll;
typedef double d;
typedef long double ld;
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin>>n;
    vector<ll> v(n);
    for(auto &x:v)
    {
        cin>>x;
    }
    ll N=100005;
    vector<ll> prime(N+1,-1);
    prime[0]=1;
    prime[1]=1;
    for(ll i=2;i*i<=N;i++)
    {
        if(prime[i]==-1)
        {
            for(ll j=2*i;j<=N;j+=i)
            {
                prime[j]=1;
            }
        }
    }
    vector<ll> dp(n,1);
    for(ll i=0;i<n;i++)
    {
        ll val=v[i];
        if(val%2==0)
        {
            if(prime[2]!=-1)
            {
                dp[i]=max(dp[i],dp[prime[2]]+1);
            }
            prime[2]=i;
            while(val%2==0)
            {
                val=val/2;
            }
        }
        for(ll j=3;j<=val;j+=2)
        {
            if(val%j==0)
            {
                if(prime[j]!=-1)
                {
                    dp[i]=max(dp[i],dp[prime[j]]+1);
                }
                prime[j]=i;
                while(val%j==0)
                {
                    val=val/j;
                }
            }
        }
        if(val>1)
        {
            if(prime[val]!=-1)
            {
                dp[i]=max(dp[i],dp[prime[val]]+1);
            }
            prime[val]=i;
        }
    }
    ll ans=1;
    for(ll i=0;i<n;i++)
    {
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}


/// Code By t_bone_289 (Tushar Singh)
/// An Immature Programmer.... I'm still developing !!
#include<bits/stdc++.h>
using namespace std;
#define pll pair<ll,ll>
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define endl '\n'
#define M 1000000007
#define error(x) cout << #x << " = " << x << endl;
#define pi 3.14159265358979323846
typedef long long ll;
typedef double d;
typedef long double ld;
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n;
    cin>>n;
    vector<ll> v(n);
    for(auto &x:v)
    {
        cin>>x;
    }
    ll N=100005;
    vector<ll> prime(N+1,-1);
    prime[0]=1;
    prime[1]=1;
    for(ll i=2;i*i<=N;i++)
    {
        if(prime[i]==-1)
        {
            for(ll j=2*i;j<=N;j+=i)
            {
                prime[j]=1;
            }
        }
    }
    vector<ll> dp(n,1);
    for(ll i=0;i<n;i++)
    {
        ll val=v[i];
        if(val%2==0)
        {
            if(prime[2]!=-1)
            {
                dp[i]=max(dp[i],dp[prime[2]]+1);
            }
            prime[2]=i;
            while(val%2==0)
            {
                val=val/2;
            }
        }
        for(ll j=3;j*j<=val;j+=2)
        {
            if(val%j==0)
            {
                if(prime[j]!=-1)
                {
                    dp[i]=max(dp[i],dp[prime[j]]+1);
                }
                prime[j]=i;
                while(val%j==0)
                {
                    val=val/j;
                }
            }
        }
        if(val>1)
        {
            if(prime[val]!=-1)
            {
                dp[i]=max(dp[i],dp[prime[val]]+1);
            }
            prime[val]=i;
        }
    }
    ll ans=1;
    for(ll i=0;i<n;i++)
    {
        ans=max(ans,dp[i]);
    }
    cout<<ans;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;

const int MAX = 1e5+1;

vector<int> decompose(int num) {
    vector<int> components;
    for (int i = 2; i*i <= num; i++) {
       if (num%i == 0)  {
           components.push_back(i);
           while(num%i == 0) {
               num /= i;
           }
       }
    }
    if(num > 1)
        components.push_back(num);

    return components;
}

int main(){
    int N;
    cin >> N;

    vector<int> nums(N);
    for (auto& num : nums)
        cin >> num;

    vector<int> max_lens(MAX);
    vector<int> results(N);

    for (int i = N-1; i >= 0; i--) {
        auto components = decompose(nums[i]);

        int maximum = INT_MIN;
        // get max subproblem
        for (auto& component : components) {
            maximum = max(maximum, max_lens[component]);
        }

        results[i] = maximum+1;

        // update
        for (auto& component : components) {
            max_lens[component] = results[i];
        }
    }

    int maximum = INT_MIN;
    for (auto result : results)
        maximum = max(result, maximum);

    cout << maximum << endl;

    return 0;
}


#include <iostream>
#include <vector>
using namespace std;

const int MAX = 1e5+1;

vector<int> decompose(int num) {
    vector<int> components;
    for (int i = 2; i*i <= num; i++) {
       if (num%i == 0)  {
           components.push_back(i);
           while(num%i == 0) {
               num /= i;
           }
       }
    }
    if(num > 1)
        components.push_back(num);

    return components;
}

int main(){
    int N;
    cin >> N;

    vector<int> nums(N);
    for (auto& num : nums)
        cin >> num;

    vector<int> max_lens(MAX);
    vector<int> results(N);

    for (int i = N-1; i >= 0; i--) {
        auto components = decompose(nums[i]);

        int maximum = INT_MIN;
        // get max subproblem
        for (auto& component : components) {
            maximum = max(maximum, max_lens[component]);
        }

        results[i] = 1;
        if (maximum != INT_MIN)
            results[i] += maximum;

        // update
        for (auto& component : components) {
            max_lens[component] = results[i];
        }
    }

    int maximum = INT_MIN;
    for (auto result : results)
        maximum = max(result, maximum);

    cout << maximum << endl;

    return 0;
}


      #include<bits/stdc++.h>
      #include <ext/pb_ds/assoc_container.hpp>
      #include <ext/pb_ds/tree_policy.hpp>
      using namespace __gnu_pbds;
      using namespace std;
      #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
      #define ll            long long
      #define pb            push_back
      #define ppb           pop_back
      #define si            set <ll>
      #define endl          '\n'
      #define fr            first
      #define sc            second
      #define mii           map<ll,ll>
      #define msi           map<string,ll>
      #define mis           map<ll,string>
      #define rep(i,a,b)    for(ll  i=a;i<b;i++)
      #define all(v)        v.begin(),v.end()
      //#define sort(v)       sort(all(v))
      #define pii           pair<ll ,ll >
      #define vi            vector<ll >
      #define vii           vector<pair<ll,ll>>
      #define vs            vector<string>
      #define sz(x)         (ll)x.size()
      #define rt            return
      #define M             1000000007
      #define bs            binary_search
      #define rev(a)        reverse(all(a));
      #define sp(n)         setprecision(n)
      #define spl           " "
      #define arr(a,n)      rep(i,0,n) cin>>a[i]
      #define mod           998244353
      #define time          cout << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
      #define INF           1ll<<31
      #define rt            return
      void __print(int x) {cerr << x;}
      void __print(long x) {cerr << x;}
      void __print(long long x) {cerr << x;}
      void __print(unsigned x) {cerr << x;}
      void __print(unsigned long x) {cerr << x;}
      void __print(unsigned long long x) {cerr << x;}
      void __print(float x) {cerr << x;}
      void __print(double x) {cerr << x;}
      void __print(long double x) {cerr << x;}
      void __print(char x) {cerr << '\'' << x << '\'';}
      void __print(const char *x) {cerr << '\"' << x << '\"';}
      void __print(const string &x) {cerr << '\"' << x << '\"';}
      void __print(bool x) {cerr << (x ? "true" : "false");}
      template<typename T, typename V>
      void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
      template<typename T>
      void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
      void _print() {cerr << "]\n";}
      template <typename T, typename... V>
      void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
      #ifndef ONLINE_JUDGE
      #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
      #else
      #define debug(x...)
      #endif
      ll bpow(ll a, ll b, ll mm = M)
      {
          ll res = 1;

          while(b)
          {
              if(b & 1)
                  res = (res * a) % mm;
              a = (a * a) % mm;

              b >>= 1;
          }

          return res;
      }
      ll modInverse(ll A,ll mm)
      {
          return bpow(A,mm-2,mm);
      }
      ll nCrModPFermat(ll n, ll r, ll p)
      {
         if (r==0)
            return 1;
          ll fac[n+1];
          fac[0] = 1;
          for (ll i=1 ; i<=n; i++)
              fac[i] = fac[i-1]*i%p;

          return (fac[n]*modInverse(fac[r],p)%p*modInverse(fac[n-r], p)%p)%p;
      }
      const ll N=1e5+5;
      std::vector<ll> a(N);
      std::vector<ll> dp(N,0);
      unordered_set<ll> prime_factors(ll n)
      {
        unordered_set<ll> st;
        if(n%2==0) st.insert(2);
        while(n%2==0) n/=2;
        for(ll i=3;i<=sqrt(n);i++)
          if(n%i==0)
            {st.insert(i);n=n/i;}
        if(n>2) st.insert(n);
        return st;
      }
      void solve()
      {
        ll n;
        cin>>n;
        arr(a,n);
        dp[0]=1;
        unordered_set<ll> k=prime_factors(a[0]);
        unordered_map<ll,vector<ll>> mp;
        for(auto e:k) mp[e].pb(1);
        rep(i,1,n)
        {
          unordered_set<ll> res=prime_factors(a[i]);
          ll mx=0;
          for(auto e:res)
            for(auto u:mp[e])
              mx=max(mx,dp[u-1]);
          if(mx==0) dp[i]=1;
          else dp[i]=mx+1;
          for(auto e:res) mp[e].pb(i+1);
        }
        ll mx=-1;
        rep(i,0,n) mx=max(mx,dp[i]);
        cout<<mx<<endl;

      }
      signed main()
      {
          ios_base::sync_with_stdio(false);
          cin.tie(0);
          cout.tie(0);
          ll t=1;
          //cin>>t;
          while(t--) solve();
      }


      #include<bits/stdc++.h>
      #include <ext/pb_ds/assoc_container.hpp>
      #include <ext/pb_ds/tree_policy.hpp>
      using namespace __gnu_pbds;
      using namespace std;
      #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
      #define ll            long long
      #define pb            push_back
      #define ppb           pop_back
      #define si            set <ll>
      #define endl          '\n'
      #define fr            first
      #define sc            second
      #define mii           map<ll,ll>
      #define msi           map<string,ll>
      #define mis           map<ll,string>
      #define rep(i,a,b)    for(ll  i=a;i<b;i++)
      #define all(v)        v.begin(),v.end()
      //#define sort(v)       sort(all(v))
      #define pii           pair<ll ,ll >
      #define vi            vector<ll >
      #define vii           vector<pair<ll,ll>>
      #define vs            vector<string>
      #define sz(x)         (ll)x.size()
      #define rt            return
      #define M             1000000007
      #define bs            binary_search
      #define rev(a)        reverse(all(a));
      #define sp(n)         setprecision(n)
      #define spl           " "
      #define arr(a,n)      rep(i,0,n) cin>>a[i]
      #define mod           998244353
      #define time          cout << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
      #define INF           1ll<<31
      #define rt            return
      void __print(int x) {cerr << x;}
      void __print(long x) {cerr << x;}
      void __print(long long x) {cerr << x;}
      void __print(unsigned x) {cerr << x;}
      void __print(unsigned long x) {cerr << x;}
      void __print(unsigned long long x) {cerr << x;}
      void __print(float x) {cerr << x;}
      void __print(double x) {cerr << x;}
      void __print(long double x) {cerr << x;}
      void __print(char x) {cerr << '\'' << x << '\'';}
      void __print(const char *x) {cerr << '\"' << x << '\"';}
      void __print(const string &x) {cerr << '\"' << x << '\"';}
      void __print(bool x) {cerr << (x ? "true" : "false");}
      template<typename T, typename V>
      void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
      template<typename T>
      void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
      void _print() {cerr << "]\n";}
      template <typename T, typename... V>
      void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
      #ifndef ONLINE_JUDGE
      #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
      #else
      #define debug(x...)
      #endif
      ll bpow(ll a, ll b, ll mm = M)
      {
          ll res = 1;

          while(b)
          {
              if(b & 1)
                  res = (res * a) % mm;
              a = (a * a) % mm;

              b >>= 1;
          }

          return res;
      }
      ll modInverse(ll A,ll mm)
      {
          return bpow(A,mm-2,mm);
      }
      ll nCrModPFermat(ll n, ll r, ll p)
      {
         if (r==0)
            return 1;
          ll fac[n+1];
          fac[0] = 1;
          for (ll i=1 ; i<=n; i++)
              fac[i] = fac[i-1]*i%p;

          return (fac[n]*modInverse(fac[r],p)%p*modInverse(fac[n-r], p)%p)%p;
      }
      const ll N=1e5+5;
      std::vector<ll> a(N);
      std::vector<ll> dp(N,0);
      vector<ll> divisors(int n)
      {
          vi res;
          for (int i=2; i<=sqrt(n); i++)
          {
              if (n%i == 0)
              {
                  if (n/i == i)
                      res.pb(i);

                  else {
                      res.pb(i);
                      res.pb(n/i);
                  }
              }
          }
          return res;
      }
      void solve()
      {
        ll n;
        cin>>n;
        arr(a,n);
        dp[a[0]]=1;
        rep(i,1,n)
        {
          std::vector<ll> d=divisors(a[i]);
          ll mx=0;
          for(ll x:d) mx=max(mx,dp[x]);
          dp[a[i]]=mx+1;
          for(ll x:d) dp[x]=mx+1;
        }
       ll ans=-1;
       for(auto e:dp) ans=max(ans,e);
        cout<<ans<<endl;

      }
      signed main()
      {
          ios_base::sync_with_stdio(false);
          cin.tie(0);
          cout.tie(0);
          ll t=1;
          //cin>>t;
          while(t--) solve();
      }


      #include<bits/stdc++.h>
      #include <ext/pb_ds/assoc_container.hpp>
      #include <ext/pb_ds/tree_policy.hpp>
      using namespace __gnu_pbds;
      using namespace std;
      #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
      #define ll            long long
      #define pb            push_back
      #define ppb           pop_back
      #define si            set <ll>
      #define endl          '\n'
      #define fr            first
      #define sc            second
      #define mii           map<ll,ll>
      #define msi           map<string,ll>
      #define mis           map<ll,string>
      #define rep(i,a,b)    for(ll  i=a;i<b;i++)
      #define all(v)        v.begin(),v.end()
      //#define sort(v)       sort(all(v))
      #define pii           pair<ll ,ll >
      #define vi            vector<ll >
      #define vii           vector<pair<ll,ll>>
      #define vs            vector<string>
      #define sz(x)         (ll)x.size()
      #define rt            return
      #define M             1000000007
      #define bs            binary_search
      #define rev(a)        reverse(all(a));
      #define sp(n)         setprecision(n)
      #define spl           " "
      #define arr(a,n)      rep(i,0,n) cin>>a[i]
      #define mod           998244353
      #define time          cout << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
      #define INF           1ll<<31
      #define rt            return
      void __print(int x) {cerr << x;}
      void __print(long x) {cerr << x;}
      void __print(long long x) {cerr << x;}
      void __print(unsigned x) {cerr << x;}
      void __print(unsigned long x) {cerr << x;}
      void __print(unsigned long long x) {cerr << x;}
      void __print(float x) {cerr << x;}
      void __print(double x) {cerr << x;}
      void __print(long double x) {cerr << x;}
      void __print(char x) {cerr << '\'' << x << '\'';}
      void __print(const char *x) {cerr << '\"' << x << '\"';}
      void __print(const string &x) {cerr << '\"' << x << '\"';}
      void __print(bool x) {cerr << (x ? "true" : "false");}
      template<typename T, typename V>
      void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
      template<typename T>
      void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
      void _print() {cerr << "]\n";}
      template <typename T, typename... V>
      void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
      #ifndef ONLINE_JUDGE
      #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
      #else
      #define debug(x...)
      #endif
      ll bpow(ll a, ll b, ll mm = M)
      {
          ll res = 1;

          while(b)
          {
              if(b & 1)
                  res = (res * a) % mm;
              a = (a * a) % mm;

              b >>= 1;
          }

          return res;
      }
      ll modInverse(ll A,ll mm)
      {
          return bpow(A,mm-2,mm);
      }
      ll nCrModPFermat(ll n, ll r, ll p)
      {
         if (r==0)
            return 1;
          ll fac[n+1];
          fac[0] = 1;
          for (ll i=1 ; i<=n; i++)
              fac[i] = fac[i-1]*i%p;

          return (fac[n]*modInverse(fac[r],p)%p*modInverse(fac[n-r], p)%p)%p;
      }
      const ll N=1e5+5;
      std::vector<ll> a(N);
      std::vector<ll> dp(N,0);
      unordered_set<ll> prime_factors(ll n)
      {
        unordered_set<ll> st;
        if(n%2==0) st.insert(2);
        while(n%2==0) n/=2;
        for(ll i=3;i<=sqrt(n);i++)
          if(n%i==0)
            {st.insert(i);n=n/i;}
        if(n>2) st.insert(n);
        return st;
      }
      void solve()
      {
        ll n;
        cin>>n;
        arr(a,n);
        dp[0]=1;
        unordered_set<ll> k=prime_factors(a[0]);
        unordered_map<ll,vector<ll>> mp;
        for(auto e:k) mp[e].pb(0);
        rep(i,1,n)
        {
          unordered_set<ll> res=prime_factors(a[i]);
          ll mx=0;
          for(auto e:res)
            for(auto u:mp[e])
              mx=max(mx,dp[u]);
          dp[i]=mx+1;
          for(auto e:res) mp[e].pb(i);
        }
        ll mx=-1;
        rep(i,0,n) mx=max(mx,dp[i]);
        cout<<mx<<endl;

      }
      signed main()
      {
          ios_base::sync_with_stdio(false);
          cin.tie(0);
          cout.tie(0);
          ll t=1;
          //cin>>t;
          while(t--) solve();
      }


      #include<bits/stdc++.h>
      #include <ext/pb_ds/assoc_container.hpp>
      #include <ext/pb_ds/tree_policy.hpp>
      using namespace __gnu_pbds;
      using namespace std;
      #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
      #define ll            long long
      #define pb            push_back
      #define ppb           pop_back
      #define si            set <ll>
      #define endl          '\n'
      #define fr            first
      #define sc            second
      #define mii           map<ll,ll>
      #define msi           map<string,ll>
      #define mis           map<ll,string>
      #define rep(i,a,b)    for(ll  i=a;i<b;i++)
      #define all(v)        v.begin(),v.end()
      //#define sort(v)       sort(all(v))
      #define pii           pair<ll ,ll >
      #define vi            vector<ll >
      #define vii           vector<pair<ll,ll>>
      #define vs            vector<string>
      #define sz(x)         (ll)x.size()
      #define rt            return
      #define M             1000000007
      #define bs            binary_search
      #define rev(a)        reverse(all(a));
      #define sp(n)         setprecision(n)
      #define spl           " "
      #define arr(a,n)      rep(i,0,n) cin>>a[i]
      #define mod           998244353
      #define time          cout << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
      #define INF           1ll<<31
      #define rt            return
      void __print(int x) {cerr << x;}
      void __print(long x) {cerr << x;}
      void __print(long long x) {cerr << x;}
      void __print(unsigned x) {cerr << x;}
      void __print(unsigned long x) {cerr << x;}
      void __print(unsigned long long x) {cerr << x;}
      void __print(float x) {cerr << x;}
      void __print(double x) {cerr << x;}
      void __print(long double x) {cerr << x;}
      void __print(char x) {cerr << '\'' << x << '\'';}
      void __print(const char *x) {cerr << '\"' << x << '\"';}
      void __print(const string &x) {cerr << '\"' << x << '\"';}
      void __print(bool x) {cerr << (x ? "true" : "false");}
      template<typename T, typename V>
      void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
      template<typename T>
      void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
      void _print() {cerr << "]\n";}
      template <typename T, typename... V>
      void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
      #ifndef ONLINE_JUDGE
      #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
      #else
      #define debug(x...)
      #endif
      ll bpow(ll a, ll b, ll mm = M)
      {
          ll res = 1;

          while(b)
          {
              if(b & 1)
                  res = (res * a) % mm;
              a = (a * a) % mm;

              b >>= 1;
          }

          return res;
      }
      ll modInverse(ll A,ll mm)
      {
          return bpow(A,mm-2,mm);
      }
      ll nCrModPFermat(ll n, ll r, ll p)
      {
         if (r==0)
            return 1;
          ll fac[n+1];
          fac[0] = 1;
          for (ll i=1 ; i<=n; i++)
              fac[i] = fac[i-1]*i%p;

          return (fac[n]*modInverse(fac[r],p)%p*modInverse(fac[n-r], p)%p)%p;
      }
      const ll N=1e5+5;
      std::vector<ll> a(N);
      std::vector<ll> dp(N,0);
      unordered_set<ll> prime_factors(ll n)
      {
        unordered_set<ll> st;
        if(n%2==0) st.insert(2);
        while(n%2==0) n/=2;
        for(ll i=3;i<=sqrt(n);i++)
          if(n%i==0)
            {st.insert(i);n=n/i;}
        if(n>2) st.insert(n);
        return st;
      }
      void solve()
      {
        ll n;
        cin>>n;
        arr(a,n);
        dp[0]=1;
        unordered_set<ll> k=prime_factors(a[0]);
        unordered_map<ll,ll> mp;
        for(auto e:k) mp[e]=1;
        rep(i,1,n)
        {
          unordered_set<ll> res=prime_factors(a[i]);
          ll mx=0;
          for(auto e:res) mx=max(mx,mp[e]);
          if(mx==0) dp[i]=1;
          else dp[i]=dp[mx-1]+1;
          for(auto e:res) mp[e]=i+1;

        }
        ll mx=-1;
        rep(i,0,n) mx=max(mx,dp[i]);
        cout<<mx<<endl;

      }
      signed main()
      {
          ios_base::sync_with_stdio(false);
          cin.tie(0);
          cout.tie(0);
          ll t=1;
          //cin>>t;
          while(t--) solve();
      }


      #include<bits/stdc++.h>
      #include <ext/pb_ds/assoc_container.hpp>
      #include <ext/pb_ds/tree_policy.hpp>
      using namespace __gnu_pbds;
      using namespace std;
      #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
      #define ll            long long
      #define pb            push_back
      #define ppb           pop_back
      #define si            set <ll>
      #define endl          '\n'
      #define fr            first
      #define sc            second
      #define mii           map<ll,ll>
      #define msi           map<string,ll>
      #define mis           map<ll,string>
      #define rep(i,a,b)    for(ll  i=a;i<b;i++)
      #define all(v)        v.begin(),v.end()
      //#define sort(v)       sort(all(v))
      #define pii           pair<ll ,ll >
      #define vi            vector<ll >
      #define vii           vector<pair<ll,ll>>
      #define vs            vector<string>
      #define sz(x)         (ll)x.size()
      #define rt            return
      #define M             1000000007
      #define bs            binary_search
      #define rev(a)        reverse(all(a));
      #define sp(n)         setprecision(n)
      #define spl           " "
      #define arr(a,n)      rep(i,0,n) cin>>a[i]
      #define mod           998244353
      #define time          cout << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
      #define INF           1ll<<31
      #define rt            return
      void __print(int x) {cerr << x;}
      void __print(long x) {cerr << x;}
      void __print(long long x) {cerr << x;}
      void __print(unsigned x) {cerr << x;}
      void __print(unsigned long x) {cerr << x;}
      void __print(unsigned long long x) {cerr << x;}
      void __print(float x) {cerr << x;}
      void __print(double x) {cerr << x;}
      void __print(long double x) {cerr << x;}
      void __print(char x) {cerr << '\'' << x << '\'';}
      void __print(const char *x) {cerr << '\"' << x << '\"';}
      void __print(const string &x) {cerr << '\"' << x << '\"';}
      void __print(bool x) {cerr << (x ? "true" : "false");}
      template<typename T, typename V>
      void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
      template<typename T>
      void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
      void _print() {cerr << "]\n";}
      template <typename T, typename... V>
      void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
      #ifndef ONLINE_JUDGE
      #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
      #else
      #define debug(x...)
      #endif
      ll bpow(ll a, ll b, ll mm = M)
      {
          ll res = 1;

          while(b)
          {
              if(b & 1)
                  res = (res * a) % mm;
              a = (a * a) % mm;

              b >>= 1;
          }

          return res;
      }
      ll modInverse(ll A,ll mm)
      {
          return bpow(A,mm-2,mm);
      }
      ll nCrModPFermat(ll n, ll r, ll p)
      {
         if (r==0)
            return 1;
          ll fac[n+1];
          fac[0] = 1;
          for (ll i=1 ; i<=n; i++)
              fac[i] = fac[i-1]*i%p;

          return (fac[n]*modInverse(fac[r],p)%p*modInverse(fac[n-r], p)%p)%p;
      }
      const ll N=1e5+50;
      std::vector<ll> a(N);
      std::vector<ll> dp(N,0);
      vector<ll> divisors(ll n)
      {
          vi res;
          for (ll i=2; i<=sqrt(n); i++)
          {
              if (n%i == 0)
              {
                  if (n/i == i)
                      res.pb(i);

                  else {
                      res.pb(i);
                      res.pb(n/i);
                  }
              }
          }
          return res;
      }
      void solve()
      {
        ll n;
        cin>>n;
        arr(a,n);
        dp[a[0]]=1;
        vi p=divisors(a[0]);
        for(ll x:p) dp[x]=1;
        rep(i,1,n)
        {
          std::vector<ll> d=divisors(a[i]);
          ll mx=0;
          for(ll x:d) mx=max(mx,dp[x]);
          dp[a[i]]=mx+1;
          for(ll x:d) dp[x]=max(dp[x],mx+1);
        }
       ll ans=-1;
       for(auto e:dp) ans=max(ans,e);
        cout<<ans<<endl;

      }
      signed main()
      {
          ios_base::sync_with_stdio(false);
          cin.tie(0);
          cout.tie(0);
          ll t=1;
          //cin>>t;
          while(t--) solve();
      }


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

#define ll long long

#define file_io 0

int i,t,j;
int n,nt,a[100005],dp[100005],mx,ans;
bool np[100005];
vector<int> p;

int main() {
	if (file_io) {
		freopen(".in","r",stdin);
		freopen(".out","w",stdout);
	}

	for (i=2; i*i<=100000; ++i) {
		if (np[i]==0) {
			p.push_back(i);
			for (t=i+i; t<=100000; t+=i) {
				np[t]=1;
			}
		}
	}

	scanf("%d",&n);

	for (i=1; i<=n; ++i) {
		scanf("%d",&a[i]);
	}

	//printf("%d\n",p.size());

	//dp[i], the maximum length of the sequence
	//containing factor [i]

	sort(a+1,a+n+1);
	for (i=1; i<=n; ++i) {
		dp[1]=-1;

		//found which factor can form the maxmium length of sequence
		//if we have to use a[i]
		for (t=0,mx=0; t<(int)p.size() && p[t]<=a[i]; ++t){
			if (a[i]%p[t]==0) {
				mx=max(mx,dp[a[i]/p[t]]);
				mx=max(mx,dp[p[t]]);
			}
		}

		//update all other factors to the maxmimum length
		++mx;
		for (t=0; t<(int)p.size() && p[t]<=a[i]; ++t){
			if (a[i]%p[t]==0) {
				dp[p[t]]=mx;
				dp[a[i]/p[t]]=mx;
			}
		}
	}

	for (i=2,mx=1; i<=100000; ++i) mx=max(mx,dp[i]);
	printf("%d\n",mx);

	return 0;
}
/*

*/


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

#define ll long long

#define file_io 0

int i,t,j;
int n,nt,a[100005],dp[100005],mx,ans;
bool np[100005];
vector<int> p;

int main() {
	if (file_io) {
		freopen(".in","r",stdin);
		freopen(".out","w",stdout);
	}

	for (i=2; i*i<=100000; ++i) {
		if (np[i]==0) {
			p.push_back(i);
			for (t=i+i; t<=100000; t+=i) {
				np[t]=1;
			}
		}
	}

	scanf("%d",&n);

	for (i=1; i<=n; ++i) {
		scanf("%d",&a[i]);
	}

	//printf("%d\n",p.size());

	//dp[i], the maximum length of the sequence
	//containing factor [i]
	for (i=1; i<=n; ++i) {
		dp[1]=-1;

		//found which factor can form the maxmium length of sequence
		//if we have to use a[i]
		for (t=0,mx=0; t<(int)p.size() && p[t]<=a[i]; ++t){
			if (a[i]%p[t]==0) {
				mx=max(mx,dp[a[i]/p[t]]);
				mx=max(mx,dp[p[t]]);
			}
		}

		//update all other factors to the maxmimum length
		++mx;
		for (t=0; t<(int)p.size() && p[t]<=a[i]; ++t){
			if (a[i]%p[t]==0) {
				dp[p[t]]=mx;
				dp[a[i]/p[t]]=mx;
			}
		}
	}

	for (i=2,mx=1; i<=100000; ++i) mx=max(mx,dp[i]);
	printf("%d\n",mx);

	return 0;
}
/*

*/


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

#define ll long long

#define file_io 0

int i,t,j;
int n,nt,a[100005],dp[100005],mx,ans;
bool np[100005];
vector<int> p;

int main() {
	if (file_io) {
		freopen(".in","r",stdin);
		freopen(".out","w",stdout);
	}

	for (i=2; i*i<=100000; ++i) {
		if (np[i]==0) {
			p.push_back(i);
			for (t=i+i; t<=100000; t+=i) {
				np[t]=1;
			}
		}
	}

	scanf("%d",&n);

	for (i=1; i<=n; ++i) {
		scanf("%d",&a[i]);
	}

	//printf("%d\n",p.size());

	//dp[i], the maximum length of the sequence
	//containing factor [i]
	for (i=1; i<=n; ++i) {
		//found which factor can form the maxmium length of sequence
		//if we have to use a[i]
		for (t=0,mx=0; t<(int)p.size() && p[t]<=a[i]; ++t){
			if (a[i]%p[t]==0) {
				mx=max(mx,dp[a[i]/p[t]]);
				mx=max(mx,dp[p[t]]);
			}
		}

		//update all other factors to the maxmimum length
		++mx;
		for (t=0; t<(int)p.size() && p[t]<=a[i]; ++t){
			if (a[i]%p[t]==0) {
				dp[p[t]]=mx;
				dp[a[i]/p[t]]=mx;
			}
		}
	}

	for (i=2,mx=1; i<=100000; ++i) mx=max(mx,dp[i]);
	printf("%d\n",mx);

	return 0;
}
/*

*/


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

#define ll long long

#define file_io 0

int i,t,j;
int n,nt,a[100005],dp[100005],mx,ans;
bool np[100005],prime;
vector<int> p;

int main() {
	if (file_io) {
		freopen(".in","r",stdin);
		freopen(".out","w",stdout);
	}

	for (i=2; i*i<=100000; ++i) {
		if (np[i]==0) {
			p.push_back(i);
			for (t=i+i; t<=100000; t+=i) {
				np[t]=1;
			}
		}
	}

	scanf("%d",&n);

	for (i=1; i<=n; ++i) {
		scanf("%d",&a[i]);
	}

	//printf("%d\n",p.size());

	//dp[i], the maximum length of the sequence
	//containing factor [i]

	sort(a+1,a+n+1);
	for (i=1; i<=n; ++i) {
		dp[1]=-1;

		//found which factor can form the maxmium length of sequence
		//if we have to use a[i]

		//optimization - only need sqrt
		for (t=0,mx=0,prime=1; t<(int)p.size() && p[t]<=a[i]; ++t) {
			if (a[i]%p[t]==0) {
				prime=0;
				mx=max(mx,dp[a[i]/p[t]]);
				mx=max(mx,dp[p[t]]);
			}
		}

		//update all other factors to the maxmimum length
		++mx;

		//itself is a factor
		if (prime) dp[a[i]]=mx;
		else {
			for (t=0; t<(int)p.size() && p[t]<=a[i]; ++t) {
				if (a[i]%p[t]==0) {
					dp[p[t]]=mx;
					dp[a[i]/p[t]]=mx;
				}
			}
		}
	}

	for (i=2,mx=1; i<=100000; ++i) mx=max(mx,dp[i]);
	printf("%d\n",mx);

	return 0;
}
/*
2
5101 10202
-------------
2
*/


#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

#define ll long long

#define file_io 0

int i,t,j;
int n,nt,a[100005],dp[100005],mx,ans;
bool np[100005];
vector<int> p;

int main() {
	if (file_io) {
		freopen(".in","r",stdin);
		freopen(".out","w",stdout);
	}

	p.push_back(1);
	for (i=2; i*i<=100000; ++i) {
		if (np[i]==0) {
			p.push_back(i);
			for (t=i+i; t<=100000; t+=i) {
				np[t]=1;
			}
		}
	}

	scanf("%d",&n);

	for (i=1; i<=n; ++i) {
		scanf("%d",&a[i]);
	}

	//printf("%d\n",p.size());

	//dp[i], the maximum length of the sequence
	//containing factor [i]
	for (i=1; i<=n; ++i) {
		//found which factor can form the maxmium length of sequence
		//if we have to use a[i]
		for (t=0,mx=0; t<(int)p.size() && p[t]*p[t]<=a[i]; ++t){
			if (a[i]%p[t]==0) {
				mx=max(mx,dp[a[i]/p[t]]);
				if (p[t]!=1) mx=max(mx,dp[p[t]]);
			}
		}

		//update all other factors to the maxmimum length
		++mx;
		for (t=0; t<(int)p.size() && p[t]*p[t]<=a[i]; ++t){
			if (a[i]%p[t]==0) {
				dp[p[t]]=mx;
				dp[a[i]/p[t]]=mx;
			}
		}
	}

	for (i=2,mx=1; i<=100000; ++i) mx=max(mx,dp[i]);
	printf("%d\n",mx);

	return 0;
}
/*

*/


#include <bits/stdc++.h>
using namespace std;
#define float long double
#define int long long
#define vl vector<int>
#define mp make_pair
#define pb push_back
#define sd second
#define ft first
#define all(x) x.begin(),x.end()
#define endl '\n'
#define cnl cout<<endl;
#define ms(x,v) ((x), v, sizeof((x)))
#define CASET int ___T; scanf("%lld", &___T); for(int cs=1; cs <= ___T; cs++)
const int N = 200 * 1000 + 9;
const int  inf = 1e18;
const int mod = 1000000007;
#define pi  3.141592653589793238
void solve(){
      int n; cin >> n ;
      vector<int> v(n);
      for(int i = 0; i < n;i++) cin >> v[i];
      vector<int> dp(1e5 + 10,1);
      int j = 0;
      for(int i = 0;i < n;i++){
        vector<int> divisors ;
        int mx = 0;
          for(int j = 2; j <= sqrt(v[i]); j++){
              if(v[i] % j == 0){
                 int  x = v[i] / j;
                 mx = max(mx, dp[j]);
                 mx = max(mx,dp[x]);
                 if(j != x){
                    divisors.pb(j);
                    divisors.pb(x);
                 }else divisors.pb(j);
              }
              dp[v[i]] = mx + 1;
              for(int j =0 ; j < (int)divisors.size(); j++){
                 dp[divisors[j]] = mx + 1;
              }
              divisors.empty();

          }
        }
       int  mx =0;
        for(int i = 0; i <= 1e5 + 4; i++){
            mx = max(mx,dp[i]);
        }
        cout << mx << endl;

    return ;
}
    signed main(){
     #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    //CASET
    solve();

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define float long double
#define int long long
#define vl vector<int>
#define mp make_pair
#define pb push_back
#define sd second
#define ft first
#define all(x) x.begin(),x.end()
#define endl '\n'
#define cnl cout<<endl;
#define ms(x,v) ((x), v, sizeof((x)))
#define CASET int ___T; scanf("%lld", &___T); for(int cs=1; cs <= ___T; cs++)
const int N = 200 * 1000 + 9;
const int  inf = 1e18;
const int mod = 1000000007;
#define pi  3.141592653589793238
void solve(){
      int n; cin >> n ;
      vector<int> v(n);
      for(int i = 0; i < n;i++) cin >> v[i];
      vector<int> dp(1e5 + 10,0);
      int j = 0;
      for(int i = 0;i < n;i++){
        vector<int> dv ;
        int mx = 0;
          for(int j = 2; j <= sqrt(v[i]); j++){
               int tm = v[i];
              if(tm % j == 0){
                 while(tm % j == 0){
                     tm = tm / j;
                 }
                 mx = max(mx, dp[j]);
                  dv.pb(j);
              }
              if(tm >= 2) {
                mx = max(dp[tm],mx);
                dv.pb(tm);
            }
          }
          dp[v[i]] = mx + 1;
          for(int j =0 ; j < (int)dv.size(); j++){
                 dp[dv[j]] = mx + 1;
              }
              dv.empty();
        }
        int mx = 0;
        for(int i =0; i <= v[n-1]; i++){
             mx = max(mx, dp[i]);
        }
        cout << mx << endl;

    return ;
}
    signed main(){
     #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    //CASET
    solve();

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define float long double
#define int long long
#define vl vector<int>
#define mp make_pair
#define pb push_back
#define sd second
#define ft first
#define all(x) x.begin(),x.end()
#define endl '\n'
#define cnl cout<<endl;
#define ms(x,v) ((x), v, sizeof((x)))
#define CASET int ___T; scanf("%lld", &___T); for(int cs=1; cs <= ___T; cs++)
const int N = 200 * 1000 + 9;
const int  inf = 1e18;
const int mod = 1000000007;
#define pi  3.141592653589793238
void solve(){
      int n; cin >> n ;
      vector<int> v(n);
      for(int i = 0; i < n;i++) cin >> v[i];
      vector<int> dp(1e5 + 10,0);
      int j = 0;
      for(int i = 0;i < n;i++){
        vector<int> divisors ;
        int mx = 0;
          for(int j = 2; j <= sqrt(v[i]); j++){
              if(v[i] % j == 0){
                 int  x = v[i] / j;
                 mx = max(mx, dp[j]);
                 mx = max(mx,dp[x]);
                 if(j != x){
                    divisors.pb(j);
                    divisors.pb(x);
                 }else divisors.pb(j);
              }
            }
             dp[v[i]] = mx + 1;
              for(int j =0 ; j < (int)divisors.size(); j++){
                 dp[divisors[j]] = mx + 1;
              }
              divisors.empty();
        }
       int  mx =0;
        for(int i = 0; i <= 1e5 + 4; i++){
            mx = max(mx,dp[i]);
        }
        cout << mx << endl;

    return ;
}
    signed main(){
     #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    //CASET
    solve();

    return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define int long long
#define M 998244353
//#define M 1000000007
using namespace std;
const int inf=4e18;
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	int n;cin>>n;
	int a[n+1],i,j,last[n+1]={0},dp[n+1];dp[0]=0;
	map<int,int> mp;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		mp[a[i]]=i;
	}
	for(i=2;i<=100000;i++)
	{
		int prev=0;
		for(j=i;j<=100000;j+=i)
		{
			if(mp[j])
			{
				last[mp[j]]=max(last[mp[j]],prev);
				prev=mp[j];
			}
		}
	}
	for(i=1;i<=n;i++)
		dp[i]=1+dp[last[i]];
	cout<<*max_element(dp,dp+n+1)<<endl;
}


#include<bits/stdc++.h>
#define pb push_back
#define int long long
#define M 998244353
//#define M 1000000007
using namespace std;
const int inf=4e18;
int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);cout.tie(NULL);
	int n;cin>>n;
	int a[n+1],i,j,dp[n+1];dp[0]=0;
	vector<int> last[n+1];
	map<int,int> mp;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		mp[a[i]]=i;
	}
	for(i=2;i<=100000;i++)
	{
		int prev=0;
		for(j=i;j<=100000;j+=i)
		{
			if(mp[j])
			{
				last[mp[j]].pb(prev);
				prev=mp[j];
			}
		}
	}
	for(i=1;i<=n;i++)
	{
		dp[i]=1;
		for(j=0;j<last[i].size();j++)
			dp[i]=max(dp[i],1+dp[last[i][j]]);
	}
	cout<<*max_element(dp,dp+n+1)<<endl;
}


#include <bits/stdc++.h>
using namespace std;
 #define MOD 998244353
 #define A '@'
 #define B '*'
 #define C '#'
 #define max(a,b) ((a>b)?a:b)
 #define endl "\n"
 struct frac {long x; long y;};

 long long pfmax[100001],p[100001];
 vector<long long > lis;
long long t,k,n,m, i,j,mid,b,c,res=0;
long long a[100001],dp[100001];
int main()
{
   ios::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   cin>>n;
   for (i=2;i<=100000;i++)
   {
   	p[i]=1;
   }
   k=0;
   for (i=2;i<=100000;i++)
   {
   	if (p[i]) {k++; p[i]=k; for (j=i*i;j<=100000;j+=i) p[j]=0;}
   }
   for (i=1;i<=n;i++)
   {
   	cin>>a[i];
   	lis.clear();
   	j=2;
   	while (j*j<=a[i]) {if (!(a[i]%j))
   	{
   		lis.push_back(j);
   		while (!(a[i]%j)) a[i]/=j;

   	}
   		j++;
   	}
   	if (a[i]>1) lis.push_back(a[i]);
   	for (j=0;j<lis.size();j++) {if (pfmax[p[lis[j]]]>dp[i]) dp[i]=pfmax[p[lis[j]]]; }
   	dp[i]++;
   	for (j=0;j<lis.size();j++)  pfmax[p[lis[j]]]=dp[i];
   	if (res<dp[i]) res=dp[i];

   }
   cout<<res;
}
/*
I don't want a lot for Christmas
There is just one thing I need
I don't care about bài D
That I toang yesterday
I just want you for my own
More than you could ever know
Make my wish come true oh
All I want for Christmas is tím
*/


#include <bits/stdc++.h>
using namespace std;
 #define MOD 998244353
 #define A '@'
 #define B '*'
 #define C '#'
 #define max(a,b) ((a>b)?a:b)
 #define endl "\n"
 struct frac {long x; long y;};

 long long pfmax[1001],p[100001];
 vector<long long > lis;
long long t,k,n,m, i,j,mid,b,c,res=0;
long long a[100001],dp[100001];
int main()
{
   ios::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   cin>>n;
   for (i=2;i<=100000;i++)
   {
   	p[i]=1;
   }
   k=0;
   for (i=2;i<=100000;i++)
   {
   	if (p[i]) {k++; p[i]=k; for (j=i*i;j<=100000;j+=i) p[j]=0;}
   }
   for (i=1;i<=n;i++)
   {
   	cin>>a[i];
   	lis.clear();
   	j=2;
   	while (j*j<=a[i]) {if (!(a[i]%j))
   	{
   		lis.push_back(j);
   		while (!(a[i]%j)) a[i]/=j;

   	}
   		j++;
   	}
   	if (a[i]>1) lis.push_back(a[i]);
   	for (j=0;j<lis.size();j++) {if (pfmax[p[lis[j]]]>dp[i]) dp[i]=pfmax[p[lis[j]]]; }
   	dp[i]++;
   	for (j=0;j<lis.size();j++)  pfmax[p[lis[j]]]=dp[i];
   	if (res<dp[i]) res=dp[i];

   }
   cout<<res;
}
/*
I don't want a lot for Christmas
There is just one thing I need
I don't care about bài D
That I toang yesterday
I just want you for my own
More than you could ever know
Make my wish come true oh
All I want for Christmas is tím
*/


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std ;
typedef long long ll ;
const int MAXN = 2e5 + 10 ;
int T , n ;
ll s ;
pair <ll , ll> a[MAXN] ;
bool check (ll mid) {
	ll sum = 0 ; int cnt1 = 0 , cnt2 = 0 ;
	vector <pair <ll , ll> > tmp ;
	for (int i = 1 ; i <= n ; i++) {
		if (a[i].second < mid) sum += a[i].first , cnt1++ ;
		else if (a[i].first > mid) sum += a[i].first , cnt2++ ;
		else tmp.push_back (a[i]) ;
	}
	//printf ("%d %d %d\n" , mid , cnt1 , cnt2) ;
	if (cnt1 > n / 2 || cnt2 > n / 2) return 0 ;
	int k = tmp.size () ;
	for (int i = 0 ; i < k ; i++)
		if (i < (n / 2) - cnt1) sum += tmp[i].first ;
		else sum += mid ;
	return sum <= s ;
}
int main () {
	scanf ("%d" , &T) ;
	while (T--) {
		scanf ("%d %lld" , &n , &s) ;
		for (int i = 1 ; i <= n ; i++)
			scanf ("%lld %lld" , &a[i].first , &a[i].second) ;
		sort (a + 1 , a + n + 1) ;
		ll l = a[(n + 1) / 2].first , r = 1LL * s / ((n + 1) / 2);
		while (l + 1 < r) {
			int mid = l + r >> 1 ;
			if (check (mid)) l = mid ;
			else r = mid ;
		}
		printf ("%lld\n" , check (r) ? r : l) ;
	}
	return 0 ;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std ;
typedef long long ll ;
const int MAXN = 2e5 + 10 ;
int T , n , tmp[MAXN] ;
ll s ;
pair <ll , ll> a[MAXN] ;
bool check (ll mid) {
	ll sum = 0 ; int cnt1 = 0 , cnt2 = 0 , tot = 0 ;
	for (int i = 1 ; i <= n ; i++) {
		if (a[i].second < mid) sum += a[i].first , cnt1++ ;
		else if (a[i].first > mid) sum += a[i].first , cnt2++ ;
		else tmp[++tot] = i ;
	}
	//printf ("%d %d %d\n" , mid , cnt1 , cnt2) ;
	if (cnt1 > n / 2 || cnt2 > n / 2) return 0 ;
	for (int i = 1 ; i <= tot ; i++)
		if (i <= (n / 2) - cnt1) sum += a[tmp[i]].first ;
		else sum += mid ;
	return sum <= s ;
}
int main () {
	scanf ("%d" , &T) ;
	while (T--) {
		scanf ("%d %lld" , &n , &s) ;
		for (int i = 1 ; i <= n ; i++)
			scanf ("%lld %lld" , &a[i].first , &a[i].second) ;
		sort (a + 1 , a + n + 1) ;
		ll l = a[(n + 1) / 2].first , r = s ;
		while (l + 1 < r) {
			int mid = l + r >> 1 ;
			if (check (mid)) l = mid ;
			else r = mid ;
		}
		printf ("%lld\n" , l) ;
	}
	return 0 ;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std ;
typedef long long ll ;
const int MAXN = 2e5 + 10 ;
int T , n , tmp[MAXN] ;
ll s ;
pair <ll , ll> a[MAXN] ;
bool check (ll mid) {
	ll sum = 0 ; int cnt1 = 0 , cnt2 = 0 , tot = 0 ;
	for (int i = 1 ; i <= n ; i++) {
		if (a[i].second < mid) sum += a[i].first , cnt1++ ;
		else if (a[i].first > mid) sum += a[i].first , cnt2++ ;
		else tmp[++tot] = i ;
	}
	//printf ("%d %d %d\n" , mid , cnt1 , cnt2) ;
	if (cnt1 > n / 2 || cnt2 > n / 2) return 0 ;
	for (int i = 1 ; i <= tot ; i++)
		if (i < (n / 2) - cnt1) sum += a[tmp[i]].first ;
		else sum += mid ;
	return sum <= s ;
}
int main () {
	scanf ("%d" , &T) ;
	while (T--) {
		scanf ("%d %lld" , &n , &s) ;
		for (int i = 1 ; i <= n ; i++)
			scanf ("%lld %lld" , &a[i].first , &a[i].second) ;
		sort (a + 1 , a + n + 1) ;
		ll l = a[(n + 1) / 2].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 ("%lld\n" , ans) ;
	}
	return 0 ;
}


// Problem : D. Salary Changing
// Contest : Codeforces - Educational Codeforces Round 75 (Rated for Div. 2)
// URL : https://codeforces.ml/contest/1251/problem/D
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define x first
#define y second
const int N = 2e5+7;
pii qlist[N],quark[N];
int n;ll s;
bool check(ll mid)
{
	int cnt1 = 0,cnt2 = 0,cnt3 = 0;
	ll fres = 0;
	for(int i = 1;i <= n;++i)
	{
		int l = qlist[i].x,r = qlist[i].y;
		if(r < mid)	fres += l,++cnt1;
		else if(l > mid)	fres += l,++cnt2;
		else quark[++cnt3] = {l,r};
	}
	if(cnt1 > n / 2 || cnt2 > n / 2)	return 0;
	int d = n / 2 - cnt1;
	for(int i = 1;i <= d;++i)	fres += quark[i].x;
	fres += mid * (cnt3 - d);
	return fres <= s;
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
		scanf("%d%lld",&n,&s);

		for(int i = 1;i <= n;++i)	scanf("%d%d",&qlist[i].x,&qlist[i].y);
		sort(qlist + 1,qlist + 1 + n);
		ll l = qlist[(n / 2) + 1].x,r = s;
		while(l < r)
		{
			ll mid = l + r + 1 >> 1;
			if(check(mid))	l = mid;
			else r = mid - 1;
		}
		printf("%lld\n",l);
    }
    return 0;
}


// Problem : D. Salary Changing
// Contest : Codeforces - Educational Codeforces Round 75 (Rated for Div. 2)
// URL : https://codeforces.ml/contest/1251/problem/D
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define x first
#define y second
const int N = 2e5+7;
pii qlist[N];
int n;ll s;
bool check(ll mid)
{
	vector<pii> cnt[3];
	for(int i = 1;i <= n;++i)
	{
		int l = qlist[i].x,r = qlist[i].y;
		if(r < mid)	cnt[2].push_back({l,r});
		else if(l > mid)	cnt[0].push_back({l,r});
		else cnt[1].push_back({l,r});
	}
	ll fres = 0;
	for(int i = 0;i < cnt[0].size();++i)	fres += cnt[0][i].x;
	for(int i = 0;i < cnt[2].size();++i)	fres += cnt[2][i].x;
	int d = cnt[2].size() - cnt[0].size();
	if(d > 0)
	{
		if(d >= cnt[1].size())	return 0;
		for(int i = 0;i < d;++i)	fres += cnt[1][i].x;
		fres += (cnt[1].size() - d) * mid;
	}
	else	fres += mid * (cnt[1].size());
	return fres <= s;
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
		scanf("%d%lld",&n,&s);

		for(int i = 1;i <= n;++i)	scanf("%d%d",&qlist[i].x,&qlist[i].y);
		sort(qlist + 1,qlist + 1 + n);
		ll l = qlist[(n / 2) + 1].x,r = s / (n / 2 + 1);
		while(l < r)
		{
			ll mid = l + r + 1 >> 1;
			if(check(mid))	l = mid;
			else r = mid - 1;
		}
		printf("%lld\n",l);
    }
    return 0;
}


// Problem : D. Salary Changing
// Contest : Codeforces - Educational Codeforces Round 75 (Rated for Div. 2)
// URL : https://codeforces.ml/contest/1251/problem/D
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define x first
#define y second
const int N = 2e5+7;
pii qlist[N];
int n;ll s;
bool check(ll mid)
{
	vector<pii> cnt[3];
	for(int i = 1;i <= n;++i)
	{
		int l = qlist[i].x,r = qlist[i].y;
		if(r < mid)	cnt[2].push_back({l,r});
		else if(l > mid)	cnt[0].push_back({l,r});
		else cnt[1].push_back({l,r});
	}
	ll fres = 0;
	for(int i = 0;i < cnt[0].size();++i)	fres += cnt[0][i].x;
	for(int i = 0;i < cnt[2].size();++i)	fres += cnt[2][i].x;
	int d = cnt[2].size() - cnt[0].size();
	if(d > 0)
	{
		if(d >= cnt[1].size())	return 0;
		for(int i = 0;i < d;++i)	fres += cnt[1][i].x;
		fres += (cnt[1].size() - d) * mid;
	}
	else	fres += mid * (cnt[1].size());
	return fres <= s;
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
		scanf("%d%lld",&n,&s);

		for(int i = 1;i <= n;++i)	scanf("%d%d",&qlist[i].x,&qlist[i].y);
		sort(qlist + 1,qlist + 1 + n);
		ll l = qlist[(n / 2) + 1].x,r = s;
		while(l < r)
		{
			ll mid = l + r + 1 >> 1;
			if(check(mid))	l = mid;
			else r = mid - 1;
		}
		printf("%lld\n",l);
    }
    return 0;
}


// Problem : D. Salary Changing
// Contest : Codeforces - Educational Codeforces Round 75 (Rated for Div. 2)
// URL : https://codeforces.ml/contest/1251/problem/D
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define x first
#define y second
const int N = 2e5+7;
pii qlist[N];
int n;ll s;
bool check(ll mid)
{
	vector<pii> cnt[3];
	for(int i = 1;i <= n;++i)
	{
		int l = qlist[i].x,r = qlist[i].y;
		if(r < mid)	cnt[2].push_back({l,r});
		else if(l > mid)	cnt[0].push_back({l,r});
		else cnt[1].push_back({l,r});
	}
	ll fres = 0;
	for(int i = 0;i < cnt[0].size();++i)	fres += cnt[0][i].x;
	for(int i = 0;i < cnt[2].size();++i)	fres += cnt[2][i].y;
	int d = cnt[2].size() - cnt[0].size();
	if(d > 0)
	{

		if(d >= cnt[1].size())	return 0;
		for(int i = 0;i < d;++i)	fres += cnt[1][i].x,cnt[1][i].x = 0;
		fres += (cnt[1].size() - d) * mid;
	}
	else	fres += mid * (cnt[1].size());
	return fres <= s;
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
		scanf("%d%lld",&n,&s);
		for(int i = 1;i <= n;++i)	scanf("%d%d",&qlist[i].x,&qlist[i].y);
		sort(qlist + 1,qlist + 1 + n);
		ll l = 1,r = s;
		while(l < r)
		{
			ll mid = l + r + 1 >> 1;
			if(check(mid))	l = mid;
			else r = mid - 1;
		}
		printf("%lld\n",l);
    }
    return 0;
}


// Problem : D. Salary Changing
// Contest : Codeforces - Educational Codeforces Round 75 (Rated for Div. 2)
// URL : https://codeforces.ml/contest/1251/problem/D
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define x first
#define y second
const int N = 2e5+7;
pii qlist[N],quark[N];
int n;ll s;
bool check(ll mid)
{
	int cnt1 = 0,cnt2 = 0,cnt3 = 0;
	ll fres = 0;
	for(int i = 1;i <= n;++i)
	{
		int l = qlist[i].x,r = qlist[i].y;
		if(r < mid)	fres += l,++cnt1;
		else if(l > mid)	fres += l,++cnt2;
		else quark[++cnt3] = {l,r};
	}
	if(cnt1 > n / 2 || cnt2 > n / 2)	return 0;
	for(int i = 1;i <= n / 2 - cnt1;++i)	fres += quark[i].x;
	fres += mid * (n / 2 + 1 - cnt2);
	return fres <= s;
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
		scanf("%d%lld",&n,&s);

		for(int i = 1;i <= n;++i)	scanf("%d%d",&qlist[i].x,&qlist[i].y);
		sort(qlist + 1,qlist + 1 + n);
		ll l = qlist[(n / 2) + 1].x,r = s / (n / 2 + 1);
		while(l < r)
		{
			ll mid = l + r + 1 >> 1;
			if(check(mid))	l = mid;
			else r = mid - 1;
		}
		printf("%lld\n",l);
    }
    return 0;
}


// Problem : D. Salary Changing
// Contest : Codeforces - Educational Codeforces Round 75 (Rated for Div. 2)
// URL : https://codeforces.ml/contest/1251/problem/D
// Memory Limit : 256 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define x first
#define y second
const int N = 2e5+7;
pii qlist[N],quark[N];
int n;ll s;
bool check(ll mid)
{
	int cnt1 = 0,cnt2 = 0,cnt3 = 0;
	ll fres = 0;
	for(int i = 1;i <= n;++i)
	{
		int l = qlist[i].x,r = qlist[i].y;
		if(r < mid)	fres += l,++cnt1;
		else if(l > mid)	fres += l,++cnt2;
		else quark[++cnt3] = {l,r};
	}
	if(cnt1 > n / 2 || cnt2 > n / 2)	return 0;
	for(int i = 1;i <= n / 2 - cnt1;++i)	fres += quark[i].x;
	fres += mid * (n / 2 + 1 - cnt2);
	return fres <= s;
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
		scanf("%d%lld",&n,&s);

		for(int i = 1;i <= n;++i)	scanf("%d%d",&qlist[i].x,&qlist[i].y);
		sort(qlist + 1,qlist + 1 + n);
		ll l = qlist[(n / 2) + 1].x,r = s;
		while(l < r)
		{
			ll mid = l + r + 1 >> 1;
			if(check(mid))	l = mid;
			else r = mid - 1;
		}
		printf("%lld\n",l);
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
ll n,sum;
bool vis[maxn];
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
bool check(ll now){
	for(int i=1;i<=n;i++) vis[i]=0;
	ll ans=0,cnt=0;
	for(int i=1;i<=n&&cnt<(n+1)/2;i++){
		if(s[i].r>=now){
			vis[i]=1;
			cnt++;
		}
	}
	if(cnt<(n+1)/2) return 0;
	for(int i=1;i<=n;i++){
		if(vis[i]) ans+=max(s[i].l,now);
		else ans+=s[i].l;
	}
	return ans<=sum;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		cin>>n>>sum;
		for(int i=1;i<=n;i++) cin>>s[i].l>>s[i].r;
		if(n==1){
			cout<<min(s[1].r,sum)<<endl;
			continue;
		}
		sort(s+1,s+n+1,kmp);
		ll l=1,r=sum,ans,mid;
		while(l<=r){
			mid=(l+r)>>1;
			if(check(mid)) l=mid+1,ans=mid;
			else r=mid-1;
		}
		cout<<ans<<endl;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
bool cmp(Libm x,Libm y){
	if(x.r==y.r) return x.l<y.l;
	return x.r<y.r;
}
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		int n;
		ll sum,now=0;
		cin>>n>>sum;
		for(int i=1;i<=n;i++){
			cin>>s[i].l>>s[i].r;
			now+=s[i].r;
		}
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,cmp);
		for(int i=1;i<=n/2;i++) now-=(s[i].r-s[i].l);
		if(now<=sum) cout<<s[n/2+1].r<<endl;
		else{
			for(int i=n/2+2;i<=n;i++) now-=(s[i].r-max(s[n/2+1].r,s[i].l));
			if(now<=sum) cout<<s[n/2+1].r<<endl;
			else{
				sort(s+2+n/2,s+n+1,kmp);
				ll cur=n/2+2,del=0;
				while(now>sum){
					while(s[n/2+1].r-s[cur].l-del<=0&&cur<n) cur++;
					if(s[n/2+1].r-s[cur].l-del<=0) break;
					ll d=(now-sum)/(n-cur+1);
					if((now-sum)%(n-cur+1)!=0) d++;
					if(s[n/2+1].r-del-s[cur].l>=d){
						now=sum;
						del+=d;
					}
					else{
						now-=(n-cur+1)*(s[n/2+1].r-del-s[cur].l);
						del=s[n/2+1].r-s[cur].l;
					}
				}
				if(now<=sum) cout<<s[n/2+1].r-del<<endl;
				else cout<<s[n/2+1].r-del-(now-sum)<<endl;
			}
		}
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
bool cmp(Libm x,Libm y){
	if(x.r==y.r) return x.l<y.l;
	return x.r<y.r;
}
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		int n;
		ll sum,now=0;
		cin>>n>>sum;
		for(int i=1;i<=n;i++){
			cin>>s[i].l>>s[i].r;
			now+=s[i].r;
		}
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,cmp);
		for(int i=1;i<=n/2;i++){
			if(now+s[i].l-s[i].r>sum) now-=(s[i].r-s[i].l);
			else{
				now=sum;
				break;
			}
		}
		if(now==sum) cout<<s[n/2+1].r<<endl;
		else{
			for(int i=n/2+2;i<=n;i++){
				if(now+max(s[n/2+1].r,s[i].l)-s[i].r>sum) now-=(s[i].r-max(s[n/2+1].r,s[i].l));
				else{
					now=sum;
					break;
				}
			}
			if(now==sum) cout<<s[n/2+1].r<<endl;
			else{
				sort(s+2+n/2,s+n+1,kmp);
				int cur=n/2+2,del=0;
				while(now!=sum){
					while(s[n/2+1].r-s[cur].l-del<=0) cur++;
					if((now-sum)>(n-cur+1)*(s[n/2+1].r-s[cur].l-del)){
						now-=(s[n/2+1].r-s[cur].l-del)*(n-cur+1);
						del=s[n/2+1].r-s[cur].l;
					}
					else if((now-sum)==(n-cur+1)*(s[n/2+1].r-s[cur].l-del)){
						del=s[n/2+1].r-s[cur].l;
						now=sum;
					}
					else{
						del=(now-sum)/(n-cur+1)+1;
						now=sum;
					}
				}
				cout<<s[n/2+1].r-del<<endl;
			}
		}
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
bool cmp(Libm x,Libm y){
	if(x.r==y.r) return x.l<y.l;
	return x.r<y.r;
}
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		int n;
		ll sum,now=0;
		cin>>n>>sum;
		for(int i=1;i<=n;i++){
			cin>>s[i].l>>s[i].r;
			now+=s[i].r;
		}
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,cmp);
		for(int i=1;i<=n/2;i++){
			if(now+s[i].l-s[i].r>sum) now-=(s[i].r-s[i].l);
			else{
				now=sum;
				break;
			}
		}
		if(now==sum) cout<<s[n/2+1].r<<endl;
		else{
			for(int i=n/2+2;i<=n;i++){
				if(now+max(s[n/2+1].r,s[i].l)-s[i].r>sum) now-=(s[i].r-max(s[n/2+1].r,s[i].l));
				else{
					now=sum;
					break;
				}
			}
			if(now==sum) cout<<s[n/2+1].r<<endl;
			else{
				sort(s+2+n/2,s+n+1,kmp);
				ll cur=n/2+2,del=0;
				while(now!=sum){
					while(s[n/2+1].r-s[cur].l-del<=0&&cur<n) cur++;
					if((now-sum)>(n-cur+1)*(s[n/2+1].r-s[cur].l-del)){
						now-=(s[n/2+1].r-s[cur].l-del)*(n-cur+1);
						del=s[n/2+1].r-s[cur].l;
					}
					else if((now-sum)==(n-cur+1)*(s[n/2+1].r-s[cur].l-del)){
						del=s[n/2+1].r-s[cur].l;
						now=sum;
					}
					else{
						del+=(now-sum)/(n-cur+1);
						if((now-sum)%(n-cur+1)) del++;
						now=sum;
					}
					if(cur==n) break;
				}
				if(now==sum) cout<<s[n/2+1].r-del<<endl;
				else cout<<s[n/2+1].r-del-(now-sum)<<endl;
			}
		}
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
ll n,sum;
bool vis[maxn];
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
bool check(ll now){
	for(int i=1;i<=n;i++) vis[i]=0;
	ll ans=0,cnt=0;
	for(int i=1;i<=n&&cnt<(n/2+1);i++){
		if(s[i].r>=now){
			vis[i]=1;
			cnt++;
		}
	}
	if(cnt<(n/2+1)) return 0;
	for(int i=1;i<=n;i++){
		if(vis[i]) ans+=max(s[i].l,now);
		else ans+=s[i].l;
	}
	return ans<=sum;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		cin>>n>>sum;
		for(int i=1;i<=n;i++) cin>>s[i].l>>s[i].r;
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,kmp);
		ll l=1,r=sum,ans,mid;
		while(l<=r){
			mid=(l+r)>>1;
			if(check(mid)) l=mid+1,ans=mid;
			else r=mid-1;
		}
		cout<<ans<<endl;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
ll n,sum;
bool vis[maxn];
bool kmp(Libm x,Libm y){
	return x.l<y.l;
}
bool check(ll now){
	memset(vis,0,n+1);
	ll ans=0,cnt=0;
	for(int i=1;i<=n&&cnt<=n/2+1;i++){
		if(s[i].r>=now){
			vis[i]=1;
			cnt++;
		}
	}
	if(cnt<n/2+1) return 0;
	for(int i=1;i<=n;i++){
		if(vis[i]) ans+=max(s[i].l,now);
		else ans+=s[i].l;
	}
	return ans<=sum;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		cin>>n>>sum;
		for(int i=1;i<=n;i++) cin>>s[i].l>>s[i].r;
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,kmp);
		ll l=1,r=sum,ans;
		while(l<=r){
			ll mid=(l+r)>>1;
			if(check(mid)) l=mid+1,ans=mid;
			else r=mid-1;
		}
		cout<<ans<<endl;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
ll n,sum;
bool vis[maxn];
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
bool check(ll now){
	for(int i=1;i<=n;i++) vis[i]=0;
	ll ans=0,cnt=0;
	for(int i=1;i<=n&&cnt<(n+1)/2;i++){
		if(s[i].r>=now){
			vis[i]=1;
			cnt++;
		}
	}
	if(cnt<(n+1)/2) return 0;
	for(int i=1;i<=n;i++){
		if(vis[i]) ans+=max(s[i].l,now);
		else ans+=s[i].l;
	}
	return ans<=sum;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		cin>>n>>sum;
		for(int i=1;i<=n;i++) cin>>s[i].l>>s[i].r;
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,kmp);
		ll l=1,r=sum,ans,mid;
		while(l<=r){
			mid=(l+r)>>1;
			if(check(mid)) l=mid+1,ans=mid;
			else r=mid-1;
		}
		cout<<ans<<endl;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
bool cmp(Libm x,Libm y){
	if(x.r==y.r) return x.l<y.l;
	return x.r<y.r;
}
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		int n;
		ll sum,now=0;
		cin>>n>>sum;
		for(int i=1;i<=n;i++){
			cin>>s[i].l>>s[i].r;
			now+=s[i].r;
		}
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,cmp);
		for(int i=1;i<=n/2;i++) now-=(s[i].r-s[i].l);
		if(now<=sum) cout<<s[n/2+1].r<<endl;
		else{
			for(int i=n/2+2;i<=n;i++) now-=(s[i].r-max(s[n/2+1].r,s[i].l));
			if(now<=sum) cout<<s[n/2+1].r<<endl;
			else{
				sort(s+2+n/2,s+n+1,kmp);
				ll cur=n/2+2,del=0;
				while(now>sum){
					while(s[n/2+1].r-s[cur].l-del<=0&&cur<n) cur++;
					if((now-sum)>(n-cur+1)*(s[n/2+1].r-s[cur].l-del)){
						now-=(s[n/2+1].r-s[cur].l-del)*(n-cur+1);
						del=s[n/2+1].r-s[cur].l;
					}
					else if((now-sum)==(n-cur+1)*(s[n/2+1].r-s[cur].l-del)){
						del=s[n/2+1].r-s[cur].l;
						now=sum;
					}
					else{
						del+=(now-sum)/(n-cur+1);
						if((now-sum)%(n-cur+1)!=0) del++;
						now=sum;
					}
					if(cur==n) break;
				}
				if(now<=sum) cout<<s[n/2+1].r-del<<endl;
				else cout<<s[n/2+1].r-del-(now-sum)<<endl;
			}
		}
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
bool cmp(Libm x,Libm y){
	if(x.r==y.r) return x.l<y.l;
	return x.r<y.r;
}
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		int n;
		ll sum,now=0;
		cin>>n>>sum;
		for(int i=1;i<=n;i++){
			cin>>s[i].l>>s[i].r;
			now+=s[i].r;
		}
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,cmp);
		for(int i=1;i<=n/2;i++) now-=(s[i].r-s[i].l);
		if(now<=sum) cout<<s[n/2+1].r<<endl;
		else{
			for(int i=n/2+2;i<=n;i++) now-=(s[i].r-max(s[n/2+1].r,s[i].l));
			if(now<=sum) cout<<s[n/2+1].r<<endl;
			else{
				sort(s+2+n/2,s+n+1,kmp);
				ll cur=n/2+2,del=0;
				while(now>sum){
					while(s[n/2+1].r-s[cur].l-del<=0&&cur<n) cur++;
					ll d=(now-sum)/(n-cur+1);
					if((now-sum)%(n-cur+1)!=0) d++;
					if(s[n/2+1].r-del-s[cur].l>=d){
						now=sum;
						del+=d;
					}
					else{
						now-=(n-cur+1)*(s[n/2+1].r-del-s[cur].l);
						del=s[n/2+1].r-s[cur].l;
					}
					if(cur==n) break;
				}
				if(now<=sum) cout<<s[n/2+1].r-del<<endl;
				else cout<<s[n/2+1].r-del-(now-sum)<<endl;
			}
		}
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
bool cmp(Libm x,Libm y){
	if(x.r==y.r) return x.l<y.l;
	return x.r<y.r;
}
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		ll n;
		ll sum,now=0;
		cin>>n>>sum;
		for(int i=1;i<=n;i++){
			cin>>s[i].l>>s[i].r;
			now+=s[i].r;
		}
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,cmp);
		ll ans=s[n/2+1].l;
		sort(s+1,s+n+1,cmp);
		for(int i=1;i<=n/2;i++) now-=(s[i].r-s[i].l);
		if(now<=sum) cout<<s[n/2+1].r<<endl;
		else{
			for(int i=n/2+2;i<=n;i++) now-=(s[i].r-max(s[n/2+1].r,s[i].l));
			if(now<=sum) cout<<s[n/2+1].r<<endl;
			else{
				sort(s+2+n/2,s+n+1,kmp);
				ll cur=n/2+2,del=0;
				while(now>sum){
					while(s[n/2+1].r-s[cur].l-del<=0&&cur<n) cur++;
					if(s[n/2+1].r-s[cur].l-del<=0) break;
					ll d=(now-sum)/(n-cur+1);
					if((now-sum)%(n-cur+1)!=0) d++;
					if(s[n/2+1].r-del-s[cur].l>=d){
						now=sum;
						del+=d;
					}
					else{
						now-=(n-cur+1)*(s[n/2+1].r-del-s[cur].l);
						del=s[n/2+1].r-s[cur].l;
					}
				}
				if(now<=sum) cout<<max(ans,s[n/2+1].r-del)<<endl;
				else cout<<max(ans,s[n/2+1].r-del-(now-sum))<<endl;
			}
		}
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
ll n,sum;
bool vis[maxn];
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
bool check(ll now){
	memset(vis,0,n+1);
	ll ans=0,cnt=0;
	for(int i=1;i<=n&&cnt<=n/2+1;i++){
		if(s[i].r>=now){
			vis[i]=1;
			cnt++;
		}
	}
	if(cnt<n/2+1) return 0;
	for(int i=1;i<=n;i++){
		if(vis[i]) ans+=max(s[i].l,now);
		else ans+=s[i].l;
	}
	return ans<=sum;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		cin>>n>>sum;
		for(int i=1;i<=n;i++) cin>>s[i].l>>s[i].r;
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,kmp);
		ll l=1,r=sum,ans;
		while(l<=r){
			ll mid=(l+r)>>1;
			if(check(mid)) l=mid+1,ans=mid;
			else r=mid-1;
		}
		cout<<ans<<endl;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
ll n,sum;
bool vis[maxn];
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
bool check(ll now){
	memset(vis,0,n+1);
	ll ans=0,cnt=0;
	for(int i=1;i<=n&&cnt<n/2+1;i++){
		if(s[i].r>=now){
			vis[i]=1;
			cnt++;
		}
	}
	if(cnt<n/2+1) return 0;
	for(int i=1;i<=n;i++){
		if(vis[i]) ans+=max(s[i].l,now);
		else ans+=s[i].l;
	}
	return ans<=sum;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		cin>>n>>sum;
		for(int i=1;i<=n;i++) cin>>s[i].l>>s[i].r;
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,kmp);
		ll l=1,r=sum,ans;
		while(l<=r){
			ll mid=(l+r)>>1;
			if(check(mid)) l=mid+1,ans=mid;
			else r=mid-1;
		}
		cout<<ans<<endl;
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
bool cmp(Libm x,Libm y){
	if(x.r==y.r) return x.l<y.l;
	return x.r<y.r;
}
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		ll n;
		ll sum,now=0;
		cin>>n>>sum;
		for(int i=1;i<=n;i++){
			cin>>s[i].l>>s[i].r;
			now+=s[i].r;
		}
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,kmp);
		ll ans=s[n/2+1].l;
		sort(s+1,s+n+1,cmp);
		for(int i=1;i<=n/2;i++) now-=(s[i].r-s[i].l);
		if(now<=sum) cout<<s[n/2+1].r<<endl;
		else{
			for(int i=n/2+2;i<=n;i++) now-=(s[i].r-max(s[n/2+1].r,s[i].l));
			if(now<=sum) cout<<s[n/2+1].r<<endl;
			else{
				sort(s+2+n/2,s+n+1,kmp);
				ll cur=n/2+2,del=0;
				while(now>sum){
					while(s[n/2+1].r-s[cur].l-del<=0&&cur<n) cur++;
					if(s[n/2+1].r-s[cur].l-del<=0) break;
					ll d=(now-sum)/(n-cur+1);
					if((now-sum)%(n-cur+1)!=0) d++;
					if(s[n/2+1].r-del-s[cur].l>=d){
						now=sum;
						del+=d;
					}
					else{
						now-=(n-cur+1)*(s[n/2+1].r-del-s[cur].l);
						del=s[n/2+1].r-s[cur].l;
					}
				}
				if(now<=sum) cout<<max(ans,s[n/2+1].r-del)<<endl;
				else cout<<max(ans,s[n/2+1].r-del-(now-sum))<<endl;
			}
		}
	}
}


#include <bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
const int maxn=2e5+5;
struct Libm{
	ll l,r;
}s[maxn];
bool cmp(Libm x,Libm y){
	if(x.r==y.r) return x.l<y.l;
	return x.r<y.r;
}
bool kmp(Libm x,Libm y){
	return x.l>y.l;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int T;cin>>T;
	while(T--){
		int n;
		ll sum,now=0;
		cin>>n>>sum;
		for(int i=1;i<=n;i++){
			cin>>s[i].l>>s[i].r;
			now+=s[i].r;
		}
		if(n==1){
			cout<<sum<<endl;
			continue;
		}
		sort(s+1,s+n+1,cmp);
		for(int i=1;i<=n/2;i++){
			if(now+s[i].l-s[i].r>sum) now-=(s[i].r-s[i].l);
			else{
				now=sum;
				break;
			}
		}
		if(now==sum) cout<<s[n/2+1].r<<endl;
		else{
			for(int i=n/2+2;i<=n;i++){
				if(now+max(s[n/2+1].r,s[i].l)-s[i].r>sum) now-=(s[i].r-max(s[n/2+1].r,s[i].l));
				else{
					now=sum;
					break;
				}
			}
			if(now==sum) cout<<s[n/2+1].r<<endl;
			else{
				sort(s+2+n/2,s+n+1,kmp);
				ll cur=n/2+2,del=0;
				while(now!=sum){
					while(s[n/2+1].r-s[cur].l-del<=0) cur++;
					if((now-sum)>(n-cur+1)*(s[n/2+1].r-s[cur].l-del)){
						now-=(s[n/2+1].r-s[cur].l-del)*(n-cur+1);
						del=s[n/2+1].r-s[cur].l;
					}
					else if((now-sum)==(n-cur+1)*(s[n/2+1].r-s[cur].l-del)){
						del=s[n/2+1].r-s[cur].l;
						now=sum;
					}
					else{
						del+=(now-sum)/(n-cur+1);
						if((now-sum)%(n-cur+1)) del++;
						now=sum;
					}
				}
				cout<<s[n/2+1].r-del<<endl;
			}
		}
	}
}


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

inline int read()
{
	int ret=0,f=0;char c=gc();
	while(!isdigit(c)){if(c=='-')f=1;c=gc();}
	while(isdigit(c)){ret=ret*10+c-48;c=gc();}
	if(f)return -ret;return ret;
}

const int N=200005;
int n,s,l,r,ans;
int ll[N],rr[N];

inline int max(int a,int b)
{
	return a>b?a:b;
}

inline bool jay(int mid)
{
	int sum=0;
	int all=0;
	for (register int i=1; i<=n; ++i)
	if (rr[i]<mid) sum++,all+=ll[i];
	if (sum>n/2) return false;
	for (register int i=1; i<=n; ++i)
	if (rr[i]>=mid) all+=max(mid,ll[i]);
	if (all>s) return false;
	for (register int i=1; i<=n; ++i)
	if (ll[i]<=mid && mid<=rr[i]) return true;
	return false;
}

signed main(){
	int T;
	T=read();
	while (T--)
	{
		n=read(); s=read();
		for (register int i=1; i<=n; ++i) ll[i]=read(),rr[i]=read();
		l=1; r=1e9; ans=-1;
		while (l<=r)
		{
			int mid=l+r>>1;
			if (jay(mid)) ans=mid,l=mid+1;
			else r=mid-1;
		}
		printf("%I64d\n",ans);
	}
return 0;
}


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

inline int read()
{
	int ret=0,f=0;char c=gc();
	while(!isdigit(c)){if(c=='-')f=1;c=gc();}
	while(isdigit(c)){ret=ret*10+c-48;c=gc();}
	if(f)return -ret;return ret;
}

const int N=5005;
int n,s,l,r,ans;
int ll[N],rr[N];

inline int max(int a,int b)
{
	return a>b?a:b;
}

inline bool jay(int mid)
{
	int sum=0;
	int all=0;
	for (register int i=1; i<=n; ++i)
	if (rr[i]<mid) sum++,all+=ll[i];
	if (sum>n/2) return false;
	for (register int i=1; i<=n; ++i)
	if (rr[i]>=mid) all+=max(mid,ll[i]);
	if (all>s) return false;
	return true;
}

signed main(){
	int T;
	T=read();
	while (T--)
	{
		n=read(); s=read();
		for (register int i=1; i<=n; ++i) ll[i]=read(),rr[i]=read();
		l=1; r=1e9; ans=-1;
		while (l<=r)
		{
			int mid=l+r>>1;
			if (jay(mid)) ans=mid,l=mid+1;
			else r=mid-1;
		}
		printf("%I64d\n",ans);
	}
return 0;
}


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

inline int read()
{
	int ret=0,f=0;char c=gc();
	while(!isdigit(c)){if(c=='-')f=1;c=gc();}
	while(isdigit(c)){ret=ret*10+c-48;c=gc();}
	if(f)return -ret;return ret;
}

const int N=200005;
int n,s,l,r,ans;
int ll[N],rr[N];

inline int max(int a,int b)
{
	return a>b?a:b;
}

inline bool jay(int mid)
{
	int sum=0;
	int all=0;
	for (register int i=1; i<=n; ++i)
	if (rr[i]<mid) sum++,all+=ll[i];
	if (sum>n/2) return false;
	for (register int i=1; i<=n; ++i)
	if (rr[i]>=mid) all+=max(mid,ll[i]);
	if (all>s) return false;
	return true;
}

signed main(){
	int T;
	T=read();
	while (T--)
	{
		n=read(); s=read();
		for (register int i=1; i<=n; ++i) ll[i]=read(),rr[i]=read();
		l=1; r=1e9; ans=-1;
		while (l<=r)
		{
			int mid=l+r>>1;
			if (jay(mid)) ans=mid,l=mid+1;
			else r=mid-1;
		}
		printf("%I64d\n",ans);
	}
return 0;
}


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

inline int read()
{
	int ret=0,f=0;char c=gc();
	while(!isdigit(c)){if(c=='-')f=1;c=gc();}
	while(isdigit(c)){ret=ret*10+c-48;c=gc();}
	if(f)return -ret;return ret;
}

const int N=200005;
int n,s,l,r,ans;
int ll[N],rr[N];

inline int max(int a,int b)
{
	return a>b?a:b;
}

inline bool jay(int mid)
{
	int sum=0;
	int all=0;
	for (register int i=1; i<=n; ++i)
	if (rr[i]<mid) sum++,all+=ll[i];
	if (sum>n/2) return false;
	for (register int i=1; i<=n; ++i)
	if (rr[i]>=mid) all+=max(mid,ll[i]);
	if (all>s) return false;
	//for (register int i=1; i<=n; ++i)
	//if (ll[i]<=mid && mid<=rr[i]) return true;
	return true;
}

signed main(){
	int T;
	T=read();
	while (T--)
	{
		n=read(); s=read();
		for (register int i=1; i<=n; ++i) ll[i]=read(),rr[i]=read();
		l=1; r=1e9; ans=-1;
		while (l<=r)
		{
			int mid=l+r>>1;
			if (jay(mid)) ans=mid,l=mid+1;
			else r=mid-1;
		}
		printf("%I64d\n",ans);
	}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define next Next
#define gc getchar
#define int long long
const int N=1e6+5;
int n,m,L[N],R[N];
/*char buf[1<<21],*p1=buf,*p2=buf;
inline int gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}*/
inline int read()
{
    int ret=0,f=0;char c=gc();
    while(!isdigit(c)){if(c=='-')f=1;c=gc();}
    while(isdigit(c)){ret=ret*10+c-48;c=gc();}
    if(f)return -ret;return ret;
}
bool pd(int mid)
{
	multiset<int>s;
	int A=0,B=0,sum=0;
	for(int i=1;i<=n;i++)
	{
		if(L[i]>mid)A++;
		else if(R[i]<mid)B++;
		else s.insert(mid-L[i]);
		sum+=L[i];
	}
	if(A>n/2)return 1;
	B=n/2+1-A;
	if(s.size()<B)return 0;
	while(B--)
	{
		sum+=*s.begin();
		s.erase(s.begin());
	}
	return sum<=m;
}
signed main()
{
	int T=read();
	while(T--)
	{
		n=read();m=read();
		for(int i=1;i<=n;i++)L[i]=read(),R[i]=read();
		int l=1,r=1e9;
		while(l<r)
		{
			int mid=(l+r+1)/2;
			if(pd(mid))l=mid;
			else r=mid-1;
		}
		printf("%lld\n",l);
	}
}


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

inline int read()
{
	int ret=0,f=0;char c=gc();
	while(!isdigit(c)){if(c=='-')f=1;c=gc();}
	while(isdigit(c)){ret=ret*10+c-48;c=gc();}
	if(f)return -ret;return ret;
}

const int N=5005;
int n,s,l,r,ans;
int ll[N],rr[N];

inline bool jay(int mid)
{
	int sum=0;
	int all=0;
	for (register int i=1; i<=n; ++i)
	if (rr[i]<mid) sum++,all+=ll[i];
	if (sum>n/2) return false;
	if (all+(n/2+1)*mid>s) return false;
	return true;
}

signed main(){
	int T;
	T=read();
	while (T--)
	{
		n=read(); s=read();
		for (register int i=1; i<=n; ++i) ll[i]=read(),rr[i]=read();
		l=1; r=1e9; ans=-1;
		while (l<=r)
		{
			int mid=l+r>>1;
			if (jay(mid)) ans=mid,l=mid+1;
			else r=mid-1;
		}
		printf("%I64d\n",ans);
	}
return 0;
}


// ===================================
//   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; continue; }
        if (a[i].first>mid) { ++cnt2,rest-=a[i].first; continue; }
        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 if (cnt2<n/2) ++cnt2,rest-=mid+1;
        else 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("%lld\n",L);
    }
    return 0;
}


// ===================================
//   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; continue; }
        if (a[i].first>mid) { ++cnt2,rest-=a[i].first; continue; }
        vec.push_back(a[i]);
    }
    if (cnt1>n/2||cnt2>n/2) return 0;
    sort(vec.begin(),vec.end(),cmp1); int flag=0;
    for (auto i:vec) {
        if (cnt1<n/2) ++cnt1,rest-=i.first;
        else if (!flag) flag=1,rest-=mid;
        else ++cnt2,rest-=mid+1;
    }
    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;
}


// ===================================
//   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; continue; }
        if (a[i].first>mid) { ++cnt2,rest-=a[i].first; continue; }
        vec.push_back(a[i]);
    }
    if (cnt1>n/2||cnt2>n/2) return 0;
    sort(vec.begin(),vec.end(),cmp1); int flag=0;
    for (auto i:vec) {
        if (cnt1<n/2) ++cnt1,rest-=i.first;
        else if (!flag) flag=1,rest-=mid;
        else ++cnt2,rest-=mid+1;
    }
    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;
        }
        if (check(L+1)) ++L;
        printf("%I64d\n",L);
    }
    return 0;
}


// ===================================
//   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; continue; }
        if (a[i].first>mid) { ++cnt2,rest-=a[i].first; continue; }
        vec.push_back(a[i]);
    }
    if (cnt1>n/2||cnt2>n/2) return 0;
    sort(vec.begin(),vec.end(),cmp1);
    for (auto i:vec) {
        if (cnt1<n/2) ++cnt1,rest-=i.first;
        else 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;
}