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

void solve(){
	int n,p,k,ans=-1;
	cin >> n >>p >> k;
	int a[n],sum[n+1];
	for(int i=0;i<n;i++) cin >> a[i];
	sort(a,a+n);
	sum[0] = a[0];
	sum[n] = 0;
	for(int i=1;i<k-1;i++) sum[i] = sum[i-1] + a[i];
	for(int i=k-1;i<n;i++) {
		if(i-k >= 0) sum[i] = sum[i-k] + a[i];
		else sum[i] = a[i];
	}
	for(int i=0;i<n;i++) {
		if(sum[i] <= p) ans = i;
	}
	cout << ans+1 << endl;

}

int main() {
	int t;
	cin >> t;
	while(t--) solve();
	return 0;
}


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

void solve() {
	int n,p,k,x,cnt=0,ans=0,temp;
	cin >> n >> p >> k;
	int a[n];
	for(int i=0;i<n;i++) cin >> a[i];
	int j=0;
	for(;j<n;j++) {
		if(a[j] > p) break;
	}
	j--;
	temp = p;
	cnt = 0;
	x= j;
	while( x >= 0 ) {
		//cout  << x << " " << temp << endl;
		if(x >= 1 && temp>=a[x]) {
			cnt+=2;
			temp -= a[x];
			x -= 2;
		}
		else if(x >= 0 && temp >= a[x]) {
			cnt++;
			temp -= a[x];
			x--;
		}
		else break;
	}
	ans = max(ans,cnt);
	temp = p;
	cnt = 0;
	x= j-1;
	while( x >= 0 ) {
		//cout  << x << " " << temp << endl;
		if(x >= 1 && temp>=a[x]) {
			cnt+=2;
			temp -= a[x];
			x -= 2;
		}
		else if(x >= 0 && temp >= a[x]) {
			cnt++;
			temp -= a[x];
			x--;
		}
		else break;
	}
	ans = max(cnt,ans);
	cout << ans << endl;
}

int main() {
	int t;
	cin >> t;
	while(t--) solve();
	return 0;
}


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

void solve(){
	int n,p,k,ans=0,temp,cnt=0,x;
	cin >> n >> p >> k;
	int a[n];
	for(int i=0;i<n;i++) cin >> a[i];
	sort(a,a+n);
	int j;
	for(j=0;j<n;j++) {
		if(a[j] > p) break;
	}
	j--;
	//cout << j << endl;
	for(;j>=0;j--){
		temp = p;
		cnt = 0;
		x= j;
		while( x >= 0 ) {
			//cout  << x << " " << temp << endl;
			if(x >= 1 && temp>=a[x]) {
				cnt+=2;
				temp -= a[x];
				x -= 2;
			}
			else if(x >= 0 && temp >= a[x]) {
				cnt++;
				temp -= a[x];
				x--;
			}
			else break;
		}
		//cout <<  j << "  " << cnt << endl;
		ans = max(ans,cnt);
	}
	cout << ans << endl;
}

int main() {
	int t;
	cin >> t;
	while(t--) solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SIZE (int) (2e5+7)

ll n;
ll a[SIZE];

ll buy(ll k,ll p, ll i){
	ll x=0,y=0;
	//cout << p << " " << i << endl;
	if(p <= 0 || i >= n) return 0;
	if(i<n-1 && p >= a[i+1]) x = 2 + buy(k,p-a[i+1],i+2);
	if(i<n && p >= a[i]) y = 1 + buy(k,p-a[i],i+1);
	return max(x,y);
}

int main() {
	int t;
	cin >> t;
	while(t--) {
		ll k,p;
		cin >> n >> p >> k;
		for(int i=0;i<n;i++) cin >> a[i];
		sort(a,a+n);
		ll ans = buy(k,p,0);
		cout << ans << endl;
	}
	return 0;
}


/**Bismillahir Rahmanir Raheem
    Author:Refat(BSMRSTU)**/
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define pil pair<int,ll>
const int N = 1e5 + 5;
const int M = 1e9 + 7;
const double pi=2*acos(0.0);
const double esp=1e-9;
int Set(int N,int pos) {
    return N=N | (1<<pos);
}
int reset(int N,int pos) {
    return N= N & ~(1<<pos);
}
bool check(int N,int pos) {
    return (bool)(N & (1<<pos));
}
int ar[N];
int main() {
    /// freopen("input.txt","r",stdin);
    /// freopen("output.txt","w",stdout);
    int test,n,p,k,ans,tem,ck;
    scanf("%d",&test);
    for(int cs=1; cs<=test; cs++) {
        scanf("%d %d %d",&n,&p,&k);
        ans=0;
        for(int i=1; i<=n; i++) {
            scanf("%d",&ar[i]);
        }
        sort(ar+1,ar+n+1);
        if(n==1) {
            if(p<=ar[1]) {
                printf("1\n");
            } else {
                printf("0\n");
            }
        } else {
            int i;
            for(i=1; i<n; i++) {
                tem=p-ar[i];
                if(tem<0) {
                    break;
                }
                else {
                    if(tem<ar[i+1]) {
                        ck=max(ar[i],ar[i+1]);
                        if(ck<=p) {
                            ans+=2;
                            p-=ck;
                            //break;
                            i++;
                        }
                        else{
                            ans++;
                            p-=ar[i];
                        }
                    } else {
                        ans++;
                        p-=ar[i];
                    }
                }
            }
            if(i==n) {
                if(p>=ar[n])ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


/**Bismillahir Rahmanir Raheem
    Author:Refat(BSMRSTU)**/
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define pil pair<int,ll>
const int N = 1e5 + 5;
const int M = 1e9 + 7;
const double pi=2*acos(0.0);
const double esp=1e-9;
int Set(int N,int pos) {
    return N=N | (1<<pos);
}
int reset(int N,int pos) {
    return N= N & ~(1<<pos);
}
bool check(int N,int pos) {
    return (bool)(N & (1<<pos));
}
int ar[N];
int main() {
    /// freopen("input.txt","r",stdin);
    /// freopen("output.txt","w",stdout);
    int test,n,p,k,ans,tem,ck;
    scanf("%d",&test);
    for(int cs=1; cs<=test; cs++) {
        scanf("%d %d %d",&n,&p,&k);
        ans=0;
        for(int i=1; i<=n; i++) {
            scanf("%d",&ar[i]);
        }
        sort(ar+1,ar+n+1);
        if(n==1) {
            if(p<=ar[1]) {
                printf("1\n");
            } else {
                printf("0\n");
            }
        } else {
            int i;
            for(i=1; i<n; i++) {
                tem=p-ar[i];
                if(tem<0) {
                    break;
                }
                else {
                    if(tem<ar[i+1]) {
                        ck=max(ar[i],ar[i+1]);
                        if(ck<=p) {
                            ans+=2;
                            break;
                        }
                        else{
                            ans++;
                            break;
                        }
                    } else {
                        ans++;
                        p-=ar[i];
                    }
                }
            }
            if(i==n) {
                if(p>=ar[n])ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


/**Bismillahir Rahmanir Raheem
    Author:Refat(BSMRSTU)**/
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define pil pair<int,ll>
const int N = 2e5 + 5;
const int M = 1e9 + 7;
const double pi=2*acos(0.0);
const double esp=1e-9;
int Set(int N,int pos) {
    return N=N | (1<<pos);
}
int reset(int N,int pos) {
    return N= N & ~(1<<pos);
}
bool check(int N,int pos) {
    return (bool)(N & (1<<pos));
}
int ar[N],p;

bool fun(int l,int t) {
    for(int i=l; i>=1;) {
        if(ar[i]<=t) {
            t-=ar[i];
            i-=2;
        } else {
            return false;
        }
    }
    return true;
}
int main() {
    /// freopen("input.txt","r",stdin);
    /// freopen("output.txt","w",stdout);
    int test,n,k,ans,tem,ck;
    scanf("%d",&test);
    for(int cs=1; cs<=test; cs++) {
        scanf("%d %d %d",&n,&p,&k);
        ans=0;
        for(int i=1; i<=n; i++) {
            scanf("%d",&ar[i]);
        }
        sort(ar+1,ar+n+1);
        int l=1,r=n;
        while(l<=r) {
            int mid=(l+r)>>1;
            if(fun(mid,p)) {
                l=mid+1;
                ans=mid;
            } else {
                r=mid-1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


/**Bismillahir Rahmanir Raheem
    Author:Refat(BSMRSTU)**/
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define pil pair<int,ll>
const int N = 1e5 + 5;
const int M = 1e9 + 7;
const double pi=2*acos(0.0);
const double esp=1e-9;
int Set(int N,int pos) {
    return N=N | (1<<pos);
}
int reset(int N,int pos) {
    return N= N & ~(1<<pos);
}
bool check(int N,int pos) {
    return (bool)(N & (1<<pos));
}
int ar[N];
int main() {
    /// freopen("input.txt","r",stdin);
    /// freopen("output.txt","w",stdout);
    int test,n,p,k,ans,tem,ck;
    scanf("%d",&test);
    for(int cs=1; cs<=test; cs++) {
        scanf("%d %d %d",&n,&p,&k);
        ans=0;
        for(int i=1; i<=n; i++) {
            scanf("%d",&ar[i]);
        }
        sort(ar+1,ar+n+1);
        if(n==1) {
            if(p<=ar[1]) {
                printf("1\n");
            } else {
                printf("0\n");
            }
        } else {
            int i;
            for(i=1; i<n; i++) {
                tem=p-ar[i];
                if(tem<0) {
                    break;
                }
                else if(tem==0){
                    ans++;
                    break;
                }
                else {
                    if(tem<ar[i+1]) {
                        ck=max(ar[i],ar[i+1]);
                        if(ck<=p) {
                            ans+=2;
                            break;
                        }
                    } else {
                        ans++;
                        p-=ar[i];
                    }
                }
            }
            if(i==n) {
                if(p>=ar[n])ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


/**Bismillahir Rahmanir Raheem
    Author:Refat(BSMRSTU)**/
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define pil pair<int,ll>
const int N = 1e5 + 5;
const int M = 1e9 + 7;
const double pi=2*acos(0.0);
const double esp=1e-9;
int Set(int N,int pos) {
    return N=N | (1<<pos);
}
int reset(int N,int pos) {
    return N= N & ~(1<<pos);
}
bool check(int N,int pos) {
    return (bool)(N & (1<<pos));
}
int ar[N];
int main() {
    /// freopen("input.txt","r",stdin);
    /// freopen("output.txt","w",stdout);
    int test,n,p,k,ans,tem,ck;
    scanf("%d",&test);
    for(int cs=1; cs<=test; cs++) {
        scanf("%d %d %d",&n,&p,&k);ans=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&ar[i]);
        }
        sort(ar+1,ar+n+1);
        if(n==1){
            if(p<=ar[1]){
                printf("1\n");
            }
            else{
                printf("0\n");
            }
        }
        else{
                int i;
            for(i=1;i<n;i++){
                tem=p-ar[i];
                if(tem<0){
                   break;
                }
                else{
                    if(tem<ar[i+1]){
                       ck=max(ar[i],ar[i+1]);
                       if(ck<=p){
                        ans+=2;
                        break;
                       }
                    }
                    else{
                        ans++;
                        p-=ar[i];
                    }
                }
            }
            if(i==n){
                if(p>=ar[n])ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


/**Bismillahir Rahmanir Raheem
    Author:Refat(BSMRSTU)**/
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define pil pair<int,ll>
const int N = 1e5 + 5;
const int M = 1e9 + 7;
const double pi=2*acos(0.0);
const double esp=1e-9;
int Set(int N,int pos) {
    return N=N | (1<<pos);
}
int reset(int N,int pos) {
    return N= N & ~(1<<pos);
}
bool check(int N,int pos) {
    return (bool)(N & (1<<pos));
}
int ar[N],p;

bool fun(int l,int t) {
    for(int i=l; i>=1;) {
        if(ar[i]<=t) {
            t-=ar[i];
            i-=2;
        } else {
            return false;
        }
    }
    return true;
}
int main() {
    /// freopen("input.txt","r",stdin);
    /// freopen("output.txt","w",stdout);
    int test,n,k,ans,tem,ck;
    scanf("%d",&test);
    for(int cs=1; cs<=test; cs++) {
        scanf("%d %d %d",&n,&p,&k);
        ans=0;
        for(int i=1; i<=n; i++) {
            scanf("%d",&ar[i]);
        }
        sort(ar+1,ar+n+1);
        int l=1,r=n;
        while(l<=r) {
            int mid=(l+r)>>1;
            if(fun(mid,p)) {
                l=mid+1;
                ans=mid;
            } else {
                r=mid-1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


/**Bismillahir Rahmanir Raheem
    Author:Refat(BSMRSTU)**/
#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define pil pair<int,ll>
const int N = 1e5 + 5;
const int M = 1e9 + 7;
const double pi=2*acos(0.0);
const double esp=1e-9;
int Set(int N,int pos) {
    return N=N | (1<<pos);
}
int reset(int N,int pos) {
    return N= N & ~(1<<pos);
}
bool check(int N,int pos) {
    return (bool)(N & (1<<pos));
}
int ar[N];
int main() {
    /// freopen("input.txt","r",stdin);
    /// freopen("output.txt","w",stdout);
    int test,n,p,k,ans,tem,ck;
    scanf("%d",&test);
    for(int cs=1; cs<=test; cs++) {
        scanf("%d %d %d",&n,&p,&k);
        ans=0;
        for(int i=1; i<=n; i++) {
            scanf("%d",&ar[i]);
        }
        sort(ar+1,ar+n+1);
        if(n==1) {
            if(p<=ar[1]) {
                printf("1\n");
            } else {
                printf("0\n");
            }
        } else {
            int i;
            for(i=1; i<n; ) {
                tem=p-ar[i];
                if(p<ar[i]) {
                    break;
                }
                else {
                    if(p>=ar[i+1]) {
                        p-=ar[i];
                        ans+=2;
                        i+=2;
                    } else {
                        ans++;
                        p-=ar[i];
                        ++i;
                    }
                }
            }
            if(i==n) {
                if(p>=ar[n])ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
   long long int t; cin >>t;
   for(long long int tc=1;tc<=t;tc++){
       long long int n,p,k; cin >> n >> p >>k;
       vector<long long int>v(n);
       for(long long int i=0;i<n;i++)cin>>v[i];
       sort(v.begin(),v.end());
       long long int ans=0;
       while(1){
           if(p<v[0])break;
           if(v.size()==1){
               if(v[0]<=p)ans++;break;
           }
           if(v.size()==2){
               if(v[1]<=p){
                   ans+=2;
               }
               else if(v[0]<=p)ans++;
               break;
           }
           if(v[0]+v[2]==p){
               p-=v[2];
               v.erase(v.begin()+1);v.erase(v.begin()+1);
               ans+=2;
           }
           else{
               if(v[1]<=p){
                   ans+=2;p-=v[1];
                   v.erase(v.begin());v.erase(v.begin());
               }
               else {
                   ans++;p-=v[0];v.erase(v.begin());
               }
           }
       }
       cout << ans << endl;
   }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
   long long int t; cin >>t;
   for(long long int tc=1;tc<=t;tc++){
       long long int n,p,k; cin >> n >> p >>k;
       vector<long long int>v(n);
       for(long long int i=0;i<n;i++)cin>>v[i];
       sort(v.begin(),v.end());
       long long int sum=0,prefix=0,ans=-1;
       for(long long int i=0;i<=k;i++){
           sum=prefix;
           long long int cnt=i;
           for(long long int j=i+k-1;j<n;j+=k){
               if(sum+v[j]<=p){
                   cnt+=k;sum+=v[j];
               }else break;
           }
           ans=max(ans,cnt);
           prefix+=v[i];
       }
       cout << ans << endl;
   }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
   long long int t; cin >>t;
   for(long long int tc=1;tc<=t;tc++){
       long long int n,p,k; cin >> n >> p >>k;
       vector<long long int>v(n);
       for(long long int i=0;i<n;i++)cin>>v[i];
       sort(v.begin(),v.end());
       long long int sum=0,prefix=0,ans=-1;
       for(long long int i=0;i<=k;i++){
           sum=prefix;
           if(sum>p)break;
           long long int cnt=i;
           for(long long int j=i+k-1;j<n;j+=k){
               if(sum+v[j]<=p){
                   cnt+=k;sum+=v[j];
               }else break;
           }
           ans=max(ans,cnt);
           prefix+=v[i];
       }
       cout << ans << endl;
   }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
   long long int t; cin >>t;
   for(long long int tc=1;tc<=t;tc++){
       long long int n,p,k; cin >> n >> p >>k;
       vector<long long int>v(n);
       for(long long int i=0;i<n;i++)cin>>v[i];
       sort(v.begin(),v.end());
       long long int ans=0;
       while(1){
           if(v.size()==0) break;
           //for(int i=0;i<v.size();i++) cout <<v[i]<<" "; cout << endl;
           if(p<v[0])break;
           if(v.size()==1){
               if(v[0]<=p)ans++;break;
           }
           if(v.size()==2){
               if(v[1]<=p){
                   ans+=2;
               }
               else if(v[0]<=p)ans++;
               break;
           }
           if(v[0]+v[2]==p){
               p-=v[2];//p-=v[0];
               v.erase(v.begin()+1);v.erase(v.begin()+1);//v.erase(v.begin());
               ans+=2;
           }
           else{
               if(v[1]<=p){
                   ans+=2;p-=v[1];
                   v.erase(v.begin());v.erase(v.begin());
               }
               else {
                   ans++;p-=v[0];v.erase(v.begin());
               }
           }
           //cout << ans << endl;
       }
       cout << ans << endl;
   }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
   long long int t; cin >>t;
   for(long long int tc=1;tc<=t;tc++){
       long long int n,p,k; cin >> n >> p >>k;
       vector<long long int>v(n);
       for(long long int i=0;i<n;i++)cin>>v[i];
       sort(v.begin(),v.end());
       long long int ans=0;
       while(1){
           if(v.size()==0) break;
           //for(int i=0;i<v.size();i++) cout <<v[i]<<" "; cout << endl;
           if(p<v[0])break;
           if(v.size()==1){
               if(v[0]<=p)ans++;break;
           }
           if(v.size()==2){
               if(v[1]<=p){
                   ans+=2;
               }
               else if(v[0]<=p)ans++;
               break;
           }
           if(v[0]+v[2]==p){
               p-=v[2];p-=v[0];
               v.erase(v.begin());v.erase(v.begin());v.erase(v.begin());
               ans+=3;
           }
           else{
               if(v[1]<=p){
                   ans+=2;p-=v[1];
                   v.erase(v.begin());v.erase(v.begin());
               }
               else {
                   ans++;p-=v[0];v.erase(v.begin());
               }
           }
       }
       cout << ans << endl;
   }
}


#include <bits/stdc++.h>

using namespace std;

int main() {
   long long int t; cin >>t;
   for(long long int tc=1;tc<=t;tc++){
       long long int n,p,k; cin >> n >> p >>k;
       vector<long long int>v(n);
       for(long long int i=0;i<n;i++)cin>>v[i];
       sort(v.begin(),v.end());
       long long int ans=0;
       while(1){
           if(p<v[0])break;
           if(v.size()==1){
               if(v[0]<=p)ans++;break;
           }
           if(v.size()==2){
               if(v[1]<=p){
                   ans+=2;
               }
               else if(v[0]<=p)ans++;
               break;
           }
           if(v[0]+v[2]==p){
               p-=v[2];p-=v[0];
               v.erase(v.begin());v.erase(v.begin());v.erase(v.begin());
               ans+=3;
           }
           else{
               if(v[1]<=p){
                   ans+=2;p-=v[1];
                   v.erase(v.begin());v.erase(v.begin());
               }
               else {
                   ans++;p-=v[0];v.erase(v.begin());
               }
           }
       }
       cout << ans << endl;
   }
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, p, k, arr[N];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%d",&arr[i]);
        int ans =0;
        sort(arr, arr+n);
        int i = 0;
        multiset<int> bought;
        for(int i = 0;i<n;i++) {
            if(p >= arr[i]) {
                ans++;
                p -= arr[i];
                bought.insert(arr[i]*-1);
            } else if(bought.size() >= k-1) {
                int sum = 0;
                int rem = k-1;
                while(rem--) {
                    int curr = *bought.begin();
                    sum += curr*-1;
                    bought.erase(bought.begin());
                }
                if(sum + p >= arr[i]) {
                    p += sum - arr[i];
                    ans++;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;

int main() {
    int t;
    scanf("%d",&t);
    for(int tt = 1;tt<=t;tt++) {
        int n, p, k;
        scanf("%d%d%d",&n,&p,&k);
        int arr[n+1], sum[n+1] = {};
        for(int i =1;i<=n;i++)
            scanf("%d",&arr[i]);
        sort(arr, arr+n);
        for(int i = 1;i<=n;i++)
            sum[i] = arr[i] + sum[i-1];
        int ans = 0;
        for(int i = 0;i<k;i++) {
            if(sum[i] > p)
                break;
            int curr = i;
            int tot = sum[i];
            for(int j = i+k;j<=n;j+=k) {
                if(tot + arr[j] <= p)
                    curr+= k, tot += arr[j];
                else
                    break;
            }
            ans = max(ans, curr);
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, p, k, arr[N];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%d",&arr[i]);
        int ans =0;
        sort(arr, arr+n);
        int i = 0;
        multiset<int> bought;
        for(;i<n;i++)
            if(arr[i] <= p)
                p -= arr[i], ans++, bought.insert(arr[i]*-1);
            else
                break;
        for(;i<n;i++) {
            if(bought.size() >= k-1) {
                int sum = 0;
                int rem = k-1;
                while(rem--) {
                    int curr = *bought.begin();
                    sum += curr*-1;
                    bought.erase(bought.begin());
                }
                if(sum + p >= arr[i]) {
                    p += sum - arr[i];
                    ans++;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, k;
ll p, arr[N];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%lld%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%lld",&arr[i]);
        int ans =0;
        sort(arr, arr+n);
        multiset<int> bought;
        for(int i = 0;i<n;i++) {
            if(p >= arr[i]) {
                ans++;
                p -= arr[i];
                bought.insert(arr[i]*-1);
            } else {
                ll sum = 0;
                int rem = k-1;
                int mini = 1e9;
                while(rem && !bought.empty()) {
                    rem--;
                    int curr = *bought.begin();
                    sum += curr*-1;
                    mini = min(mini, curr*-1);
                    bought.erase(bought.begin());
                }
                if(sum + p >= arr[i+rem]) {
                    p += sum - arr[i+rem];
                    bought.insert(mini - arr[i+rem]);
                    for(int j = 0;j<k-1;j++)
                        bought.insert(0);
                    ans+= rem + 1;
                    if(rem)
                        break;
                } else {
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, k;
ll p, arr[N];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%lld%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%lld",&arr[i]);
        int ans =0;
        sort(arr, arr+n);
        multiset<int> bought;
        for(int i = 0;i<n;i++) {
            if(p >= arr[i]) {
                ans++;
                p -= arr[i];
                bought.insert(arr[i]*-1);
            } else {
                ll sum = 0;
                int rem = k-1;
                int mini = 1e9;
                while(rem && !bought.empty()) {
                    rem--;
                    int curr = *bought.begin();
                    sum += curr*-1;
                    mini = min(mini, curr*-1);
                    bought.erase(bought.begin());
                }
                if(bought.empty())
                    mini = 0;
                if((i+rem < n) && (sum + p >= arr[i+rem])) {
                    p += sum - arr[i+rem];
                    bought.insert(mini - arr[i+rem]);
                    for(int j = 0;j<k-1;j++)
                        bought.insert(0);
                    ans += rem + 1;
                    i += rem;
                } else {
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, k;
ll p, arr[N];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%lld%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%lld",&arr[i]);
        int ans =0;
        sort(arr, arr+n);
        multiset<int> bought;
        for(int i = 0;i<n;i++) {
            if(p >= arr[i]) {
                ans++;
                p -= arr[i];
                bought.insert(arr[i]*-1);
            } else {
                ll sum = 0;
                int rem = k-1;
                int mini = 1e9;
                while(rem && !bought.empty()) {
                    rem--;
                    int curr = *bought.begin();
                    sum += curr*-1;
                    mini = min(mini, curr*-1);
                    bought.erase(bought.begin());
                }
                if(rem)
                    mini = 0;
                if((i+rem < n) && (sum + p >= arr[i+rem])) {
                    p += sum - arr[i+rem];
                    bought.insert(mini - arr[i+rem]);
                    for(int j = 0;j<k-1;j++)
                        bought.insert(0);
                    ans += rem + 1;
                    i += rem;
                } else {
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, p, k, arr[N];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%d%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%d",&arr[i]);
        int ans =0;
        sort(arr, arr+n);
        int lst = -1;
        for(int i = k-1;i<n;i+=k) {
            if(arr[i] <= p) {
                ans += k;
                p-= arr[i];
                lst = i;
            } else {
                break;
            }
        }
        for(int i = lst+1;i<n;i++)
            if(arr[i] <= p)
                ans++, p -= arr[i];
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, k;
ll p, arr[N];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%lld%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%lld",&arr[i]);
        int ans =0;
        sort(arr, arr+n);
        multiset<int> bought;
        for(int i = 0;i<n;i++) {
            if(p >= arr[i]) {
                ans++;
                p -= arr[i];
                bought.insert(arr[i]*-1);
            } else {
                ll sum = 0;
                int rem = k-1;
                int mini = 1e9;
                while(rem && !bought.empty()) {
                    rem--;
                    int curr = *bought.begin();
                    sum += curr*-1;
                    mini = min(mini, curr*-1);
                    bought.erase(bought.begin());
                }
                if((i+rem < n) && (sum + p >= arr[i+rem])) {
                    p += sum - arr[i+rem];
                    bought.insert(mini - arr[i+rem]);
                    for(int j = 0;j<k-1;j++)
                        bought.insert(0);
                    ans += rem + 1;
                    i += rem;
                } else {
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, k;
ll p, arr[N];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%lld%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%lld",&arr[i]);
        int ans =0;
        sort(arr, arr+n);
        multiset<int> bought;
        for(int i = 0;i<n;i++) {
            if(p >= arr[i]) {
                ans++;
                p -= arr[i];
                bought.insert(arr[i]*-1);
            } else if(bought.size() >= k-1) {
                ll sum = 0;
                int rem = k-1;
                int mini = 1e9;
                while(rem--) {
                    int curr = *bought.begin();
                    sum += curr*-1;
                    mini = min(mini, curr*-1);
                    bought.erase(bought.begin());
                }
                if(sum + p >= arr[i]) {
                    p += sum - arr[i];
                    bought.insert(mini - arr[i]);
                    ans++;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, k;
ll p, arr[N];

int check(int mid) {
    ll curr = p;
    int i =0;
    for(;i<mid;i++)
        curr -= arr[i];
    int ans = mid;
    if(curr < 0)
        return -1;
    i += k-1;
    while(i < n) {
        if(curr >= arr[i])
            curr -= arr[i], ans += k;
        i+= k;
    }
    return ans;
}

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%lld%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%lld",&arr[i]);
        sort(arr, arr+n);
        multiset<int> bought;
        int l = 0, r = n;
        while(r - l >= 3) {
            int mid1 = l + (r-l)/3;
            int mid2 = r - (r-l)/3;
            int a = check(mid1);
            if( a == -1) {
                r = mid1-1;
                continue;
            }
            int b = check(mid2);
            if( b == -1) {
                r = mid2 - 1;
                continue;
            }
            if ( a <= b) {
                l = mid1+1;
            } else {
                r = mid2-1;
            }
        }
        int ans = 0;
        for(int i = l;i<=r;i++)
            ans = max(ans, check(i));
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, k;
ll p, arr[N];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%lld%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%lld",&arr[i]);
        int ans =0;
        sort(arr, arr+n);
        multiset<int> bought;
        for(int i = 0;i<n;i++) {
            if(p >= arr[i]) {
                ans++;
                p -= arr[i];
                bought.insert(arr[i]*-1);
            } else {
                ll sum = 0;
                int rem = k-1;
                int mini = 1e9;
                while(rem && !bought.empty()) {
                    rem--;
                    int curr = *bought.begin();
                    sum += curr*-1;
                    mini = min(mini, curr*-1);
                    bought.erase(bought.begin());
                }
                if((i+rem < n) && (sum + p >= arr[i+rem])) {
                    p += sum - arr[i+rem];
                    bought.insert(mini - arr[i+rem]);
                    for(int j = 0;j<k-1;j++)
                        bought.insert(0);
                    ans += rem + 1;
                    if(rem)
                        break;
                } else {
                    break;
                }
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, k;
ll p, arr[N];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%lld%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%lld",&arr[i]);
        int ans =0;
        sort(arr, arr+n);
        multiset<ll> bought;
        int l = 0, r =n;
        for(int i = 0;i<n;i++) {
            if(p >= arr[i]) {
                p -= arr[i];
                bought.insert(arr[i]*-1);
                ans = i+1;
            } else {
                if(bought.size() >= k-1) {
                    ll sum = 0;
                    int rem = k-1;
                    int mini = 1e9;
                    vector<int> tmp;
                    while(rem--) {
                        int curr = *bought.begin();
                        tmp.pb(curr);
                        sum += curr*-1;
                        mini = min(mini, curr*-1);
                        bought.erase(bought.begin());
                    }
                    if(sum + p >= arr[i]) {
                        p += sum - arr[i];
                        bought.insert(mini - arr[i]);
                        for(int j = 0;j<k-1;j++)
                            bought.insert(0);
                        ans = i+1;
                    } else {
                        bought.insert(0);
                        while(!tmp.empty())
                            bought.insert(tmp.back()), tmp.pop_back();
                    }
                } else {
                    bought.insert(0);
                }
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, k;
ll p, arr[N];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%lld%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%lld",&arr[i]);
        int ans =0;
        sort(arr, arr+n);
        multiset<int> bought;
        for(int i = 0;i<n;i++) {
            if(p >= arr[i]) {
                ans++;
                p -= arr[i];
                bought.insert(arr[i]*-1);
            } else if(bought.size() >= k-1) {
                ll sum = 0;
                int rem = k-1;
                int mini = 1e9;
                while(rem--) {
                    int curr = *bought.begin();
                    sum += curr*-1;
                    mini = min(mini, curr*-1);
                    bought.erase(bought.begin());
                }
                if(sum + p >= arr[i]) {
                    p += sum - arr[i];
                    bought.insert(mini - arr[i]);
                    for(int j = 0;j<k-2;j++)
                        bought.insert(0);
                    ans++;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;

int main() {
    int t;
    scanf("%d",&t);
    for(int tt = 1;tt<=t;tt++) {
        int n, p, k;
        scanf("%d%d%d",&n,&p,&k);
        int arr[n+1], sum[n+1] = {};
        for(int i =1;i<=n;i++)
            scanf("%d",&arr[i]);
        sort(arr+1, arr+n+1);
        for(int i = 1;i<=n;i++)
            sum[i] = arr[i] + sum[i-1];
        int ans = 0;
        for(int i = 0;i<k;i++) {
            if(sum[i] > p)
                break;
            int curr = i;
            int tot = sum[i];
            for(int j = i+k;j<=n;j+=k) {
                if(tot + arr[j] <= p)
                    curr+= k, tot += arr[j];
                else
                    break;
            }
            ans = max(ans, curr);
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include <bits/stdc++.h>
#define us unordered_set
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
#define MOD2 1000000009
#define BASE 29
#define BASE2 31
#define eps 1e-9
#define INF 1000000002
#define PI 3.14159265359

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<pii,pii> piiii;
const int N = 200002;
int n, m, k;
ll p, arr[N];

int main() {
    int t;
    scanf("%d",&t);
    while(t--) {
        scanf("%d%lld%d",&n,&p,&k);
        for(int i =0;i<n;i++)
            scanf("%lld",&arr[i]);
        int ans =0;
        sort(arr, arr+n);
        multiset<int> bought;
        for(int i = 0;i<n;i++) {
            if(p >= arr[i]) {
                ans++;
                p -= arr[i];
                bought.insert(arr[i]*-1);
            } else if(bought.size() >= k-1) {
                ll sum = 0;
                int rem = k-1;
                int mini = 1e9;
                while(rem--) {
                    int curr = *bought.begin();
                    sum += curr*-1;
                    mini = min(mini, curr*-1);
                    bought.erase(bought.begin());
                }
                if(sum + p >= arr[i]) {
                    p += sum - arr[i];
                    bought.insert(mini - arr[i]);
                    for(int j = 0;j<k-1;j++)
                        bought.insert(0);
                    ans++;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        printf("%d\n",ans);
    }

    return 0;
}


#include<bits/stdc++.h>
#define debug cout<<"debug"<<endl;
#define ll long long
const int maxn=2e5+10;
using namespace std;

int a[maxn];
ll gcd(ll a,ll b)
{
	return a==0?b:gcd(b,a%b);
}
ll pow(int a,int b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans*=a;
		b>>=1;a*=a;
	}
	return ans;
}
multiset<int>::iterator it;
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		ll n,p,k;
		int index=1,ans=0;
		cin>>n>>p>>k;
		k--;
		multiset<int> se,sed;
		se.clear();sed.clear();
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int i=1;i<=n;i++)
		{
			if(a[i]<=p)
			{
				ans++;
				p-=a[i];se.insert(a[i]);
			}
			else
			{
				sed.insert(a[i]);
			}
		}
		while(k && sed.size()!=0 && se.size()!=0)
		{
			it=se.end();it--;
			int maxp=*it;
			p+=maxp;
			it=sed.begin();
			while(p>=(*it) && sed.size()!=0 && se.size()!=0)
			{
				p-=(*it);sed.erase(sed.begin());se.insert((*it));
				it=sed.begin();ans++;
			}
		//	cout<<ans<<" while "<<k<<endl;
			k--;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
const int maxn=2e5+10;
using namespace std;

int a[maxn];
ll gcd(ll a,ll b)
{
	return a==0?b:gcd(b,a%b);
}
ll pow(int a,int b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans*=a;
		b>>=1;a*=a;
	}
	return ans;
}
multiset<int>::iterator it;
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		ll n,p,k;
		int index=1,ans=0;
		cin>>n>>p>>k;
		k--;
		multiset<int> se;
		se.clear();
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int i=1;i<=n;i++)
		{
			if(a[i]<=p)
			{
				ans++;
				p-=a[i];se.insert(a[i]);
			}
			else
			{
				if(k==0) break;
				if(se.size()==0) continue;
				it=se.end();
				it--;
				int maxp=*it;
				p+=maxp;i--;k--;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define debug cout<<"debug"<<endl;
#define ll long long
const int maxn=2e5+10;
using namespace std;

ll a[maxn];
ll gcd(ll a,ll b)
{
	return a==0?b:gcd(b,a%b);
}
ll pow(int a,int b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans*=a;
		b>>=1;a*=a;
	}
	return ans;
}
multiset<ll>::iterator it;
bool cmp(ll a,ll b)
{
	return a<b;
}
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		ll n,p,k;
		int index=1,ans=0;
		cin>>n>>p>>k;
		k--;
		multiset<ll> se,sed;
		se.clear();sed.clear();
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(a[i]<=p)
			{
				ans++;
				p-=a[i];se.insert(a[i]);
			}
			else
			{
				sed.insert(a[i]);
			}
		}
		while(k && sed.size()!=0 && se.size()!=0)
		{
			it=se.end();it--;
			ll maxp=*it;
			p+=maxp;
			it=sed.begin();
			while(p>=(*it) && sed.size()!=0 && se.size()!=0)
			{
				p-=(*it);sed.erase(sed.begin());se.insert((*it));
				/*cout<<"size "<<sed.size()<<endl;
				for(it=sed.begin();it!=sed.end();it++) cout<<*it<<" ";
				cout<<endl;*/
				it=sed.begin();ans++;
			}
		//	cout<<ans<<" while "<<k<<endl;
			k--;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define debug cout<<"debug"<<endl;
#define ll long long
const int maxn=2e5+10;
using namespace std;

ll a[maxn],sum[maxn];
ll gcd(ll a,ll b)
{
	return a==0?b:gcd(b,a%b);
}
ll pow(int a,int b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans*=a;
		b>>=1;a*=a;
	}
	return ans;
}
bool cmp(ll a,ll b)
{
	return a<b;
}
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		ll n,p,k;
		int index=1,ans=0;
		cin>>n>>p>>k;
		for(int i=1;i<=n;i++) cin>>a[i],sum[i]=0;
		sum[0]=0;
		sort(a+1,a+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			sum[i]=sum[i-1]+a[i];
			if(sum[i]<=p) ans=i;
		}
		for(int i=0;i<k;i++)
		{
			for(int j=i+k;j<=n;j+=k)
			{
				sum[i]+=a[j];
				if(sum[i]<p) ans=max(ans,j);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
const int maxn=2e5+10;
using namespace std;

int a[maxn];
ll gcd(ll a,ll b)
{
	return a==0?b:gcd(b,a%b);
}
ll pow(int a,int b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans*=a;
		b>>=1;a*=a;
	}
	return ans;
}
multiset<int>::iterator it;
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		ll n,p,k;
		int index=1,ans=0;
		cin>>n>>p>>k;
		k--;
		multiset<int> se;
		se.clear();
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int i=1;i<=n;i++)
		{
			if(a[i]<=p)
			{
				ans++;
				p-=a[i];se.insert(a[i]);
			}
			else
			{
				if(k==0) break;
				if(se.size()==0) break;
				it=se.end();
				it--;
				int maxp=*it;
				p+=maxp;i--;k--;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define debug cout<<"debug"<<endl;
#define ll long long
const int maxn=2e5+10;
using namespace std;

ll a[maxn],sum[maxn];
ll gcd(ll a,ll b)
{
	return a==0?b:gcd(b,a%b);
}
ll pow(int a,int b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans*=a;
		b>>=1;a*=a;
	}
	return ans;
}
bool cmp(ll a,ll b)
{
	return a<b;
}
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		ll n,p,k;
		int index=1,ans=0;
		cin>>n>>p>>k;
		for(int i=1;i<=n;i++) cin>>a[i],sum[i]=0;
		sum[0]=0;
		sort(a+1,a+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			sum[i]=sum[i-1]+a[i];
			if(sum[i]<=p) ans=i;
		}
		for(int i=0;i<k;i++)
		{
			for(int j=i+k;j<=n;j+=k)
			{
				sum[i]+=a[j];
				if(sum[i]<=p) ans=max(ans,j);
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
const int maxn=2e5+10;
using namespace std;

int a[maxn];
ll gcd(ll a,ll b)
{
	return a==0?b:gcd(b,a%b);
}
ll pow(int a,int b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans*=a;
		b>>=1;a*=a;
	}
	return ans;
}
multiset<int>::iterator it;
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		ll n,p,k;
		int index=1,ans=0;
		cin>>n>>p>>k;
		k--;
		multiset<int> se;
		for(int i=1;i<=n;i++) cin>>a[i];
		for(int i=1;i<=n;i++)
		{
			if(a[i]<=p)
			{
				ans++;
				p-=a[i];se.insert(a[i]);
			}
			else
			{
				if(k==0) break;
				if(se.size()==0) break;
				it=se.end();
				it--;
				int maxp=*it;
				p+=maxp;i--;k--;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define debug cout<<"debug"<<endl;
#define ll long long
const int maxn=2e5+10;
using namespace std;

ll a[maxn];
ll gcd(ll a,ll b)
{
	return a==0?b:gcd(b,a%b);
}
ll pow(int a,int b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans*=a;
		b>>=1;a*=a;
	}
	return ans;
}
multiset<ll>::iterator it,itd;
bool cmp(ll a,ll b)
{
	return a<b;
}
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		ll n,p,k;
		int index=1,ans=0;
		cin>>n>>p>>k;
		k--;
		multiset<ll> se,sed;
		se.clear();sed.clear();
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(a[i]<=p)
			{
				ans++;
				p-=a[i];se.insert(a[i]);
			}
			else
			{
				if(k==0) break;
				p+=a[i-1];sed.insert(a[i]);
				i--;k--;
			}
		}
		/*while(k && sed.size()!=0 && se.size()!=0)
		{
			it=se.end();it--;
			ll maxp=*it;
			p+=maxp;
			itd=sed.begin();
			while(p>=(*it) && sed.size()!=0 && se.size()!=0)
			{
				p-=(*it);sed.erase(sed.begin());se.insert((*it));
				itd=sed.begin();ans++;
			}
			k--;
		}*/
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define debug cout<<"debug"<<endl;
#define ll long long
const int maxn=2e5+10;
using namespace std;

int a[maxn];
ll gcd(ll a,ll b)
{
	return a==0?b:gcd(b,a%b);
}
ll pow(int a,int b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans*=a;
		b>>=1;a*=a;
	}
	return ans;
}
multiset<int>::iterator it;
int cmp(int a,int b)
{
	return a<b;
}
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		ll n,p,k;
		int index=1,ans=0;
		cin>>n>>p>>k;
		k--;
		multiset<int> se,sed;
		se.clear();sed.clear();
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
			if(a[i]<=p)
			{
				ans++;
				p-=a[i];se.insert(a[i]);
			}
			else
			{
				sed.insert(a[i]);
			}
		}
		while(k && sed.size()!=0 && se.size()!=0)
		{
			it=se.end();it--;
			int maxp=*it;
			p+=maxp;
			it=sed.begin();
			while(p>=(*it) && sed.size()!=0 && se.size()!=0)
			{
				p-=(*it);sed.erase(sed.begin());se.insert((*it));
				/*cout<<"size "<<sed.size()<<endl;
				for(it=sed.begin();it!=sed.end();it++) cout<<*it<<" ";
				cout<<endl;*/
				it=sed.begin();ans++;
			}
		//	cout<<ans<<" while "<<k<<endl;
			k--;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define test() int t; cin>>t; while(t--)
int main()
{
     int n,p,k,pref,ans;
     test()
     {
          pref=0,ans=0;
          scanf("%d%d%d",&n,&p,&k);
          vector<int> a(n);
          for (int i = 0; i < n; i++)
           cin >> a[i];
          sort(a.begin(), a.end());
          for (int i = 0; i <= 2; i++)
          {
            int sum = pref;
            if (sum > p) break;
            int cnt = i;
            for (int j=i+1; j<n; j+=2)
            {
                if (sum + a[j] <= p)
                {
                    cnt += 2;
                    sum += a[j];
                } else break;
            }
            pref += a[i];
            ans = max(ans, cnt);
          }
        cout <<ans<< "\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define test() int t; cin>>t; while(t--)
int main()
{
     ll n,p,k,count;
     test()
     {
          count=0;
          scanf("%ld%ld%ld",&n,&p,&k);
          ll a[n];
          for(int i=0;i<n;i++)
          scanf("%ld",&a[i]);
          sort(a,a+n);
          int i;
          for(i=1;i<n;i+=2)
          {
               if(p>=a[i])
               {
                 count+=2;
                 p-=a[i];
                 continue;
               }
               else if(p>=a[i-1])
               {
                    count+=1;
                    break;
               }
          }
          if(i==n)
          {
               if(p>=a[n-1])
               count+=1;
          }
          printf("%ld\n",count);
     }
     return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define test() int t; cin>>t; while(t--)
int main()
{
     ll n,p,k,count,i;
     ll t;
     cin>>t;
     while(t--)
     {
          count=0;
          cin>>n>>p>>k;
          ll a[n+2];
          for(ll j=0;j<n;j++)
          scanf("%ld",&a[j]);
          sort(a,a+n);
          for(i=1;i<n;i+=2)
          {
               if(p>=a[i])
               {
                 count+=2;
                 p-=a[i];
                 continue;
               }
               else if(p>=a[i-1])
               {
                    count+=1;
                    p-=a[i-1];
                    break;
               }
          }
          if(i==n)
          {
               if(p>=a[n-1])
               count+=1;
          }
          cout<<count<<"\n";
     }
     return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define test() int t; cin>>t; while(t--)
int main()
{
     int n,p,k,pref,ans;
     test()
     {
          pref=0,ans0;
          scanf("%d%d%d",&n,&p,&k);
          vector<int> a(n);
          for (int i = 0; i < n; i++)
           cin >> a[i];
          sort(a.begin(), a.end());
          for (int i = 0; i <= 2; i++)
          {
            int sum = pref;
            if (sum > p) break;
            int cnt = i;
            for (int j=i+1; j<n; j+=2)
            {
                if (sum + a[j] <= p)
                {
                    cnt += 2;
                    sum += a[j];
                } else break;
            }
            pref += a[i];
            ans = max(ans, cnt);
          }
        cout <<ans<< "\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define test() int t; cin>>t; while(t--)
int main()
{
     ll n,p,k,count;
     test()
     {
          count=0;
          scanf("%ld%ld%ld",&n,&p,&k);
          ll a[n];
          for(int j=0;j<n;j++)
          scanf("%ld",&a[j]);
          sort(a,a+n);
          int i;
          for(i=1;i<n;i+=2)
          {
               if(p>=a[i])
               {
                 count+=2;
                 p-=a[i];
                 continue;
               }
               else if(p>=a[i-1])
               {
                    count+=1;
                    p-=a[i-1];
                    break;
               }
               else continue;
          }
          if(i==n)
          {
               if(p>=a[n-1])
               count+=1;
          }
          printf("%ld\n",count);
     }
     return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define test() int t; cin>>t; while(t--)
int main()
{
     ll n,p,k,count;
     test()
     {
          count=0;
          scanf("%ld%ld%ld",&n,&p,&k);
          ll a[n];
          for(int i=0;i<n;i++)
          scanf("%ld",&a[i]);
          sort(a,a+n);
          for(int i=1;i<n;i+=2)
          {
               if(p>=a[i])
               {
                 count+=2;
                 p-=a[i];
                 continue;
               }
               else if(p>=a[i-1])
               {
                    count+=1;
                    break;
               }
          }
          if(i==n)
          {
               if(p>=a[n-1])
               count+=1;
          }
          printf("%ld\n",count);
     }
     return 0;
}


#include<iostream>
using namespace std;
const int maxn=2e5+10;
int arr[maxn];
int main()
{
    int test;
    cin>>test;
    while(test--){
        int n,p,k;
        cin>>n>>p>>k;
        for(int i=0;i<n;i++){
            cin>>arr[i];
        }
        sort(arr,arr+n);//default increasing
        int cost=0,ans=0;
        for(int i=0;i<k;i++){//枚举单买without using offer的goods 一定是<k 否则无意义(>=k 成组买一定更优解 单买对象一定是价格升序前k个 否则无意义
            int sum=cost;
            if(sum>p)   break;
            int cnt=i;
            for(int j=i+k-1;j<n;j+=k){//i==0时 单买的商品数是0 j=k-1 (0,k-1)...全部成组（k个/组）购买
                if(sum+arr[j]<=p){
                    cnt+=k;
                    sum+=arr[j];
                }
                else{//cannot afford more
                    break;
                    //这里即使p-sum>0 仍剩余钱时也不必向后单买商品计算 因为是从左往右遍历的 如果剩下钱可以单买当前后面的商品 一定可以单买更多前面的价格更低的商品 最优解一定在这次之前就被记录并计算了
                }
            }
            cost+=arr[i];//下一层i+1的计算是建立在单买价格最低的前i件商品的基础上的 花费是cost
            ans=max(ans,cnt);//不断更新记录最优解
        }
        cout<<ans<<endl;
        //O(k*(n%k)) 时间复杂度是线性的
    }
}


#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=2e5+10;
int arr[maxn];
int main()
{
    int test;
    cin>>test;
    while(test--){
        int n,p,k;
        cin>>n>>p>>k;
        for(int i=0;i<n;i++){
            cin>>arr[i];
        }
        sort(arr,arr+n);//default increasing
        int cost=0,ans=0;
        for(int i=0;i<k;i++){//枚举单买without using offer的goods 一定是<k 否则无意义(>=k 成组买一定更优解 单买对象一定是价格升序前k个 否则无意义
            int sum=cost;
            if(sum>p)   break;
            int cnt=i;
            for(int j=i+k-1;j<n;j+=k){//i==0时 单买的商品数是0 j=k-1 (0,k-1)...全部成组（k个/组）购买
                if(sum+arr[j]<=p){
                    cnt+=k;
                    sum+=arr[j];
                }
                else{//cannot afford more
                    break;
                    //这里即使p-sum>0 仍剩余钱时也不必向后单买商品计算 因为是从左往右遍历的 如果剩下钱可以单买当前后面的商品 一定可以单买更多前面的价格更低的商品 最优解一定在这次之前就被记录并计算了
                }
            }
            cost+=arr[i];//下一层i+1的计算是建立在单买价格最低的前i件商品的基础上的 花费是cost
            ans=max(ans,cnt);//不断更新记录最优解
        }
        cout<<ans<<endl;
        //O(k*(n%k)) 时间复杂度是线性的
    }
}


#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int arr[200010];
int main()
{
    int test;
    cin>>test;
    while(test--){
        int n,p,k;
        cin>>n>>p>>k;
        memset(arr,0,sizeof(arr));
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        sort(arr+1,arr+1+n);//increasing
        int ans=0;
        int i,sum=0;
        for(i=1;i<=n;i++){
            sum+=arr[i];
            if(sum+arr[i+2]>p){//cannot afford more
                if(i!=1)    ans=(i-1)+2;
                break;
            }
        }
        if(i==n+1&&sum<=p&&p>arr[1])//ans==0
            ans=n;//rich enough to buy all
        ans=min(ans,n);//2 10000 2 10000 10000 both use this offer and not use can buy all goods
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int arr[200010];
int main()
{
    int test;
    cin>>test;
    while(test--){
        int n,p,k;
        cin>>n>>p>>k;
        memset(arr,0,sizeof(arr));
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        sort(arr+1,arr+1+n);//increasing
        int sum=0,ans=0,i;
        for(i=1;i<=n;i++){
            sum+=arr[i];
            if(sum+arr[i+2]<=p){

            }
            else{
                sum=sum-arr[i]+arr[i+1];
                break;
            }
        }
        if(sum>0&&sum<=p)   ans=i+1;
        ans=min(ans,n);
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int arr[200010];
int main()
{
    int test;
    cin>>test;
    while(test--){
        int n,p,k;
        cin>>n>>p>>k;
        memset(arr,0,sizeof(arr));
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        sort(arr+1,arr+1+n);//increasing
        int sum=0,ans=0,i;
        for(i=1;i<=n;i++){
            sum+=arr[i];
            if(sum+arr[i+2]<=p){

            }
            else{
                sum=sum-arr[i]+arr[i+1];
                break;
            }
        }
        if(sum>0&&sum<=p)   ans=i+1;//using this offer and do not pay for arr[i+1] but the expensive one arr[i+2]
            ans=min(ans,n);//can buy all without using this offer
        if(ans==0&&p>arr[1])    ans=1;//can not buy the arr[2] but arr[1] so just by one without using the offer
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int arr[200010];
int main()
{
    int test;
    cin>>test;
    while(test--){
        int n,p,k;
        cin>>n>>p>>k;
        memset(arr,0,sizeof(arr));
        for(int i=1;i<=n;i++){
            cin>>arr[i];
        }
        sort(arr+1,arr+1+n);//increasing
        arr[n+1]=arr[n+2]=0x3f3f3f3f;
        int sum=0,ans=0,i;
        for(i=1;i<=n;i++){
            sum+=arr[i];
            if(sum+arr[i+2]<=p){

            }
            else{
                sum=sum-arr[i]+arr[i+1];
                break;
            }
        }
        if(sum>0&&sum<=p)   ans=i+1;//using this offer and do not pay for arr[i+1] but the expensive one arr[i+2]
            ans=min(ans,n);//can buy all without using this offer
        if(ans==0&&p>arr[1])    ans=1;//can not buy the arr[2] but arr[1] so just by one without using the offer
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int arr[200010];
int main()
{
    int test;
    cin>>test;
    while(test--){
        int n,p,k;
        cin>>n>>p>>k;
        for(int i=0;i<n;i++){
            cin>>arr[i];
        }
        sort(arr,arr+n);//increasing
        int ans=0;
        if(p>=arr[0]){
            for(int i=0;i<n;i++){
                if(p-arr[i]>=arr[i+2]){
                    p-=arr[i];
                    ans++;
                }
                else if(p-arr[i+1]>0){
                    //                p-=arr[i+1];
                    ans+=2;
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int arr[200010];
int main()
{
    int test;
    cin>>test;
    while(test--){
        int n,p,k;
        cin>>n>>p>>k;
        for(int i=0;i<n;i++){
            cin>>arr[i];
        }
        sort(arr,arr+n);//increasing
        int ans=0;
        if(p>=arr[0]){
            for(int i=0;i<n;i++){
                if(p-arr[i]>=arr[i+2]){
                    p-=arr[i];
                    ans++;
                }
                else if(p-arr[i+1]>0){
                    //                p-=arr[i+1];
                    ans+=2;
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    while(t--)
    {
        vector<int>v1;
        long long int i,n,p,k;cin>>n>>p>>k;
        for(i=0;i<n;i++)
        {
            long long int temp;cin>>temp;
            v1.push_back(temp);
        }
        sort(v1.begin(),v1.end());
        i = 0;
        int count = 0 , pc = 0 ;
        while(pc<p && count<n)
        {
            if(count>n)
            {
                break;
            }
            while(v1[i+1]==v1[i] && pc + v1[i]<=p)
                     {
                          i++;
                          count++;
                     }
            if(i+k>=n)
            {

                  while(pc<p && count<n)
                  {
                     while(v1[i+1]==v1[i] && pc + v1[i]<=p)
                     {
                          i++;
                          count++;
                     }
                    pc += v1[i];
                    i++;
                    if(pc<=p)
                    count++;
                  }
            }
            else if(pc+v1[i]+v1[i+k]<=p)
            {
                count += k+1;
                pc += v1[i]+v1[i+k];
                i = k+i+1;
            }
            else if(pc+v1[i+2*k-1]<=p)
            {

                count += k;
                pc += v1[i+k-1];
                i = i+k;
            }
            else
            {
                while(pc<p && count<n)
                {
                    pc += v1[i];
                    i++;
                    count++;
                }
            }
        }
        cout<<count<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define sorta(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<>())
#define endl '\n'
#define mp(a,b) make_pair(a,b)
#define nod(N) floor(log10(N)) + 1
#define f(p) p.first
#define s(p) p.second
#define lb(a) lower_bound(a)
#define ub(a) upper_bound(a)
#define fo(i,n) for(int i = 0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
#define foa(i,a,n) for(int i = a;i<=n;i++)
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define reverse(s) reverse(s.begin(),s.end())
#define lbv(v,a) lower_bound(v.begin(),v.end(),a)
#define gcd(a,b) __gcd(a,b)
#define dispaly(v) for(int i=0;i<v.size();i++)cout<<v[i]<<" ";
int main()
{
    fast;
    ll i,j;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,p,k;cin>>n>>p>>k;
        vector<ll>v1(n+1);
        fo1(i,n)cin>>v1[i];
        v1[0] = 0; sorta(v1);
        ll dp[n+1] = {0}; ll sum = 0;dp[0] = 0;

        for(i=1;i<=k-1;i++)
        {
            dp[i] = v1[i] + dp[i-1];
        }

        for(i;i<=n;i++)
        {
            j = i; ll temp = j;
            j = j/k; temp = temp - j*k;
            while(j>0)
            {
                dp[i] += v1[j*k+temp]; j--;
            }
            dp[i] += dp[temp];

        }
        i = n;
        while(i>=1)
        {
            if(dp[i]!=0 && dp[i]<=p){break;}
            i--;
        }
        // fo1(i,n)cout<<dp[i]<<" ";
        cout<<i<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
#define sorta(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<>())
#define endl '\n'
#define mp(a,b) make_pair(a,b)
#define nod(N) floor(log10(N)) + 1
#define f(p) p.first
#define s(p) p.second
#define lb(a) lower_bound(a)
#define ub(a) upper_bound(a)
#define fo(i,n) for(int i = 0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
#define foa(i,a,n) for(int i = a;i<=n;i++)
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define reverse(s) reverse(s.begin(),s.end())
#define lbv(v,a) lower_bound(v.begin(),v.end(),a)
#define gcd(a,b) __gcd(a,b)
#define dispaly(v) for(int i=0;i<v.size();i++)cout<<v[i]<<" ";
int main()
{
    fast;
    ll i,j;
    ll t = 1;
    cin>>t;
    while(t--)
    {
        ll n,p,k;cin>>n>>p>>k;
        vector<ll>v1(n+1);
        fo1(i,n)cin>>v1[i];
        v1[0] = 0; sorta(v1);
        ll dp[n+1] = {0}; ll sum = 0;dp[0] = 0;

        for(i=1;i<=k-1;i++)
        {
            dp[i] = v1[i] + dp[i-1];
        }

        for(i;i<=n;i++)
        {
            dp[i] = v1[i] + dp[i-k];
        }
        i = n;
        while(i>=1)
        {
            if(dp[i]!=0 && dp[i]<=p){break;}
            i--;
        }
        // fo1(i,n)cout<<dp[i]<<" ";
        cout<<i<<endl;

    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    while(t--)
    {
        vector<int>v1;
        long long int i,n,p,k;cin>>n>>p>>k;
        for(i=0;i<n;i++)
        {
            long long int temp;cin>>temp;
            v1.push_back(temp);
        }
        sort(v1.begin(),v1.end());
        i = 0;
        int count = 0 , pc = 0 ;
        while(pc<p && count<=n)
        {
              if(i>=n)
              {
                  break;
              }
              else if(i+1>=n)
              {
                  if(pc+v1[i]<=p)
                  {
                      count++;
                      i++;
                  }
                  break;
              }
              else if(i+2>=n)
              {

                  if(pc+v1[i+1]<=p)
                  {
                      count+=2;
                      pc += v1[i+1];
                      i += 2;
                  }
                  else if(pc+v1[i]<=p)
                  {
                      pc += v1[i];
                      count++;
                      i++;
                  }
                  break;
              }
              if(v1[i]==v1[i+1] && pc+v1[i]<=p)
              {
                  i++;
                  count++;
              }
              if(pc+v1[i]+v1[i+2]<=p)
              {
                count += 3;
                pc += v1[i]+v1[i+2];
                i = 3+i;
              }
              else if(pc+v1[i+1]<=p)
              {
                  count += 2;
                  pc += v1[i+1];
                  i++;
              }
              else if(pc + v1[i]<=p)
              {
                  count++;
                  pc += v1[i];
                  i++;
              }
              else
              {
                  break;
              }

        }
        cout<<count<<endl;
    }
}
// pc += v1[i+1];


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define pb push_back
#define ll long long int
#define db double
#define SORTA(v) sort(v.begin(),v.end())
#define SORTD(v) sort(v.begin(),v.end(),greater<>())
#define endl '\n'
#define nod(N) floor(log10(N)) + 1
#define LB(a) lower_bound(a)
#define UB(a) upper_bound(a)
#define F first
#define S second
#define FOR(i,n) for(long long int i = 0;i<n;i++)
#define FOR1(i,n) for(long long int i=1;i<=n;i++)
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define REVERSE(s) reverse(s.begin(),s.end())
#define LBV(v,a) lower_bound(v.begin(),v.end(),a)
#define UBV(v,a) upper_bound(v.begin(),v.end(),a)
#define GCD(a,b) __gcd(a,b)
#define MAX pow((ll)2,63) - 999;
#define ITX(it,x1) for(auto it = x1.begin();it!=x1.end();it++)
#define I insert
#define stll(x) stoll(x, nullptr, 10);
#define VFIND(v1,val) find(v1.begin() , v1.end() , val )
#define VFILL(v1 , val)   std::fill(v1.begin(), v1.end(), val);
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define all(x) x.begin() , x.end()

// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

ll mxm() {return LLONG_MIN;}
template<typename... Args>
ll mxm(ll a, Args... args) { return max(a,mxm(args...)); }
ll mnm() {return LLONG_MAX;}
template<typename... Args>
ll mnm(ll a, Args... args) { return min(a,mnm(args...)); }
ll cl(ll a, ll b){if(a%b == 0)return a/b;else return a/b + 1;}
const int M = 1e9 + 7;

long long int powxy(long long int x, long long int y)
{
    if (y == 0) return 1;
    if (y%2 == 1) return (x*powxy(x, y-1))%M;
    long long int t = powxy(x, y/2);
    return (t*t)%M;
}
void DPM(map<ll,ll>m1)
{
    for(auto it = m1.begin();it!=m1.end();it++)
    {
        cout<<it->first<<" "<<it->second<<endl;
    }
    cout<<endl;
}
void DPV(vector<ll>v)
{
    FOR(i,v.size())cout<<v[i]<<" ";cout<<endl;
}
void DPS(set<ll>s1)
{
    for(auto it =s1.begin();it!=s1.end();it++)cout<<*it<<" ";cout<<endl;
}
void DPA(ll arr[],ll n)
{
    FOR(i,n)cout<<arr[i]<<" ";cout<<endl;
}

ll dx[4] = { 0,0,1,-1 };
ll dy[4] = { 1,-1,0,0 };
string pos = "RLDU";

int main()
{
    FAST;
    ll i ,j , T = 1;
    cin>>T;
    while(T--)
    {
        i=0;j=0;

        ll n,p,k; cin>>n>>p>>k;

        vector<ll>v1(n+1);
        FOR1(i,n)cin>>v1[i];
        SORTA(v1);

        ll dp[n+1] ;

        FOR(i,n+1)dp[i]=0;

        ll cc = 0 ; ll ans = 0;

        i=1;

        while( i<=n )
        {
            if(i>=k)
            {
                dp[i] = v1[i] + dp[i-k];
            }

            else dp[i] = v1[i] + dp[i-1];
            i++;
        }

        FOR1(i,n)
        {
            if(dp[i] <= p)ans = i;
        }

        cout<<ans<<endl;

    }
}


#include <cstdio>
#include <map>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#define fr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,b) fr(i,0,b)
#define F first
#define S second
using namespace std;
map<string,int> mapa;

int c(int a){
	int i=1, v=0;
	while(a){
		if(a%10){
			v+=(a%10)*i;
			i*=10;
		}
		a/=10;
	}
	return v;
}

int vs[100100];

int main(){
	double r=0.0;
	int n;
	scanf("%d",&n);
	vs[0]=0;
	rep(i,n){
		scanf("%d",vs+i+1);
	}
	n++;
	sort(vs,vs+n);
	for(int i=(n&1); i<n;i+=2){
		//printf(" %d %d\n",vs[i+1],vs[i]);
		r += acos(-1.0)*(vs[i+1]*vs[i+1]-vs[i]*vs[i]);
	}
	printf("%.10lf\n",r);
	return 0;
}

/* Compile Error:
 22222 */
/* Compile Error:
 22222 */


#include <bits/stdc++.h>
#define rep(x,n) for(int x=0;x<int(n);++x)
#define print(x) cout<<x<<endl
#define dbg(x) cerr<<#x<<" = "<<x<<endl
#define pv(x,y) {for(typeof(y) z=(x);z!=(y);z++)cerr<<*z<<" ";cerr<<endl;}
#define _ <<" , "<<
#define mp make_pair
#define x first
#define y second
using namespace std;
typedef pair<int,int> pt;

int main() {
  if(true) cin.sync_with_stdio(false);
  int n;
  cin>>n;
  vector<long long> r(n+1);
  rep(i,n) cin>>r[i+1];
  sort(r.begin()+1,r.end());
  //
  //rep(i,n) r[i+1] += r[i];
  double z=0;
  rep(i,n) if(i%2==0) z+=(r[i+1]*r[i+1]-r[i]*r[i]);
  printf("%.6lf\n",z*M_PI);
  return 0;
}


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<cmath>
#include<queue>

using namespace std;

#define fr(a,b,c) for(int a = b; a < c; a++)
#define rep(a,b) fr(a,0,b)
#define ln puts("")

typedef long long ll;

int n;
double r[1111];

bool read(){
	if(scanf("%d", &n) == EOF) return false;

	rep(i,n) scanf("%lf", &r[i]);

	return true;
}

double area(double r){
	return acos(-1)*r*r;
}

void process(){
	sort(r, r+n);
	reverse(r, r+n);

	double res = area(r[0]);

	fr(i,1,n){
		if(i&1) res -= area(r[i]);
		else res += area(r[i]);
	}

	printf("%lf\n", res);
}

int main(){
	while(read()) process();

	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int a[201],i,n;
double ans;
int main(){
  scanf("%d",&n);
  for(i=1;i<=n;++i)
    scanf("%d",&a[i]);
  sort(a+1,a+n+1);
  if(n%2==0){
    for(i=n;i>1;--i)
      if(i%2!=0)
        ans+=M_PI*(double)(a[i]*a[i]);
      else
        ans-=M_PI*(double)(a[i]*a[i]);
  }
  else{
    for(i=n;i>=1;--i)
      if(i%2!=0)
        ans+=M_PI*(double)(a[i]*a[i]);
      else
        ans-=M_PI*(double)(a[i]*a[i]);
  }
  printf("%.15lf",ans);
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int a[101],i,n;
double ans;
int main(){
  scanf("%d",&n);
  for(i=1;i<=n;++i)
    scanf("%d",&a[i]);
  sort(a+1,a+n+1);
  if(n%2==0){
    for(i=n-1;i>=1;--i)
      if(i%2!=0)
        ans+=M_PI*(a[i]*a[i]);
      else
        ans-=M_PI*(a[i]*a[i]);
  }
  else{
    for(i=n;i>=1;--i)
      if(i%2!=0)
        ans+=M_PI*(a[i]*a[i]);
      else
        ans-=M_PI*(a[i]*a[i]);
  }
  printf("%.15lf",ans-0.00001);
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int a[201],i,n;
double ans;
int main(){
  scanf("%d",&n);
  for(i=1;i<=n;++i)
    scanf("%d",&a[i]);
  sort(a+1,a+n+1);
  if(n%2==0){
    for(i=n-1;i>=1;--i)
      if(i%2!=0)
        ans+=M_PI*(double)(a[i]*a[i]);
      else
        ans-=M_PI*(double)(a[i]*a[i]);
  }
  else{
    for(i=n;i>=1;--i)
      if(i%2!=0)
        ans+=M_PI*(double)(a[i]*a[i]);
      else
        ans-=M_PI*(double)(a[i]*a[i]);
  }
  printf("%.20lf",ans);
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int a[101],i,n;
double ans;
int main(){
  scanf("%d",&n);
  for(i=1;i<=n;++i)
    scanf("%d",&a[i]);
  sort(a+1,a+n+1);
  if(n%2==0){
    for(i=n-1;i>=1;--i)
      if(i%2!=0)
        ans+=M_PI*(a[i]*a[i]);
      else
        ans-=M_PI*(a[i]*a[i]);
  }
  else{
    for(i=n;i>=1;--i)
      if(i%2!=0)
        ans+=M_PI*(a[i]*a[i]);
      else
        ans-=M_PI*(a[i]*a[i]);
  }
  printf("%.15lf",ans);
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int a[201],i,n;
double ans=0.0;
int main(){
  scanf("%d",&n);
  for(i=1;i<=n;++i)
    scanf("%d",&a[i]);
  sort(a+1,a+n+1);
  if(n%2==0){
    for(i=n;i>=1;--i)
      if(i%2==0)
        ans+=M_PI*(double)(a[i]*a[i]);
      else
        ans-=M_PI*(double)(a[i]*a[i]);
  }
  else{
    for(i=n;i>=1;--i)
      if(i%2!=0)
        ans+=M_PI*(double)(a[i]*a[i]);
      else
        ans-=M_PI*(double)(a[i]*a[i]);
  }
  printf("%.15lf",ans);
}


#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int a[101],i,n;
double ans;
int main(){
  scanf("%d",&n);
  for(i=1;i<=n;++i)
    scanf("%d",&a[i]);
  sort(a+1,a+n+1);
  if(n%2==0){
    for(i=n-1;i>=1;--i)
      if(i%2!=0)
        ans+=M_PI*(a[i]*a[i]);
      else
        ans-=M_PI*(a[i]*a[i]);
  }
  else{
    for(i=n;i>=1;--i)
      if(i%2!=0)
        ans+=M_PI*(a[i]*a[i]);
      else
        ans-=M_PI*(a[i]*a[i]);
  }
  printf("%.15lf",ans);
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-4
#define VI vector<int>
#define VS vector<string>
#define LL long long
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)

using namespace std;
bool is_palindrome(const string &s, int start, int end) {
 int sz = end - start + 1;
if (sz % 2 == 0) {
  int middle = start + sz/2;
    int i;
  for (i = 0; middle - 1 - i >= 0 && middle+i <= end && s[middle-1-i] == s[middle+i]; i++);
   if (middle - i - 1 < 0 || middle+i > end)
    return true;
 } else {
   int middle = start + sz/2;
int i;
 for (i = 1; middle - i >= 0 && middle+i <= end && s[middle-i]==s[middle+i]; i++);
   if (middle - i < 0 || middle+i > end)
return true;
 }
return false;
}
//Polya-Burnside theory : (n^6+3n^4+12n^3+8n^2)/24
int euclidd,euclidx,euclidy;
void extendedeuclid(int a,int b)
{
	if(b==0)
	{
		euclidd=a;
		euclidx=1;
		euclidy=0;
		return ;
	}
	extendedeuclid(b,a%b);
	int d1,x1,y1;
	d1=euclidd;
	x1=euclidx;
	y1=euclidy;
	euclidx=y1;
	euclidy=x1-((a/b)*y1);
}
double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;
}
double dist_2point(double x1,double y1,double x2,double y2)
{
    double d;
    d=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
    return d;
}
int binary_search(int a[],int n,int l,int u)
{
	int mid;
  if(l>u)
	  return 0;
  mid=floor(l+u)/2;
  if(a[mid]==n)
	  return mid;
  else if(a[mid]>n)
	  binary_search(a,n,l,mid-1);
	  else
	  binary_search(a,n,mid+1,u);
}
int gcd(int a,int b)
{
while(b>0)
{
a=a%b;
a=a^b;
b=b^a;
a=a^b;
}
return a;
}
long lcm(long a,long b)
{
    long x=(a*b)/gcd(a,b);
    return x;
}
long is_prime(long n)
{
	long ii;
if (n == 1)
 return 0;
if (n == 2)
 return 1;
if (n%2 == 0)
 return 0;
for (ii=3;ii*ii<=n;ii=ii+2)
     if (n%ii == 0)
           return 0;
return 1;
}

int main()
{
ios_base::sync_with_stdio(false);
int n,i;
double r[1001],a,s=0.0,prv;
scanf("%d",&n);
for(i=0;i<n;i++)
{
       scanf("%lf",&r[i]);
}
sort(r,r+n);
for(i=n-1;i>=0;i--)
{

  a=2*acos(0.0)*r[i]*r[i];
 // printf("%lf ",a);
  if(i%2==0)
  {
    s+=a;
   // prv=a;
  }
  else
  {
     // prv-=a;
      s-=a;
  }
}
printf("%.15lf\n",s);
return 0;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-4
#define VI vector<int>
#define VS vector<string>
#define LL long long
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)

using namespace std;
bool is_palindrome(const string &s, int start, int end) {
 int sz = end - start + 1;
if (sz % 2 == 0) {
  int middle = start + sz/2;
    int i;
  for (i = 0; middle - 1 - i >= 0 && middle+i <= end && s[middle-1-i] == s[middle+i]; i++);
   if (middle - i - 1 < 0 || middle+i > end)
    return true;
 } else {
   int middle = start + sz/2;
int i;
 for (i = 1; middle - i >= 0 && middle+i <= end && s[middle-i]==s[middle+i]; i++);
   if (middle - i < 0 || middle+i > end)
return true;
 }
return false;
}
//Polya-Burnside theory : (n^6+3n^4+12n^3+8n^2)/24
int euclidd,euclidx,euclidy;
void extendedeuclid(int a,int b)
{
	if(b==0)
	{
		euclidd=a;
		euclidx=1;
		euclidy=0;
		return ;
	}
	extendedeuclid(b,a%b);
	int d1,x1,y1;
	d1=euclidd;
	x1=euclidx;
	y1=euclidy;
	euclidx=y1;
	euclidy=x1-((a/b)*y1);
}
double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;
}
double dist_2point(double x1,double y1,double x2,double y2)
{
    double d;
    d=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
    return d;
}
int binary_search(int a[],int n,int l,int u)
{
	int mid;
  if(l>u)
	  return 0;
  mid=floor(l+u)/2;
  if(a[mid]==n)
	  return mid;
  else if(a[mid]>n)
	  binary_search(a,n,l,mid-1);
	  else
	  binary_search(a,n,mid+1,u);
}
int gcd(int a,int b)
{
while(b>0)
{
a=a%b;
a=a^b;
b=b^a;
a=a^b;
}
return a;
}
long lcm(long a,long b)
{
    long x=(a*b)/gcd(a,b);
    return x;
}
long is_prime(long n)
{
	long ii;
if (n == 1)
 return 0;
if (n == 2)
 return 1;
if (n%2 == 0)
 return 0;
for (ii=3;ii*ii<=n;ii=ii+2)
     if (n%ii == 0)
           return 0;
return 1;
}

int main()
{
ios_base::sync_with_stdio(false);
long int n,i;
long int r[1001],s=0,a;
scanf("%ld",&n);
for(i=0;i<n;i++)
{
       scanf("%ld",&r[i]);
}
sort(r,r+n);
for(i=n-1;i>=0;i--)
{

  //a=2*acos(0.0)*r[i]*r[i];
 // printf("%lf ",a);
 a=r[i]*r[i];
  if(i%2==0)
  {
    s+=a;
   // prv=a;
  }
  else
  {
     // prv-=a;
      s-=a;
  }
}
printf("%.15lf\n",s*PI);
return 0;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-15
#define VI vector<int>
#define VS vector<string>
#define LL long long
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)

using namespace std;
bool is_palindrome(const string &s, int start, int end) {
 int sz = end - start + 1;
if (sz % 2 == 0) {
  int middle = start + sz/2;
    int i;
  for (i = 0; middle - 1 - i >= 0 && middle+i <= end && s[middle-1-i] == s[middle+i]; i++);
   if (middle - i - 1 < 0 || middle+i > end)
    return true;
 } else {
   int middle = start + sz/2;
int i;
 for (i = 1; middle - i >= 0 && middle+i <= end && s[middle-i]==s[middle+i]; i++);
   if (middle - i < 0 || middle+i > end)
return true;
 }
return false;
}
//Polya-Burnside theory : (n^6+3n^4+12n^3+8n^2)/24
int euclidd,euclidx,euclidy;
void extendedeuclid(int a,int b)
{
    if(b==0)
    {
        euclidd=a;
        euclidx=1;
        euclidy=0;
        return ;
    }
    extendedeuclid(b,a%b);
    int d1,x1,y1;
    d1=euclidd;
    x1=euclidx;
    y1=euclidy;
    euclidx=y1;
    euclidy=x1-((a/b)*y1);
}
double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;
}
double dist_2point(double x1,double y1,double x2,double y2)
{
    double d;
    d=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
    return d;
}
int binary_search(int a[],int n,int l,int u)
{
    int mid;
  if(l>u)
      return 0;
  mid=floor(l+u)/2;
  if(a[mid]==n)
      return mid;
  else if(a[mid]>n)
      binary_search(a,n,l,mid-1);
      else
      binary_search(a,n,mid+1,u);
}
int gcd(int a,int b)
{
while(b>0)
{
a=a%b;
a=a^b;
b=b^a;
a=a^b;
}
return a;
}
long lcm(long a,long b)
{
    long x=(a*b)/gcd(a,b);
    return x;
}
long is_prime(long n)
{
    long ii;
if (n == 1)
 return 0;
if (n == 2)
 return 1;
if (n%2 == 0)
 return 0;
for (ii=3;ii*ii<=n;ii=ii+2)
     if (n%ii == 0)
           return 0;
return 1;
}

int main()
{
ios_base::sync_with_stdio(false);
long int n,i;
long long int r[1001],s=0,a;
scanf("%ld",&n);
for(i=0;i<n;i++)
{
       scanf("%ld",&r[i]);
}
sort(r,r+n);
int m=0;
for(i=n-1;i>=0;i--,m++)
{

  //a=2*acos(0.0)*r[i]*r[i];

 a=r[i]*r[i];

  if(m%2==0)
  {
    s+=a;
    //  printf("%ld p\n",a);
   // prv=a;
  }
  else
  {
     // prv-=a;
      // printf("%ld n\n",a);
      s-=a;
  }
}
double d=s;
printf("%.15lf\n",d*2*acos(0.0));
return 0;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-4
#define VI vector<int>
#define VS vector<string>
#define LL long long
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)

using namespace std;
bool is_palindrome(const string &s, int start, int end) {
 int sz = end - start + 1;
if (sz % 2 == 0) {
  int middle = start + sz/2;
    int i;
  for (i = 0; middle - 1 - i >= 0 && middle+i <= end && s[middle-1-i] == s[middle+i]; i++);
   if (middle - i - 1 < 0 || middle+i > end)
    return true;
 } else {
   int middle = start + sz/2;
int i;
 for (i = 1; middle - i >= 0 && middle+i <= end && s[middle-i]==s[middle+i]; i++);
   if (middle - i < 0 || middle+i > end)
return true;
 }
return false;
}
//Polya-Burnside theory : (n^6+3n^4+12n^3+8n^2)/24
int euclidd,euclidx,euclidy;
void extendedeuclid(int a,int b)
{
	if(b==0)
	{
		euclidd=a;
		euclidx=1;
		euclidy=0;
		return ;
	}
	extendedeuclid(b,a%b);
	int d1,x1,y1;
	d1=euclidd;
	x1=euclidx;
	y1=euclidy;
	euclidx=y1;
	euclidy=x1-((a/b)*y1);
}
double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;
}
double dist_2point(double x1,double y1,double x2,double y2)
{
    double d;
    d=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
    return d;
}
int binary_search(int a[],int n,int l,int u)
{
	int mid;
  if(l>u)
	  return 0;
  mid=floor(l+u)/2;
  if(a[mid]==n)
	  return mid;
  else if(a[mid]>n)
	  binary_search(a,n,l,mid-1);
	  else
	  binary_search(a,n,mid+1,u);
}
int gcd(int a,int b)
{
while(b>0)
{
a=a%b;
a=a^b;
b=b^a;
a=a^b;
}
return a;
}
long lcm(long a,long b)
{
    long x=(a*b)/gcd(a,b);
    return x;
}
long is_prime(long n)
{
	long ii;
if (n == 1)
 return 0;
if (n == 2)
 return 1;
if (n%2 == 0)
 return 0;
for (ii=3;ii*ii<=n;ii=ii+2)
     if (n%ii == 0)
           return 0;
return 1;
}

int main()
{
ios_base::sync_with_stdio(false);
int n,i;
double a,prv;
int r[1001],s=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
       scanf("%d",&r[i]);
}
sort(r,r+n);
for(i=n-1;i>=0;i--)
{

  //a=2*acos(0.0)*r[i]*r[i];
 // printf("%lf ",a);
 a=r[i]*r[i];
  if(i%2==0)
  {
    s+=a;
   // prv=a;
  }
  else
  {
     // prv-=a;
      s-=a;
  }
}
printf("%.15lf\n",s*PI);
return 0;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-15
#define VI vector<int>
#define VS vector<string>
#define LL long long
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)

using namespace std;
bool is_palindrome(const string &s, int start, int end) {
 int sz = end - start + 1;
if (sz % 2 == 0) {
  int middle = start + sz/2;
    int i;
  for (i = 0; middle - 1 - i >= 0 && middle+i <= end && s[middle-1-i] == s[middle+i]; i++);
   if (middle - i - 1 < 0 || middle+i > end)
    return true;
 } else {
   int middle = start + sz/2;
int i;
 for (i = 1; middle - i >= 0 && middle+i <= end && s[middle-i]==s[middle+i]; i++);
   if (middle - i < 0 || middle+i > end)
return true;
 }
return false;
}
//Polya-Burnside theory : (n^6+3n^4+12n^3+8n^2)/24
int euclidd,euclidx,euclidy;
void extendedeuclid(int a,int b)
{
    if(b==0)
    {
        euclidd=a;
        euclidx=1;
        euclidy=0;
        return ;
    }
    extendedeuclid(b,a%b);
    int d1,x1,y1;
    d1=euclidd;
    x1=euclidx;
    y1=euclidy;
    euclidx=y1;
    euclidy=x1-((a/b)*y1);
}
double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;
}
double dist_2point(double x1,double y1,double x2,double y2)
{
    double d;
    d=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
    return d;
}
int binary_search(int a[],int n,int l,int u)
{
    int mid;
  if(l>u)
      return 0;
  mid=floor(l+u)/2;
  if(a[mid]==n)
      return mid;
  else if(a[mid]>n)
      binary_search(a,n,l,mid-1);
      else
      binary_search(a,n,mid+1,u);
}
int gcd(int a,int b)
{
while(b>0)
{
a=a%b;
a=a^b;
b=b^a;
a=a^b;
}
return a;
}
long lcm(long a,long b)
{
    long x=(a*b)/gcd(a,b);
    return x;
}
long is_prime(long n)
{
    long ii;
if (n == 1)
 return 0;
if (n == 2)
 return 1;
if (n%2 == 0)
 return 0;
for (ii=3;ii*ii<=n;ii=ii+2)
     if (n%ii == 0)
           return 0;
return 1;
}

int main()
{
ios_base::sync_with_stdio(false);
long int n,i;
long long int r[1001],s=0,a;
scanf("%ld",&n);
for(i=0;i<n;i++)
{
       scanf("%ld",&r[i]);
}
sort(r,r+n);
for(i=n-1;i>=0;i--)
{

  //a=2*acos(0.0)*r[i]*r[i];

 a=r[i]*r[i];

  if(i%2==1)
  {
    s+=a;
    //  printf("%ld p\n",a);
   // prv=a;
  }
  else
  {
     // prv-=a;
      // printf("%ld n\n",a);
      s-=a;
  }
}
double d=s;
printf("%.15lf\n",d*2*acos(0.0));
return 0;
}


#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i,n) for(__typeof(n) i=0; i<(n); i++)
#define REP2(i,n) for(__typeof(n) i=1; i<=(n); i++)
#define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
#define INF (1<<30)
#define PI 3.14159265358979323846264338327950
#define PB(x) push_back(x)
#define ALL(x) x.begin(),x.end()
#define SZ size()
#define eps 1e-4
#define VI vector<int>
#define VS vector<string>
#define LL long long
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define CLEAR(x) memset(x,0,sizeof(x));
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)

using namespace std;
bool is_palindrome(const string &s, int start, int end) {
 int sz = end - start + 1;
if (sz % 2 == 0) {
  int middle = start + sz/2;
    int i;
  for (i = 0; middle - 1 - i >= 0 && middle+i <= end && s[middle-1-i] == s[middle+i]; i++);
   if (middle - i - 1 < 0 || middle+i > end)
    return true;
 } else {
   int middle = start + sz/2;
int i;
 for (i = 1; middle - i >= 0 && middle+i <= end && s[middle-i]==s[middle+i]; i++);
   if (middle - i < 0 || middle+i > end)
return true;
 }
return false;
}
//Polya-Burnside theory : (n^6+3n^4+12n^3+8n^2)/24
int euclidd,euclidx,euclidy;
void extendedeuclid(int a,int b)
{
	if(b==0)
	{
		euclidd=a;
		euclidx=1;
		euclidy=0;
		return ;
	}
	extendedeuclid(b,a%b);
	int d1,x1,y1;
	d1=euclidd;
	x1=euclidx;
	y1=euclidy;
	euclidx=y1;
	euclidy=x1-((a/b)*y1);
}
double area_triangle(double x1,double y1,double x2,double y2,double x3,double y3)
{
    double a;
    a=x1*(y2-y3)+y1*(x3-x2)+((x2*y3)-(y2*x3))+eps;
    a=a/2+eps;
return a;
}
double dist_2point(double x1,double y1,double x2,double y2)
{
    double d;
    d=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
    return d;
}
int binary_search(int a[],int n,int l,int u)
{
	int mid;
  if(l>u)
	  return 0;
  mid=floor(l+u)/2;
  if(a[mid]==n)
	  return mid;
  else if(a[mid]>n)
	  binary_search(a,n,l,mid-1);
	  else
	  binary_search(a,n,mid+1,u);
}
int gcd(int a,int b)
{
while(b>0)
{
a=a%b;
a=a^b;
b=b^a;
a=a^b;
}
return a;
}
long lcm(long a,long b)
{
    long x=(a*b)/gcd(a,b);
    return x;
}
long is_prime(long n)
{
	long ii;
if (n == 1)
 return 0;
if (n == 2)
 return 1;
if (n%2 == 0)
 return 0;
for (ii=3;ii*ii<=n;ii=ii+2)
     if (n%ii == 0)
           return 0;
return 1;
}

int main()
{
ios_base::sync_with_stdio(false);
long int n,i;
long long int r[1001],s=0,a;
scanf("%ld",&n);
for(i=0;i<n;i++)
{
       scanf("%ld",&r[i]);
}
sort(r,r+n);
for(i=n-1;i>=0;i--)
{

  //a=2*acos(0.0)*r[i]*r[i];
 // printf("%lf ",a);
 a=r[i]*r[i];
  if(i%2==0)
  {
    s+=a;
   // prv=a;
  }
  else
  {
     // prv-=a;
      s-=a;
  }
}
printf("%.15lf\n",s*2*acos(0.0));
return 0;
}