#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll inf=1000000000000000007;
ll mod=1000000007;
vector<ll> adj[100005];

int main()
{
	fio
	bool flag=true;
	ll test_case=1,tc;
	if(flag)
	cin>>test_case;
	tc=test_case;
	while(tc--){
    	ll n,p,k,i,j,x,sum = 0,ans = 0,c = 0,flag = 0;
    	ll maxi = -1e9,mini = 1e9;
    	vector <ll> v;
    	map<ll,ll>mp;
    	string s;
    	cin>>n>>p>>k;
    	v.push_back(0);
    	for(i=1;i<=n;i++){
    	    cin>>x;
    	    v.push_back(x);
    	   // v[i] += v[max((i-k),0LL)];
    	}
    	sort(v.begin(),v.end());
    	for(i=1;i<=n;i++)
    	v[i] += v[max((i-k),0LL)];
    	auto it = lower_bound(v.begin(),v.end(),p);
    	ll diff = it - v.begin();
    	diff--;
    	ans = diff;
    	if(*it == p){
    	    auto it1=v.begin();
    	    for(it1=it;it1!=v.end();it1++){
    	        if(*it1>p){
    	            break;
    	        }
    	    }
    	    it = it1;
    	    diff = it-v.begin();
    	    diff--;
    	    ans=diff;
    	}
    	ll mode = diff%k;
    	it--;
    	p-=*it;
    	for(i=1;i<mode;i++){
    	    if(v[i]>p){
    	        ans -= (mode-i);
    	        break;
    	    }
    	    p-=v[i];
    	}
    	cout<<ans<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll inf=1000000000000000007;
ll mod=1000000007;
vector<ll> adj[100005];

int main()
{
	fio
	bool flag=true;
	ll test_case=1,tc;
	if(flag)
	cin>>test_case;
	tc=test_case;
	while(tc--){
    	ll n,p,k,i,j,x,y,sum = 0,ans = 0,c = 0,flag = 0;
    	ll maxi = -1e9,mini = 1e9;
    	vector <ll> v;
    	map<ll,ll>mp;
    	string s;
    	cin>>n>>p>>k;
    	ll a[n+1],b[n+1];
    	a[0]=0;
    	for(i=1;i<=n;i++)
    	cin>>a[i];
    	x = n+1;
    	sort(a,a+x);
    	b[0]=0;
    	for(i=1;i<=n;i++){
    	    if(i<k)
    	    b[i] = a[i]+b[i-1];
    	    else if(i == k)
    	    b[i] = a[i];
    	    else
    	    b[i] = a[i] + b[i-k];
    	}
    	for(i=1;i<=n;i++){
    	    if(b[i]>p){
    	        ll mode = i%k;
    	        if((i+(k-mode))<=n){
        	        if(b[i+(k-mode)] <= p){
        	            i = i+(k-mode);
        	        }
    	        }
    	        else
    	        break;
    	    }
    	}
    	ans = (i-1);
    	cout<<ans<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll inf=1000000000000000007;
ll mod=1000000007;
vector<ll> adj[100005];

int main()
{
	fio
	bool flag=true;
	ll test_case=1,tc;
	if(flag)
	cin>>test_case;
	tc=test_case;
	while(tc--){
    	ll n,p,k,i,j,x,y,sum = 0,ans = 0,c = 0,flag = 0;
    	ll maxi = -1e9,mini = 1e9;
    	vector <ll> v;
    	map<ll,ll>mp;
    	string s;
    	cin>>n>>p>>k;
    	ll a[n+1],b[n+1];
    	a[0]=0;
    	for(i=1;i<=n;i++)
    	cin>>a[i];
    	x = n+1;
    	sort(a,a+x);
    	b[0]=0;
    	for(i=1;i<=n;i++){
    	    if(i<k)
    	    b[i] = a[i]+b[i-1];
    	    else if(i == k)
    	    b[i] = a[i];
    	    else
    	    b[i] = a[i] + b[i-k];
    	}
    	for(i=1;i<=n;i++){
    	    if(b[i]>p){
    	        ll mode = i%k;
    	        if(b[i+(k-mode)] <= p){
    	            i = i+(k-mode);
    	        }
    	        else
    	        break;
    	    }
    	}
    	ans = (i-1);
    	cout<<ans<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll inf=1000000000000000007;
ll mod=1000000007;
vector<ll> adj[100005];

int main()
{
	fio
	bool flag=true;
	ll test_case=1,tc;
	if(flag)
	cin>>test_case;
	tc=test_case;
	while(tc--){
    	ll n,p,k,i,j,x,y,sum = 0,ans = 0,c = 0,flag = 0;
    	ll maxi = -1e9,mini = 1e9;
    	vector <ll> v;
    	map<ll,ll>mp;
    	string s;
    	cin>>n>>p>>k;
    	ll a[n+1],b[n+1];
    	a[0]=0;
    	for(i=1;i<=n;i++)
    	cin>>a[i];
    	x = n+1;
    	sort(a,a+x);
    	b[0]=0;
    	for(i=1;i<=n;i++){
    	    if(i<k)
    	    b[i] = a[i]+b[i-1];
    	    else if(i == k)
    	    b[i] = a[i];
    	    else
    	    b[i] = a[i] + b[i-k];
    	}
    	for(i=1;i<=n;i++){
    	    if(b[i]>p){
    	        ll mode = i%k;
    	        if((i+(k-mode))<=n){
        	        if(b[i+(k-mode)] <= p){
        	            i = i+(k-mode);
        	        }
        	        else
    	            break;
    	        }
    	        else
    	        break;
    	    }
    	}
    	ans = (i-1);
    	cout<<ans<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      ll sum = 0;
      ll temp = 0;
      for (int u = 0; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
      if (temp > ans) {
        ans = temp;
      }
      break;
    }
    ll sum = v[0];
    ll temp = 1;
    for (int e = 1; e <= i; e++) {
      if (sum + v[e] <= c) {
        temp++;
        sum += v[e];
      } else {
        break;
      }
    }
    if (i >= k - 1) {
      temp = k;
      sum = v[i];
    }
    if (i + k >= n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k >= n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        for (int u = j - k + 1; u < n; u++) {
          if (sum + v[u] <= c) {
            sum += v[u];
            temp++;
          } else {
            break;
          }
        }
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  for (int i = 0; i < n; i++) {
    if (v[i] > c) {
      ll sum = 0;
      ll temp = 0;
      for (int u = 0; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
      if (temp > ans) {
        ans = temp;
      }
      break;
    }
    ll sum = v[0];
    ll temp = 1;
    if (i >= k - 1) {
      temp = k;
      sum = v[i];
    }
    if (i + k >= n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k >= n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        for (int u = j - k + 1; u < n; u++) {
          if (sum + v[u] <= c) {
            sum += v[u];
            temp++;
          } else {
            break;
          }
        }
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      break;
    }
    ll sum = v[i];
    ll temp = 1;
    if (i == k - 1) {
      temp = k;
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k > n) {
          if (v[j + 1] + sum <= c) {
            temp++;
          }
        }
      } else {
        ll ki = j - 1;
        while (sum + v[ki] > c && ki > j - k) {
          ki--;
        }
        if (ki != j - k) {
          // cout << "to " << toAdd << endl;
          temp += 1;
        }
        break;
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  for (int i = 0; i < n; i++) {
    if (v[i] > c) {
      ll sum = 0;
      ll temp = 0;
      for (int u = 0; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
      if (temp > ans) {
        ans = temp;
      }
      break;
    }
    ll sum = v[i];
    ll temp = 1;
    if (i >= k - 1) {
      temp = k;
    }
    if (i + k >= n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k >= n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        for (int u = j - k + 1; u < n; u++) {
          if (sum + v[u] <= c) {
            sum += v[u];
            temp++;
          } else {
            break;
          }
        }
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      break;
    }
    ll sum = v[i];
    ll temp = 1;
    if (i == k - 1) {
      temp = k;
    }
    if (i + k > n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k > n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        ll ki = j - 1;
        while (sum + v[ki] > c && ki > j - k) {
          ki--;
        }
        if (ki != j - k) {
          // cout << "to " << toAdd << endl;
          temp += 1;
        }
        break;
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      break;
    }
    ll sum = v[i];
    ll temp = 1;
    if (i == k - 1) {
      temp = k;
    }
    if (i + k > n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k > n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        for (int u = j - k + 1; u < n; u++) {
          if (sum + v[u] <= c) {
            sum += v[u];
            temp++;
          } else {
            break;
          }
        }
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      break;
    }
    ll sum = v[i];
    ll temp = 1;
    if (i == k - 1) {
      temp = k;
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k > n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        ll ki = j - 1;
        while (sum + v[ki] > c && ki > j - k) {
          ki--;
        }
        if (ki != j - k) {
          // cout << "to " << toAdd << endl;
          temp += 1;
        }
        break;
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  ll gu = 0;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      ll sum = 0;
      ll temp = 0;
      for (int u = 0; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
      if (temp > ans) {
        ans = temp;
      }
      break;
    }
    ll sum = 0;
    ll temp = 0;
    if (gu + v[i] <= c) {
      gu += v[i];
      sum = gu;
      temp = i + 1;
    }
    if (i == k - 1) {
      temp = k;
      sum = v[i];
    } else if (i + k >= n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
      if (temp > ans) {
        ans = temp;
      }
      i = k - 2;
      // i++;
    }
    // cout << i << endl;
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k >= n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        for (int u = j - k + 1; u < n; u++) {
          if (sum + v[u] <= c) {
            sum += v[u];
            temp++;
          } else {
            break;
          }
        }
        break;
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  ll gu = 0;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      ll sum = 0;
      ll temp = 0;
      for (int u = 0; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
      if (temp > ans) {
        ans = temp;
      }
      break;
    }
    ll sum = 0;
    ll temp = 0;
    if (gu + v[i] <= c) {
      gu += v[i];
      sum = gu;
      temp = i + 1;
    }
    if (i >= k - 1) {
      temp = k;
      sum = v[i];
    }
    if (i + k >= n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k >= n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        for (int u = j - k + 1; u < n; u++) {
          if (sum + v[u] <= c) {
            sum += v[u];
            temp++;
          } else {
            break;
          }
        }
        break;
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      ll sum = 0;
      ll temp = 0;
      for (int u = 0; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
      if (temp > ans) {
        ans = temp;
      }
      break;
    }
    ll sum = v[i];
    ll temp = 1;
    if (i == k - 1) {
      temp = k;
    }
    if (i + k >= n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k >= n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        for (int u = j - k + 1; u < n; u++) {
          if (sum + v[u] <= c) {
            sum += v[u];
            temp++;
          } else {
            break;
          }
        }
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  ll gu = 0;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      ll sum = 0;
      ll temp = 0;
      for (int u = 0; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
      if (temp > ans) {
        ans = temp;
      }
      break;
    }
    ll sum = 0;
    ll temp = 0;
    if (gu + v[i] <= c) {
      gu += v[i];
      sum = gu;
      temp = i + 1;
    }
    if (i >= k - 1) {
      temp = k;
      sum = v[i];
    }
    if (i + k >= n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
      if (temp > ans) {
        ans = temp;
      }
      break;
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k >= n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        for (int u = j - k + 1; u < n; u++) {
          if (sum + v[u] <= c) {
            sum += v[u];
            temp++;
          } else {
            break;
          }
        }
        break;
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      break;
    }
    ll sum = v[i];
    ll temp = 1;
    if (i == k - 1) {
      temp = k;
    }
    if (i + k >= n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k > n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        for (int u = j - k + 1; u < n; u++) {
          if (sum + v[u] <= c) {
            sum += v[u];
            temp++;
          } else {
            break;
          }
        }
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      break;
    }
    ll sum = v[i];
    ll temp = 1;
    if (i == k - 1) {
      temp = k;
    }
    if (i + k >= n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k >= n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        for (int u = j - k + 1; u < n; u++) {
          if (sum + v[u] <= c) {
            sum += v[u];
            temp++;
          } else {
            break;
          }
        }
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  ll gu = 0;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      break;
    }
    ll sum = 0;
    ll temp = 0;
    if (gu + v[i] <= c) {
      gu += v[i];
      sum = gu;
      temp = i + 1;
    }
    if (i >= k - 1) {
      temp = k;
      sum = v[i];
    }
    if (i + k >= n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
      if (temp > ans) {
        ans = temp;
      }
      break;
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k >= n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        for (int u = j - k + 1; u < n; u++) {
          if (sum + v[u] <= c) {
            sum += v[u];
            temp++;
          } else {
            break;
          }
        }
        break;
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define bg begin
#define ff first
#define ss second
#define PI 3.14
#define quick                  \
  ios::sync_with_stdio(false); \
  cin.tie(0);
#define time cout << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define countBits(x) __builtin_popcount(ll(x))
#define countZeroesAtBegin(x) __builtin_clz(ll(x))
#define countZeroesAtEnd(x) __builtin_ctz(ll(x))
#define last(x) x[x.end() - x.begin() - 1]
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)
#define forkn(i, k, n) for (ll i = k; i < ll(n); i++)
#define forin(n) for (ll i = ll(n - 1); i >= 0; i--)

bool comp(pl a, pl b) {
  if (a.ff < b.ff) {
    return true;
  }
  return false;
}

void solve() {
  ll n, c, k;
  cin >> n >> c >> k;
  vector<ll> v(n);
  forn(n) { cin >> v[i]; }
  sort(v.begin(), v.end());
  ll ans = 0;
  if (v[0] > c) {
    cout << 0 << endl;
    return;
  }
  // cout << "elems ";
  // for (auto i : v) {
  //   cout << i << " ";
  // }
  // cout << " cost c - " << c << endl;
  // cout << endl;
  for (int i = 0; i < k; i++) {
    if (v[i] > c) {
      ll sum = 0;
      ll temp = 0;
      for (int u = 0; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
      if (temp > ans) {
        ans = temp;
      }
      break;
    }
    ll sum = v[0];
    ll temp = 1;
    for (int e = 1; e <= i; e++) {
      if (sum + v[e] <= c) {
        temp++;
        sum += v[e];
      } else {
        break;
      }
    }
    if (i >= k - 1) {
      temp = k;
      sum = v[i];
    }
    if (i + k >= n) {
      for (int u = i + 1; u < n; u++) {
        if (sum + v[u] <= c) {
          sum += v[u];
          temp++;
        } else {
          break;
        }
      }
    }
    for (int j = i + k; j < n; j += k) {
      if (sum + v[j] <= c) {
        temp += k;
        sum += v[j];
        if (j + k >= n) {
          for (int u = j + 1; u < n; u++) {
            if (sum + v[u] <= c) {
              sum += v[u];
              temp++;
            } else {
              break;
            }
          }
        }
      } else {
        for (int u = j - k + 1; u < n; u++) {
          if (sum + v[u] <= c) {
            sum += v[u];
            temp++;
          } else {
            break;
          }
        }
        break;
      }
    }
    // cout << i << " " << temp << endl;
    if (temp > ans) {
      ans = temp;
    }
  }
  cout << ans << endl;
}

int main() {
  quick;
  // solve();
  ll t;
  cin >> t;
  while (t--) {
    solve();
  }
}


#include<bits/stdc++.h>

using namespace std;
#define int long long
int str[200009];//后缀
int arr[200009];
int n,p,k;
int ok(int res){
	int add=0;
	int x=res/k;
	int y=res%k;
	int temp=res;
	while(1){
		if(temp<k){
			break;
		}
		add+=arr[temp];
		temp-=k;
	}
	if(add>p){
		return 0;
	}
	for(int i=0;i<=y;i++){
		add+=arr[i];
		if(add>p){
			return 0;
		}
	}
	return 1;
}
signed  main(){
	int _;
	cin>>_;
	while(_--){
		cin>>n>>p>>k;
		for(int i=0;i<n;i++) cin>>arr[i];
		sort(arr,arr+n);
		int add=0;
		int ans=-1;
		int Add=0;
		for(int i=0;i<k;i++){
			Add+=arr[i];
			if(Add>p){
				break;
			}else{
				ans=max(ans,i);
			}
		}
	//	cout<<ans<<'\n';
		int l=k;
		int r=n-1;
		if(n>=k){
			if(arr[k-1]<=p){
				ans=max(ans,k-1);
			}
		}
		//cout<<ans<<'\n';
		while(l<=r){
			int mid=(l+r)/2;
			if(ok(mid)){
				ans=max(ans,mid);
				l=mid+1;
			}else{
				r=mid-1;
			}
		}
		if(ans==-1){
			cout<<"0"<<'\n';
		}else{
			cout<<ans+1<<'\n';
		}
		for(int i=0;i<=n+1;i++) arr[i]=0;
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define int long long
int str[200009];//前缀
int arr[200009];
int n,p,k;
signed  main(){
	int _;
	cin>>_;
	while(_--){
		cin>>n>>p>>k;
		for(int i=1;i<=n;i++) cin>>arr[i];
		sort(arr+1,arr+1+n);
		for(int i=1;i<=n;i++) str[i]=str[i-1]+arr[i];
		int ans=0;
		for(int i=0;i<k;i++){
			int sum=str[i];
			if(sum<=p){
				ans=max(ans,i);
			}else{
				break;
			}
			for(int j=i+k;j<=n;j+=k){
				sum+=arr[j];
				if(sum<=p){
					ans=max(ans,j);
				}else{
					break;
				}
			}
		}
		cout<<ans<<'\n';
		for(int i=0;i<=n+1;i++) arr[i]=0,str[i]=0;

	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define int long long
int str[200009];//后缀
int arr[200009];
int n,p,k;
int ok(int res){
	int x=res/k;
	int add=0;
	int y=res%k;
	int temp=res;
	while(1){
		if(temp<k){
			break;
		}
		add+=arr[temp];
		temp-=k;
	}
	if(temp>p){
		return 0;
	}
	for(int i=0;i<x;i++){
		add+=arr[i];
		if(add>p){
			return 0;
		}
	}
	return 1;
}
signed  main(){
	int _;
	cin>>_;
	while(_--){
		cin>>n>>p>>k;
		for(int i=0;i<n;i++) cin>>arr[i];
		sort(arr,arr+n);
		int add=0;
		int ans=-1;
		int Add=0;
		for(int i=0;i<k;i++){
			Add+=arr[i];
			if(Add>p){
				break;
			}else{
				ans=max(ans,i);
			}
		}
		int l=k;
		int r=n-1;
		if(n>=k){
			if(arr[k-1]<=p){
				ans=max(ans,k-1);
			}
		}
		//cout<<ans<<'\n';
		while(l<=r){
			int mid=(l+r)/2;
			if(ok(mid)){
				ans=max(ans,mid);
				l=mid+1;
			}else{
				r=mid-1;
			}
		}
	//	cout<<ok(3)<<'\n';
		if(ans==-1){
			cout<<"0"<<'\n';
		}else{
			cout<<ans+1<<'\n';
		}
		for(int i=0;i<=n+1;i++) arr[i]=0;
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define int long long
int str[200009];//前缀
int arr[200009];
int n,p,k;
signed  main(){
	int _;
	cin>>_;
	while(_--){
		cin>>n>>p>>k;
		for(int i=1;i<=n;i++) cin>>arr[i];
		sort(arr+1,arr+1+n);
		for(int i=1;i<=n;i++) str[i]=str[i-1]+arr[i];
		int ans=0;
		for(int i=0;i<n;i++){
			int sum=str[i];
			if(sum<=p){
				ans=max(ans,i);
			}else{
				break;
			}
			for(int j=i+k;j<=n;j+=k){
				sum+=arr[j];
				if(sum<=p){
					ans=max(ans,j);
				}else{
					break;
				}
			}
		}
		cout<<ans<<'\n';
		for(int i=0;i<=n+1;i++) arr[i]=0,str[i]=0;

	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
#define int long long
int str[200009];//后缀
int arr[200009];
int n,p,k;
int ok(int res){
	int add=0;
	int x=res/k;
	int y=res%k;
	int temp=res;
	while(1){
		if(temp<k){
			break;
		}
		add+=arr[temp];
		temp-=k;
	}
	if(temp>p){
		return 0;
	}
	for(int i=0;i<=y;i++){
		add+=arr[i];
		if(add>p){
			return 0;
		}
	}
	return 1;
}
signed  main(){
	int _;
	cin>>_;
	while(_--){
		cin>>n>>p>>k;
		for(int i=0;i<n;i++) cin>>arr[i];
		sort(arr,arr+n);
		int add=0;
		int ans=-1;
		int Add=0;
		for(int i=0;i<k;i++){
			Add+=arr[i];
			if(Add>p){
				break;
			}else{
				ans=max(ans,i);
			}
		}
	//	cout<<ans<<'\n';
		int l=k;
		int r=n-1;
		if(n>=k){
			if(arr[k-1]<=p){
				ans=max(ans,k-1);
			}
		}
		//cout<<ans<<'\n';
		while(l<=r){
			int mid=(l+r)/2;
			if(ok(mid)){
				ans=max(ans,mid);
				l=mid+1;
			}else{
				r=mid-1;
			}
		}
		if(ans==-1){
			cout<<"0"<<'\n';
		}else{
			cout<<ans+1<<'\n';
		}
		for(int i=0;i<=n+1;i++) arr[i]=0;
	}
	return 0;
}


#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=200100;
const int M=1000100;
const LL mod=998244353;
int a[N];
int s[N];//前缀和
int main(){
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--){
		int n,p,k;
		cin>>n>>p>>k;
		s[0]=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			s[i]=0;
		}
		sort(a+1,a+n+1);
		for(int i=1;i<=n;i++) s[i]=s[i-1]+a[i];//单纯的前缀和
		int mmax=0;
		for(int i=0;i<k;i++){//单取的个数
			int sum=s[i];
			int ans=i;
			if(sum>p){
				ans--;
				mmax=max(mmax,ans);
				break;
			}
			for(int j=i+k;j<=n;j+=k){
				sum+=a[j];
				ans+=k;
				if(sum>p){
					ans-=k;
					break;
				}
			}
			mmax=max(mmax,ans);
		}
		cout<<mmax<<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,j,n,p,k;cin>>n>>p>>k;
        for(i=0;i<n;i++)
        {
            long long int temp;cin>>temp;
            v1.push_back(temp);
        }
        long long int price = 0;
        long long int ans = 0;
        sort(v1.begin(),v1.end());
        for(i=0;i<=k;i++)
        {
            long long int count = 0 , price = 0;
            if(v1[i]>p) break;
            for(j=i;j<n;j+=k)
            {
                if(price + v1[j]<=p)
                {
                    count = j+1 ;
                    price += v1[j];
                }
                else
                {
                    break;
                }
            }
            ans = max(ans,count);
        }
        cout<<ans<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    while(t--)
    {
        vector<int>v1;
        long long int i,j,n,p,k;cin>>n>>p>>k;
        for(i=0;i<n;i++)
        {
            long long int temp;cin>>temp;
            v1.push_back(temp);
        }
        long long int price = 0;
        long long int ans = 0;
        sort(v1.begin(),v1.end());
        for(i=0;i<k;i++)
        {
            long long int count = 0 , price = 0;
            if(v1[i]>p) break;
            for(j=i;j<n;j+=k)
            {
                if(price + v1[j]<=p)
                {
                    count = j+1 ;
                    price += v1[j];
                }
                else
                {
                    break;
                }
            }
            ans = max(ans,count);
        }
        cout<<ans<<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];
        }
        // cout<<i<<endl;
        for(i;i<=n;i++)
        {
            j = i;
            while(j>=k)
            {
                dp[i]+= v1[j]; j = j - k;
            }
            dp[i]+= dp[j];
        }
        i = 1;
        // fo1(i,n)cout<<dp[i]<<" ";
        while(dp[i]<=p && i<n && dp[i]!=0)i++;
        if(dp[n]<=p && dp[n]!=0) i = n+1;
        cout<<i-1<<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];
        }
        // cout<<i<<endl;
        for(i;i<=n;i++)
        {
            j = i;
            while(j>=k)
            {
                dp[i]+= v1[j]; j = j - k;
            }
            dp[i]  += dp[j];
        }
        i = n;
        while(i>=1)
        {
            if(dp[i]!=0 && dp[i]<=p){break;}
            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,j,n,p,k;cin>>n>>p>>k;
        for(i=0;i<n;i++)
        {
            long long int temp;cin>>temp;
            v1.push_back(temp);
        }
        long long int price = 0;
        long long int ans = 0;
        sort(v1.begin(),v1.end());
        for(i=0;i<k;i++)
        {
            long long int count = 0 , price = 0;
            if(v1[i]>p) break;
            for(j=i+k-2;j<n;j+=k)
            {
                if(price + v1[j]<=p)
                {
                    count = j + k-1;
                    price += v1[j];
                }
                else
                {
                    break;
                }
            }
            ans = max(ans,count);
        }
        cout<<ans<<endl;
    }
}


#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 <bits/stdc++.h>
#define sqr(a) ((a)*(a))
#define rsz resize
#define forr(i,a,b) for(ll i=(a);i<(b);i++)
#define forn(i,n) forr(i,0,n)
#define dforn(i,n) for(int i=n-1;i>=0;i--)
#define forall(it,v) for(auto it=v.begin();it!=v.end();it++)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fst first
#define snd second
#define PI 3.1415926535897932384626

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int main(int argc, char *argv[]) {
	int q;
	cin >> q;

	while(q--){
		ll n, p, k;
		cin >> n >> p >> k;
		ll in[20000];
		ll price[20000];
		ll cont = 0;
		ll nt = n;

		for(ll i=0; i<k; i++){
			price[i] = 0;
		}

		for(ll i=0; i<n; i++){
			cin >> in[i];
		}
		sort(in, in+n);
		ll s  = 0;
		bool rip = false;
		while(n--){
			price[cont%k] = price[cont%k] + in[cont];
			if ((price[cont%k])>p){
				rip = true;
				break;
			}
			if((cont%k)==(k-1)){
				s++;
			}
			cont++;

		}

		if(rip==false){cout << nt << endl;}
		else if(cont == 0){cout << 0 << endl;}
		else{

			if((price[cont%k])>p){cont--;}
			if((cont%k)==(k-1)){cout << s*k << endl;}
			else if(in[cont]==in[((s+1)*k)-1]){cout << (s+1)*k << endl;}
			else{cout << (s*k) + 1 << endl;}
		}

	}
	return 0;
}


#include <bits/stdc++.h>
#define sqr(a) ((a)*(a))
#define rsz resize
#define forr(i,a,b) for(ll i=(a);i<(b);i++)
#define forn(i,n) forr(i,0,n)
#define dforn(i,n) for(int i=n-1;i>=0;i--)
#define forall(it,v) for(auto it=v.begin();it!=v.end();it++)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fst first
#define snd second
#define PI 3.1415926535897932384626

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int main(int argc, char *argv[]) {
	int q;
	cin >> q;

	while(q--){
		ll n, p, k;
		cin >> n >> p >> k;
		ll in[200000];
		ll price[200000];
		ll cont = 0;
		ll nt = n;

		for(ll i=0; i<k; i++){
			price[i] = 0;
		}

		for(ll i=0; i<n; i++){
			cin >> in[i];
		}
		sort(in, in+n);
		ll s  = 0;
		bool rip = false;
		while(n--){
			price[cont%k] = price[cont%k] + in[cont];
			if ((price[cont%k])>p){
				rip = true;
				break;
			}
			if((cont%k)==(k-1)){
				s++;
			}
			cont++;

		}

		if(rip==false){cout << nt << endl;}
		else if(cont == 0){cout << 0 << endl;}
		else{

			if((price[cont%k])>p){cont--;}
			if((cont%k)==(k-1)){cout << s*k << endl;}
			else if(((((s+1)*k)-1)<=(nt-1))&&(in[cont]==in[((s+1)*k)-1])){cout << (s+1)*k << endl;}
			else{cout << (s*k) + 1 << endl;}
		}

	}
	return 0;
}


#include <bits/stdc++.h>
#define sqr(a) ((a)*(a))
#define rsz resize
#define forr(i,a,b) for(ll i=(a);i<(b);i++)
#define forn(i,n) forr(i,0,n)
#define dforn(i,n) for(int i=n-1;i>=0;i--)
#define forall(it,v) for(auto it=v.begin();it!=v.end();it++)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fst first
#define snd second
#define PI 3.1415926535897932384626

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int main(int argc, char *argv[]) {

	ll q;
	cin >> q;
	while(q--){
		ll n, c, k;
		cin >> n >> c >> k;
		ll price[200000];
		forn(i, n){cin>>price[i];}
		sort(price, price+n);
		ll ok[200000];
		ll cont[200000];
		ok[0] = 0;
		cont[0] = 0;

		for(ll i=1; i<k; i++){
			ok[i] = ok[i-1] + price[i-1];
			cont[i] = cont[i-1] + 1;
		}

		if(c<(ok[0]+price[k-1])){
			ll pos = 1;

			while((pos<k)&&(c>=ok[pos])){pos++;}
			pos--;
			cout << cont[pos] << endl;
		}
		else{
		for(ll i=0; i<k; i++){
			cont[i] +=  k;
			while((cont[i]<=n) && (c>=(ok[i]+price[cont[i]-1]))){
				ok[i] += price[cont[i]-1];
				cont[i] +=  k;
			}
			cont[i] -= k;
		}

		sort(cont, cont+k);
		cout << cont[k-1] << endl;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#define sqr(a) ((a)*(a))
#define rsz resize
#define forr(i,a,b) for(ll i=(a);i<(b);i++)
#define forn(i,n) forr(i,0,n)
#define dforn(i,n) for(int i=n-1;i>=0;i--)
#define forall(it,v) for(auto it=v.begin();it!=v.end();it++)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fst first
#define snd second
#define PI 3.1415926535897932384626

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int main(int argc, char *argv[]) {

	ll q;
	cin >> q;
	while(q--){
		ll n, c, k;
		cin >> n >> c >> k;
		ll price[200000];
		forn(i, n){cin>>price[i];}
		sort(price, price+n);
		ll ok[200000];
		int cont[200000];
		ok[0] = 0;
		cont[0] = 0;
		bool fin = false;

		for(int i=1; i<k; i++){
			ok[i] = ok[i-1] + price[i-1];
			cont[i] = cont[i-1] + 1;
		}

		ll tot = 0;
		if(c<(ok[0]+price[k-1])){
			ll pos = 1;

			while((pos<k)&&(c>=ok[pos])){pos++;}
			pos--;
			cout << cont[pos] << endl;
		}
		else{
		for(ll i=k-1; i<n; i++){
			if(c>=(ok[tot%k]+price[i])){
				ok[tot%k] += price[i];
				cont[tot%k] +=  k;

				tot++;
			}
			else {break;}
		}

		tot--;

		cout << cont[tot%k] << endl;
		}
		//for(ll i=0; i<k; i++){cout << "ok " << ok[i] << " cont " << cont[i] << endl;}
	}

	return 0;
}


#include <bits/stdc++.h>
#define sqr(a) ((a)*(a))
#define rsz resize
#define forr(i,a,b) for(ll i=(a);i<(b);i++)
#define forn(i,n) forr(i,0,n)
#define dforn(i,n) for(int i=n-1;i>=0;i--)
#define forall(it,v) for(auto it=v.begin();it!=v.end();it++)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fst first
#define snd second
#define PI 3.1415926535897932384626

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int main(int argc, char *argv[]) {
	int q;
	cin >> q;

	while(q--){
		ll n, p, k;
		cin >> n >> p >> k;
		ll in[20000];
		ll price[20000];
		ll cont = 0;
		ll nt = n;

		for(ll i=0; i<k; i++){
			price[i] = 0;
		}

		for(ll i=0; i<n; i++){
			cin >> in[i];
		}
		sort(in, in+n);
		ll s  = 0;
		bool rip = false;
		while(n--){
			price[cont%k] = price[cont%k] + in[cont];
			if ((price[cont%k])>p){
				rip = true;
				break;
			}
			if((cont%k)==(k-1)){
				s++;
			}
			cont++;

		}

		if(rip==false){cout << nt << endl;}
		else if(cont == 0){cout << 0 << endl;}
		else{

			if((price[cont%k])>p){cont--;}
			if((cont%k)==(k-1)){cout << s*k << endl;}
			else if(((((s+1)*k)-1)<=(nt-1))&&(in[cont]==in[((s+1)*k)-1])){cout << (s+1)*k << endl;}
			else{cout << (s*k) + 1 << endl;}
		}

	}
	return 0;
}


#include <bits/stdc++.h>
#define sqr(a) ((a)*(a))
#define rsz resize
#define forr(i,a,b) for(ll i=(a);i<(b);i++)
#define forn(i,n) forr(i,0,n)
#define dforn(i,n) for(int i=n-1;i>=0;i--)
#define forall(it,v) for(auto it=v.begin();it!=v.end();it++)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fst first
#define snd second
#define PI 3.1415926535897932384626

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int main(int argc, char *argv[]) {
	int q;
	cin >> q;

	while(q--){
		ll n, p, k;
		cin >> n >> p >> k;
		ll in[200000];
		ll price[200000];
		ll cont = 0;
		ll nt = n;

		for(ll i=0; i<k; i++){
			price[i] = 0;
		}

		for(ll i=0; i<n; i++){
			cin >> in[i];
		}
		sort(in, in+n);
		ll s  = 0;
		bool rip = false;
		while(n--){
			price[cont%k] = price[cont%k] + in[cont];
			if ((price[cont%k])>=p){
				rip = true;
				break;
			}
			if((cont%k)==(k-1)){
				s++;
			}
			cont++;

		}
		if(rip==false){cout << nt << endl;}
		else if(cont == 0){cout << 0 << endl;}
		else{

			if((price[cont%k])>p){cont--;}
			if((cont%k)==(k-1)){cout << s*k << endl;}
			else if(in[cont]==in[((s+1)*k)-1]){cout << (s+1)*k << endl;}
			else{cout << (s*k) + 1 << endl;}
		}

	}
	return 0;
}


#include <bits/stdc++.h>
#define sqr(a) ((a)*(a))
#define rsz resize
#define forr(i,a,b) for(ll i=(a);i<(b);i++)
#define forn(i,n) forr(i,0,n)
#define dforn(i,n) for(int i=n-1;i>=0;i--)
#define forall(it,v) for(auto it=v.begin();it!=v.end();it++)
#define sz(c) ((int)c.size())
#define zero(v) memset(v, 0, sizeof(v))
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fst first
#define snd second
#define PI 3.1415926535897932384626

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int main(int argc, char *argv[]) {
	int q;
	cin >> q;

	while(q--){
		ll n, p, k;
		cin >> n >> p >> k;
		ll in[20000];
		ll price[20000];
		ll cont = 0;
		ll nt = n;

		for(ll i=0; i<k; i++){
			price[i] = 0;
		}

		for(ll i=0; i<n; i++){
			cin >> in[i];
		}
		sort(in, in+n);
		ll s  = 0;
		bool rip = false;
		while(n--){
			price[cont%k] = price[cont%k] + in[cont];
			if ((price[cont%k])>p){
				rip = true;
				break;
			}
			if((cont%k)==(k-1)){
				s++;
			}
			cont++;

		}

		if(rip==false){cout << nt << endl;}
		else if(cont == 0){cout << 0 << endl;}
		else{

			if((price[cont%k])>p){cont--;}
			if((cont%k)==(k-1)){cout << s*k << endl;}
			else if(((((s+1)*k)-1)<=(n-1))&&(sin[cont]==in[((s+1)*k)-1])){cout << (s+1)*k << endl;}
			else{cout << (s*k) + 1 << endl;}
		}

	}
	return 0;
}


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

using namespace std;

const int MAXN = 35, MAXM = 905;
int s[MAXN][MAXN], t[MAXN][MAXN], cnt[MAXM],
    gox[] = {-1, -1, -1, 0, 1, 1, 1, 0},
    goy[] = {-1, 0, 1, 1, 1, 0, -1, -1},
    x = -1, y = -1, n, m;
char used[MAXN][MAXN];
vector< pair<int, int> > ans;

int dist(int x0, int y0, int x1, int y1) {
    return abs(x1 - x0) + abs(y1 - y0);
}

void makeMove(int sx, int sy, int tx, int ty) {
    while(x != tx || y != ty) {
        int tox = -1, toy = -1;
        for(int i = 0; i < 8; i++) {
            int xx = x + gox[i], yy = y + goy[i];
            if(0 <= xx && xx < n && 0 <= yy && yy < m &&
               !used[xx][yy] && (xx != sx || yy != sy) &&
               (tox == -1 || dist(tx, ty, xx, yy) < dist(tx, ty, tox, toy))) {
                tox = xx;
                toy = yy;
            }
        }
        swap(s[x][y], s[tox][toy]);
        x = tox;
        y = toy;
        ans.push_back(make_pair(x, y));
    }
    swap(s[x][y], s[sx][sy]);
    x = sx;
    y = sy;
    ans.push_back(make_pair(x, y));
}

void makePath(int tx, int ty) {
    int sx = -1, sy = -1;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++)
            if(!used[i][j] && (i != x || j != y) && s[i][j] == t[tx][ty]) {
                sx = i;
                sy = j;
                break;
            }
        if(sx != -1)
            break;
    }
    while(sx != tx || sy != ty) {
        int tox = -1, toy = -1;
        for(int i = 0; i < 8; i++) {
            int xx = sx + gox[i], yy = sy + goy[i];
            if(0 <= xx && xx < n && 0 <= yy && yy < m && !used[xx][yy] &&
               (tox == -1 || dist(tx, ty, xx, yy) < dist(tx, ty, tox, toy))) {
                tox = xx;
                toy = yy;
            }
        }
        makeMove(sx, sy, tox, toy);
        sx = tox;
        sy = toy;
    }
    used[tx][ty] = true;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++) {
            cin >> s[i][j];
            cnt[s[i][j]]++;
        }
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++) {
            cin >> t[i][j];
            cnt[t[i][j]]--;
        }
    for(int i = 0; i < MAXM; i++)
        if(cnt[i]) {
            cout << -1 << '\n';
            return 0;
        }
    if(n == 1) {
        for(int i = 0; i < m; i++)
            for(int j = i + 2; j <= m; j++) {
                rotate(s[0] + i, s[0] + i + 1, s[0] + j);
                bool ok = true;
                for(int k = 0; k < m; k++)
                    if(s[0][k] != t[0][k]) {
                        ok = false;
                        break;
                    }
                if(ok) {
                    cout << j - i - 1 << '\n';
                    for(int k = i; k < j; k++)
                        cout << 1 << ' ' << k + 1 << '\n';
                    return 0;
                }
                rotate(s[0] + i, s[0] + j - 2, s[0] + j);
                ok = true;
                for(int k = 0; k < m; k++)
                    if(s[0][k] != t[0][k]) {
                        ok = false;
                        break;
                    }
                if(ok) {
                    cout << j - i - 1 << '\n';
                    for(int k = j - 1; k >= i; k--)
                        cout << 1 << ' ' << k + 1 << '\n';
                    return 0;
                }
                rotate(s[0] + i, s[0] + i + 1, s[0] + j);
            }
        cout << -1 << '\n';
        return 0;
    }
    if(m == 1) {
        swap(m, n);
        for(int i = 0; i < m; i++)
            s[0][i] = s[i][0];
        for(int i = 0; i < m; i++)
            t[0][i] = t[i][0];
        for(int i = 0; i < m; i++)
            for(int j = i + 2; j <= m; j++) {
                rotate(s[0] + i, s[0] + i + 1, s[0] + j);
                bool ok = true;
                for(int k = 0; k < m; k++)
                    if(s[0][k] != t[0][k]) {
                        ok = false;
                        break;
                    }
                if(ok) {
                    cout << j - i - 1 << '\n';
                    for(int k = i; k < j; k++)
                        cout << k + 1 << ' ' << 1 << '\n';
                    return 0;
                }
                rotate(s[0] + i, s[0] + j - 2, s[0] + j);
                ok = true;
                for(int k = 0; k < m; k++)
                    if(s[0][k] != t[0][k]) {
                        ok = false;
                        break;
                    }
                if(ok) {
                    cout << j - i - 1 << '\n';
                    for(int k = j - 1; k >= i; k--)
                        cout << k + 1 << ' ' << 1 << '\n';
                    return 0;
                }
                rotate(s[0] + i, s[0] + i + 1, s[0] + j);
            }
        cout << -1 << '\n';
        return 0;
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++)
            if(s[i][j] == t[n - 1][m - 1]) {
                x = i;
                y = j;
                break;
            }
        if(x >= 0)
            break;
    }
    ans.push_back(make_pair(x, y));
    for(int i = 0; i < n - 2; i++)
        for(int j = 0; j < m; j++)
            makePath(i, j);
    for(int i = 0; i < m - 1; i++)
        for(int j = n - 2; j < n; j++)
            makePath(j, i);
    makePath(n - 2, m - 1);
    cout << (int)ans.size() - 1 << '\n';
    for(size_t i = 0; i < ans.size(); i++)
        cout << ans[i].first + 1 << ' ' << ans[i].second + 1 << '\n';
    return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

using namespace std;

const int N = 42;

int a[N][N], b[N][N];
int old_a[N][N];
int ca[N * N], cb[N * N];
int new_a[N][N], new_b[N][N];

bool FLAG;

void print(vector < pair <int, int> > &moves) {
  int cnt = moves.size();
  printf("%d\n", cnt - 1);
  for (int i = 0; i < cnt; i++) {
    if (FLAG) {
      swap(moves[i].first, moves[i].second);
    }
    printf("%d %d\n", moves[i].first + 1, moves[i].second + 1);
  }
  exit(0);
}

vector < pair <int, int> > moves;
int h, w;

void add(int x, int y) {
  assert(0 <= x && x < h && 0 <= y && y < w);
  int sz = moves.size();
  if (sz > 0) {
    int px = moves[sz - 1].first;
    int py = moves[sz - 1].second;
    swap(a[x][y], a[px][py]);
  }
  moves.push_back(make_pair(x, y));
}

int main() {
  scanf("%d %d", &h, &w);
  for (int i = 0; i < N * N; i++) {
    ca[i] = cb[i] = 0;
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      scanf("%d", a[i] + j);
      ca[a[i][j]]++;
      old_a[i][j] = a[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      scanf("%d", b[i] + j);
      cb[b[i][j]]++;
    }
  }
  for (int i = 0; i < N * N; i++) {
    if (ca[i] != cb[i]) {
      printf("%d\n", -1);
      return 0;
    }
  }
  FLAG = false;
  if (w == 1) {
    FLAG = true;
    swap(h, w);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        new_a[i][j] = a[j][i];
        new_b[i][j] = b[j][i];
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        a[i][j] = new_a[i][j];
        b[i][j] = new_b[i][j];
        old_a[i][j] = a[i][j];
      }
    }
  }
  if (h == 1) {
    for (int from = 0; from < w; from++) {
      for (int to = 0; to < w; to++) {
        for (int i = 0; i < h; i++) {
          for (int j = 0; j < w; j++) {
            a[i][j] = old_a[i][j];
          }
        }
        vector < pair <int, int> > moves;
        int i = 0;
        int j = from;
        moves.push_back(make_pair(i, j));
        while (j < to) {
          swap(a[i][j], a[i][j + 1]);
          j++;
          moves.push_back(make_pair(i, j));
        }
        while (j > to) {
          swap(a[i][j], a[i][j - 1]);
          j--;
          moves.push_back(make_pair(i, j));
        }
        bool ok = true;
        for (int i = 0; i < h; i++) {
          for (int j = 0; j < w; j++) {
            if (a[i][j] != b[i][j]) {
              ok = false;
            }
          }
        }
        if (ok) {
          print(moves);
        }
      }
    }
    printf("%d\n", -1);
    return 0;
  }
  int x = -1, y = -1;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == b[h - 1][w - 1]) {
        add(i, j);
        x = i;
        y = j;
        break;
      }
    }
    if (x != -1) break;
  }
  while (x > 0) {
    add(x - 1, y);
    x--;
  }
  while (y > 0) {
    add(x, y - 1);
    y--;
  }
  vector < pair <int, int> > path;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i == h - 1 && j == w - 1) {
        continue;
      }
      int ri = -1, rj = -1;
      for (int pi = i; pi < h; pi++) {
        for (int pj = (pi > i ? 0 : j + 1); pj < w; pj++) {
          if (a[pi][pj] == b[i][j]) {
            ri = pi;
            rj = pj;
            break;
          }
        }
        if (ri != -1) break;
      }
      if (rj < j) {
        while (y > rj) {
          path.push_back(make_pair(x, y));
          add(x, y - 1);
          y--;
        }
        while (x < ri - 1) {
          path.push_back(make_pair(x, y));
          add(x + 1, y);
          x++;
        }
        while (y < j) {
          add(x + 1, y);
          add(x + 1, y + 1);
          add(x, y);
          rj++;
          path.push_back(make_pair(x, y));
          add(x, y + 1);
          y++;
        }
        for (int u = (int)path.size() - 1; u >= 0; u--) {
          add(path[u].first, path[u].second);
          x = path[u].first;
          y = path[u].second;
        }
        path.clear();
      }
      if (rj > j) {
        if (ri > i) {
          while (y < rj) {
            path.push_back(make_pair(x, y));
            add(x, y + 1);
            y++;
          }
          while (x < ri - 1) {
            path.push_back(make_pair(x, y));
            add(x + 1, y);
            x++;
          }
          while (y > j) {
            add(x + 1, y);
            add(x + 1, y - 1);
            add(x, y);
            rj--;
            path.push_back(make_pair(x, y));
            add(x, y - 1);
            y--;
          }
          for (int u = (int)path.size() - 1; u >= 0; u--) {
            add(path[u].first, path[u].second);
            x = path[u].first;
            y = path[u].second;
          }
          path.clear();
        } else {
          if (x > 0) {
            path.push_back(make_pair(x, y));
            add(x - 1, y);
            x--;
            while (y < rj) {
              path.push_back(make_pair(x, y));
              add(x, y + 1);
              y++;
            }
            while (y > j + 1) {
              add(x + 1, y);
              add(x + 1, y - 1);
              add(x, y);
              rj--;
              path.push_back(make_pair(x, y));
              add(x, y - 1);
              y--;
            }
          } else {
            path.push_back(make_pair(x, y));
            add(x + 1, y);
            x++;
            while (y < rj) {
              path.push_back(make_pair(x, y));
              add(x, y + 1);
              y++;
            }
            while (y > j + 1) {
              add(x - 1, y);
              add(x - 1, y - 1);
              add(x, y);
              rj--;
              path.push_back(make_pair(x, y));
              add(x, y - 1);
              y--;
            }
          }
          for (int u = (int)path.size() - 1; u >= 0; u--) {
            add(path[u].first, path[u].second);
            x = path[u].first;
            y = path[u].second;
          }
          path.clear();
          add(x, y + 1);
          y++;
        }
      }
      if (ri > i) {
        if (j == 0) {
          path.push_back(make_pair(x, y));
          add(x, y + 1);
          y++;
          while (x < ri) {
            path.push_back(make_pair(x, y));
            add(x + 1, y);
            x++;
          }
          while (x > i + 1) {
            add(x, y - 1);
            add(x - 1, y - 1);
            add(x, y);
            path.push_back(make_pair(x, y));
            add(x - 1, y);
            x--;
          }
          for (int u = (int)path.size() - 1; u >= 0; u--) {
            add(path[u].first, path[u].second);
            x = path[u].first;
            y = path[u].second;
          }
          path.clear();
          add(x + 1, y);
          x++;
        } else {
          path.push_back(make_pair(x, y));
          add(x, y - 1);
          y--;
          while (x < ri) {
            path.push_back(make_pair(x, y));
            add(x + 1, y);
            x++;
          }
          while (x > i + 1) {
            add(x, y + 1);
            add(x - 1, y + 1);
            add(x, y);
            path.push_back(make_pair(x, y));
            add(x - 1, y);
            x--;
          }
          for (int u = (int)path.size() - 1; u >= 0; u--) {
            add(path[u].first, path[u].second);
            x = path[u].first;
            y = path[u].second;
          }
          path.clear();
          add(x + 1, y);
          x++;
        }
        if (y < w - 1) {
          add(x - 1, y + 1);
          x--;
          y++;
        } else {
          while (y > 0) {
            add(x, y - 1);
            y--;
          }
        }
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      assert(a[i][j] == b[i][j]);
    }
  }
  print(moves);
  return 0;
}


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

#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define READER_BUF_SIZE 1048576
#define WRITER_BUF_SIZE 1048576
int reader_pt=READER_BUF_SIZE,reader_last;char reader_buf[READER_BUF_SIZE];
int writer_pt=0;char writer_buf[WRITER_BUF_SIZE];
#define mygc(c) {if(reader_pt==READER_BUF_SIZE)reader_pt=0,reader_last=fread(reader_buf,sizeof(char),READER_BUF_SIZE,stdin);(c)=reader_buf[reader_pt++];}
#define mypc(c) {if(writer_pt==WRITER_BUF_SIZE)writer_pt=0,fwrite(writer_buf,sizeof(char),WRITER_BUF_SIZE,stdout);writer_buf[writer_pt++]=(c);}
#define myed {fwrite(writer_buf,sizeof(char),writer_pt,stdout);writer_pt=0;}

#define ll long long
#define ull unsigned ll

void reader(int *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
void reader(int *x, int *y){reader(x);reader(y);}
void reader(int *x, int *y, int *z){reader(x);reader(y);reader(z);}
void reader(ll *x){int k,m=0;*x=0;for(;;){mygc(k);if(k=='-'){m=1;break;}if('0'<=k&&k<='9'){*x=k-'0';break;}}for(;;){mygc(k);if(k<'0'||k>'9')break;*x=(*x)*10+k-'0';}if(m)(*x)=-(*x);}
int reader(char c[]){int i,s=0;for(;;){mygc(i);if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF) break;}c[s++]=i;for(;;){mygc(i);if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF) break;c[s++]=i;}return s;}

void writer(int x, char c){int s=0,m=0;char f[10];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
void writer(ll x, char c){int s=0,m=0;char f[20];if(x<0)m=1,x=-x;while(x)f[s++]=x%10,x/=10;if(!s)f[s++]=0;if(m)mypc('-');while(s--)mypc(f[s]+'0');mypc(c);}
void writer(const char c[]){int i;for(i=0;c[i]!='\0';i++)mypc(c[i]);}

int sign(int x){
  if(x>0) return 1;
  if(x<0) return -1;
  return 0;
}

int ress;
int res1[1000100], res2[1000100];

int N, M;
int mp[33][33], goal[33][33];
int cnt[1000];

int dameX, dameY;
int dame[33][33];

void resadd(int a, int b){
  res1[ress] = a; res2[ress++] = b;
}

void domove(int sx, int sy, int nx, int ny){
  int dx, dy;

/*  int i, j;
  printf("call %d %d -> %d %d (%d %d)\n",sx,sy,nx,ny,N,M);
  printf("DAME %d %d\n",dameX,dameY);
  rep(i,N){ rep(j,M) printf("%d",dame[i][j]); puts(""); }
  puts("");

  rep(i,N){ rep(j,M) printf("%3d",mp[i][j]); puts(""); }
  puts("");
  rep(i,N){ rep(j,M) printf("%3d",goal[i][j]); puts(""); }
  puts("");
  puts("");
  fflush(stdout);*/

  while(sx != nx || sy != ny){
    dx = sign(nx - sx);
    dy = sign(ny - sy);

    if(dame[sx+dx][sy+dy] || (dameX==sx+dx && dameY==sy+dy)){
      if(dx && dy) {
        dx = 0;
      } else {
        if(dx==0){
          if(sx) dx = -1; else dx = 1;
        } else {
          if(sy) dy = -1; else dy = 1;
        }
      }
    }

    if(dame[sx+dx][sy+dy] || (dameX==sx+dx && dameY==sy+dy)){
      dx = sign(nx - sx);
      dy = sign(ny - sy);
      dx *= -1;
      if((sx+dx<0||sx+dx>=N) || dame[sx+dx][sy+dy] || (dameX==sx+dx && dameY==sy+dy)) dx *= -1, dy *= -1;
    }

//    printf("%d %d : %d %d -> %d %d\n",N,M,sx,sy,sx+dx,sy+dy); fflush(stdout);
    assert(!(sx+dx<0||sx+dx>=N));
    assert(!(sy+dy<0||sy+dy>=M));
    assert(!(dame[sx+dx][sy+dy] || (dameX==sx+dx && dameY==sy+dy)));

    resadd(sx+dx, sy+dy);
    swap(mp[sx][sy], mp[sx+dx][sy+dy]);
    sx += dx; sy += dy;
  }
}

void domove2(int *sx, int *sy, int nx, int ny){
  domove(*sx, *sy, nx, ny);
  *sx = nx;
  *sy = ny;
}

int isOK(void){
  int i, j;
  rep(i,N) rep(j,M) if(mp[i][j]!=goal[i][j]) return 0;
  return 1;
}

void findnear(int sx, int sy, int val, int *tx, int *ty, int curx, int cury){
  int i, j, k, d;
  int opt = 100000;

  rep(i,N) rep(j,M){
    if(dame[i][j]) continue;
    if(val != mp[i][j]) continue;
    if(i==curx && j==cury) continue;

    d = (sx-i) * sign(sx-i) + (sy-j) * sign(sy-j);
    if(d < opt){
      opt = d;
      *tx = i;
      *ty = j;
    }
  }
}

void gogo(int ti, int tj, int *sx, int *sy){
  int mx, my;

//  printf("%d %d\n",N,M);
    dameX = dameY = -1;
    domove2(sx, sy, 0, 0);
//  printf("%d %d\n",N,M);

  for(;;){
    findnear(ti,tj,goal[ti][tj],&mx,&my,*sx,*sy);
    if(mx==ti && my==tj){
      dame[ti][tj] = 1;
      break;
    }

    dameX = mx;
    dameY = my;
    domove2(sx, sy, ti, tj);

    dameX = dameY = -1;
    domove2(sx, sy, mx, my);
  }
}

int main(){
  int i, j, k, oN, oM;
  int ti, tj;
  int sx, sy, mx, my;
  int ok;

  reader(&N,&M); oN=N; oM=M;
  rep(i,N) rep(j,M) reader(mp[i]+j);
  rep(i,N) rep(j,M) reader(goal[i]+j);

  rep(i,N) rep(j,M) cnt[mp[i][j]]++;
  rep(i,N) rep(j,M) cnt[goal[i][j]]--;

  rep(i,1000) if(cnt[i]) break;
  if(i < 1000){
    writer("-1\n");
    myed;
    return 0;
  }

  if(N==1){
    ok = 0;
    dameX = dameY = -1;
    rep(i,M) rep(j,M) if(!ok){
      ress = 0;
      resadd(0,i);
      domove(0,i,0,j);
      if(isOK()){
        ok = 1;
      } else {
        domove(0,j,0,i);
        ress = 0;
      }
    }
  } else if(M==1){
    ok = 0;
    dameX = dameY = -1;
    rep(i,N) rep(j,N) if(!ok){
      ress = 0;
      resadd(i,0);
      domove(i,0,j,0);
      if(isOK()){
        ok = 1;
      } else {
        domove(j,0,i,0);
        ress = 0;
      }
    }
  } else {
    rep(i,N) rep(j,M) if(goal[0][0]==mp[i][j]) sx = i, sy = j;
    resadd(sx,sy);

    while(N > 2 || M > 2){
      if(N >= M){
        ti = N-1;
        for(tj = M-1; tj >= 0; tj--){
          gogo(ti,tj,&sx,&sy);
        }
        N--;
      } else {
        tj = M-1;
        for(ti = N-1; ti >= 0; ti--){
          gogo(ti,tj,&sx,&sy);
        }
        M--;
      }
    }

    // N = M = 2
    dameX = dameY = -1;
    domove2(&sx, &sy, 0, 0);

    for(;;){
      int fg1 = 0, fg2 = 0, fg3 = 0;
      if(mp[0][1]!=goal[0][1]) fg1++;
      if(mp[1][0]!=goal[1][0]) fg2++;
      if(mp[1][1]!=goal[1][1]) fg3++;

      if(fg1+fg2+fg3==0) break;

      if(fg1+fg2+fg3==3){
        domove2(&sx, &sy, 0, 1);
        domove2(&sx, &sy, 1, 0);
        domove2(&sx, &sy, 1, 1);
        domove2(&sx, &sy, 0, 0);
        continue;
      }

      if(fg1+fg2==2){
        domove2(&sx, &sy, 0, 1);
        domove2(&sx, &sy, 1, 0);
        domove2(&sx, &sy, 0, 0);
        continue;
      }

      if(fg1+fg3==2){
        domove2(&sx, &sy, 0, 1);
        domove2(&sx, &sy, 1, 1);
        domove2(&sx, &sy, 0, 0);
        continue;
      }

      if(fg2+fg3==2){
        domove2(&sx, &sy, 1, 0);
        domove2(&sx, &sy, 1, 1);
        domove2(&sx, &sy, 0, 0);
        continue;
      }
    }
  }

  writer(ress-1,'\n');
  rep(i,ress) writer(res1[i]+1,' '), writer(res2[i]+1,'\n');

/*  if(ress > 0){
    rep(i,oN) rep(j,oM) assert(mp[i][j] == goal[i][j]);
  }*/

  myed;
  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

#define _int64 long long

int neigh[8][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};

int a1[50][50];
int a2[50][50];
int a3[50][50];
int cnt[1000];
vector<pair<int,int> > ans;
vector<pair<int,pair<int,int> > > v1,v2;
pair<int,int> d[50][50];
pair<int,int> pos[50][50];
pair<int,int> p,p1,p2;
int mlim,nlim;

void move2(pair<int,int> tar,pair<int,int> forbid)
{
  int i,x,y,k,bk,bd;
  vector<pair<int,int> > seq;
  if (tar==p) return;
    //cout<<"move2:0:"<<p.first<<" "<<p.second<<" "<<tar.first<<" "<<tar.second<<" "<<forbid.first<<" "<<forbid.second<<endl;
  seq.clear();
  seq.push_back(tar);
  while (tar!=p)
  {
    bd=2000000000;
    for (k=0;k<8;k++)
    {
      x=tar.first+neigh[k][0];
      y=tar.second+neigh[k][1];
      if ((x>=0)&&(x<=mlim)&&(y>=0)&&(y<=nlim)&&((x!=forbid.first)||(y!=forbid.second)))
      {
        if (abs(x-p.first)+abs(y-p.second)<bd)
        {
          bd=abs(x-p.first)+abs(y-p.second);
          bk=k;
        }
      }
    }
    //cout<<bd<<endl;
    tar.first+=neigh[bk][0];
    tar.second+=neigh[bk][1];
    seq.push_back(tar);
    //cout<<tar.first<<" "<<tar.second<<endl;
  }
  for (i=seq.size()-2;i>=0;i--)
  {
    //cout<<"move2:"<<seq[i].first<<" "<<seq[i].second<<endl;
    ans.push_back(seq[i]);
    swap(d[p.first][p.second],d[seq[i].first][seq[i].second]);
    pos[d[p.first][p.second].first][d[p.first][p.second].second]=p;
    pos[d[seq[i].first][seq[i].second].first][d[seq[i].first][seq[i].second].second]=seq[i];
    p=seq[i];
  }
}

void move1(pair<int,int> pos,pair<int,int> tarpos)
{
  int i;
  vector<pair<int,int> > seq;
  if (pos==tarpos) return;
    //cout<<"move1:0:"<<pos.first<<" "<<pos.second<<" "<<tarpos.first<<" "<<tarpos.second<<endl;
  seq.clear();
  seq.push_back(tarpos);
  while (tarpos!=pos)
  {
    pair<int,int> tmp=tarpos;
    if (tmp.first!=pos.first)
      tmp.first+=(pos.first-tmp.first)/abs(pos.first-tmp.first);
    if (tmp.second!=pos.second)
      tmp.second+=(pos.second-tmp.second)/abs(pos.second-tmp.second);
    //cout<<tmp.first<<" "<<tmp.second<<endl;
    tarpos=tmp;
    seq.push_back(tarpos);
  }
  for (i=seq.size()-1;i>0;i--)
  {
    //cout<<"move1:1:"<<seq[i-1].first<<" "<<seq[i-1].second<<endl;
    move2(seq[i-1],seq[i]);
    //cout<<"move1:2:"<<seq[i].first<<" "<<seq[i].second<<endl;
    move2(seq[i],make_pair(-1,-1));
  }
}

int main()
{
  int i,j,k,l,m,n,dire,b1,x,y;
  scanf("%d%d",&m,&n);
  memset(cnt,0,sizeof(cnt));
  for (i=0;i<m;i++)
    for (j=0;j<n;j++)
    {
      scanf("%d",&a1[i][j]);
      cnt[a1[i][j]]++;
    }
  for (i=0;i<m;i++)
    for (j=0;j<n;j++)
    {
      scanf("%d",&a2[i][j]);
      cnt[a2[i][j]]--;
    }
  for (i=1;i<=900;i++)
    if (cnt[i]!=0)
    {
      printf("-1\n");
      return 0;
    }
  ans.clear();
  if (m==1)
  {
    b1=0;
    for (i=0;i<n;i++)
    {
      for (j=0;j<n;j++)
        if (a1[0][i]==a2[0][j])
        {
          if (i==j) continue;
          for (k=0;k<n;k++)
            a3[0][k]=a1[0][k];
          dire=(j-i)/abs(j-i);
          for (k=i;k!=j;k+=dire)
          {
            swap(a3[0][k],a3[0][k+dire]);
          }
          b1=1;
          for (k=0;k<n;k++)
            if (a2[0][k]!=a3[0][k]) b1=0;

          if (b1==1)
          {
            for (k=i;k!=j+dire;k+=dire)
              ans.push_back(make_pair(0,k));
          }
        }
      if (b1==1) break;
    }
    if (b1==0)
    {
      printf("-1\n");
      return 0;
    }
  }
  else if (n==1)
  {
    b1=0;
    for (i=0;i<m;i++)
    {
      for (j=0;j<m;j++)
        if (a1[i][0]==a2[j][0])
        {
          if (i==j) continue;
          for (k=0;k<m;k++)
            a3[k][0]=a1[k][0];
          dire=(j-i)/abs(j-i);
          for (k=i;k!=j;k+=dire)
          {
            swap(a3[k][0],a3[k+dire][0]);
          }
          b1=1;
          for (k=0;k<m;k++)
            if (a2[k][0]!=a3[k][0]) b1=0;

          if (b1==1)
          {
            for (k=i;k!=j+dire;k+=dire)
              ans.push_back(make_pair(k,0));
          }
        }
      if (b1==1) break;
    }
    if (b1==0)
    {
      printf("-1\n");
      return 0;
    }
  }
  else
  {
    v1.clear();
    v2.clear();
    for (i=0;i<m;i++)
      for (j=0;j<n;j++)
      {
        v1.push_back(make_pair(a1[i][j],make_pair(i,j)));
        v2.push_back(make_pair(a2[i][j],make_pair(i,j)));
      }
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    for (i=0;i<v1.size();i++)
    {
      d[v1[i].second.first][v1[i].second.second]=v2[i].second;
      pos[v2[i].second.first][v2[i].second.second]=v1[i].second;
    }
    p=pos[0][0];
    ans.push_back(p);
    while ((m>2)||(n>2))
    {
      if (m>n)
      {
        mlim=m-1;
        nlim=n;
        for (i=0;i<n;i++)
        {
          p1=pos[m-1][i];
          move1(p1,make_pair(m-1,i));
        }
        m--;
      }
      else
      {
        nlim=n-1;mlim=m;
        for (i=0;i<m;i++)
        {
          p1=pos[i][n-1];
          move1(p1,make_pair(i,n-1));
        }
        n--;
      }
    }
    mlim=2;
    nlim=2;
    //cout<<pos[1][0].first<<" "<<pos[1][0].second<<endl;
    move1(pos[1][0],make_pair(1,0));
    move1(pos[1][1],make_pair(1,1));
    move1(pos[0][1],make_pair(0,1));
  }
  printf("%d\n",(int)ans.size()-1);
  for (i=0;i<ans.size();i++)
    printf("%d %d\n",ans[i].first+1,ans[i].second+1);
  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

#define _int64 long long

int neigh[8][2]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};

int a1[50][50];
int a2[50][50];
int a3[50][50];
int cnt[1000];
vector<pair<int,int> > ans;
vector<pair<int,pair<int,int> > > v1,v2;
pair<int,int> d[50][50];
pair<int,int> pos[50][50];
pair<int,int> p,p1,p2;
int mlim,nlim;

void move2(pair<int,int> tar,pair<int,int> forbid)
{
  int i,x,y,k,bk,bd;
  vector<pair<int,int> > seq;
  if (tar==p) return;
    //cout<<"move2:0:"<<p.first<<" "<<p.second<<" "<<tar.first<<" "<<tar.second<<" "<<forbid.first<<" "<<forbid.second<<endl;
    //cout<<mlim<<" "<<nlim<<endl;
  seq.clear();
  seq.push_back(tar);
  while (tar!=p)
  {
    bd=2000000000;
    for (k=0;k<8;k++)
    {
      x=tar.first+neigh[k][0];
      y=tar.second+neigh[k][1];
      if (p==make_pair(x,y))
      {
        bk=k;break;
      }
      if ((x>=0)&&(x<mlim)&&(y>=0)&&(y<nlim)&&((x!=forbid.first)||(y!=forbid.second)))
      {
        if (abs(x-p.first)+abs(y-p.second)<bd)
        {
          bd=abs(x-p.first)+abs(y-p.second);
          bk=k;
        }
      }
    }
    //cout<<bd<<endl;
    tar.first+=neigh[bk][0];
    tar.second+=neigh[bk][1];
    seq.push_back(tar);
    //cout<<tar.first<<" "<<tar.second<<endl;
  }
  for (i=seq.size()-2;i>=0;i--)
  {
    //cout<<"move2:"<<seq[i].first<<" "<<seq[i].second<<endl;
    ans.push_back(seq[i]);
    swap(d[p.first][p.second],d[seq[i].first][seq[i].second]);
    pos[d[p.first][p.second].first][d[p.first][p.second].second]=p;
    pos[d[seq[i].first][seq[i].second].first][d[seq[i].first][seq[i].second].second]=seq[i];
    p=seq[i];
  }
  seq.clear();
}

void move1(pair<int,int> pos,pair<int,int> tarpos)
{
  int i;
  vector<pair<int,int> > seq;
  if (pos==tarpos) return;
    //cout<<"move1:0:"<<pos.first<<" "<<pos.second<<" "<<tarpos.first<<" "<<tarpos.second<<endl;
  seq.clear();
  seq.push_back(tarpos);
  while (tarpos!=pos)
  {
    pair<int,int> tmp=tarpos;
    if (tmp.first!=pos.first)
      tmp.first+=(pos.first-tmp.first)/abs(pos.first-tmp.first);
    if (tmp.second!=pos.second)
      tmp.second+=(pos.second-tmp.second)/abs(pos.second-tmp.second);
    //cout<<tmp.first<<" "<<tmp.second<<endl;
    tarpos=tmp;
    seq.push_back(tarpos);
  }
  for (i=seq.size()-1;i>0;i--)
  {
    //cout<<"move1:1:"<<seq[i-1].first<<" "<<seq[i-1].second<<endl;
    move2(seq[i-1],seq[i]);
    //cout<<"move1:2:"<<seq[i].first<<" "<<seq[i].second<<endl;
    move2(seq[i],make_pair(-1,-1));
  }
  seq.clear();
}

int main()
{
  int i,j,k,l,m,n,dire,b1,x,y;
  scanf("%d%d",&m,&n);
  memset(cnt,0,sizeof(cnt));
  for (i=0;i<m;i++)
    for (j=0;j<n;j++)
    {
      scanf("%d",&a1[i][j]);
      cnt[a1[i][j]]++;
    }
  for (i=0;i<m;i++)
    for (j=0;j<n;j++)
    {
      scanf("%d",&a2[i][j]);
      cnt[a2[i][j]]--;
    }
  for (i=1;i<=900;i++)
    if (cnt[i]!=0)
    {
      printf("-1\n");
      return 0;
    }
  ans.clear();
  if (m==1)
  {
    b1=0;
    for (i=0;i<n;i++)
    {
      for (j=0;j<n;j++)
        if (a1[0][i]==a2[0][j])
        {
          if (i==j) continue;
          for (k=0;k<n;k++)
            a3[0][k]=a1[0][k];
          dire=(j-i)/abs(j-i);
          for (k=i;k!=j;k+=dire)
          {
            swap(a3[0][k],a3[0][k+dire]);
          }
          b1=1;
          for (k=0;k<n;k++)
            if (a2[0][k]!=a3[0][k]) b1=0;

          if (b1==1)
          {
            for (k=i;k!=j+dire;k+=dire)
              ans.push_back(make_pair(0,k));
          }
        }
      if (b1==1) break;
    }
    if (b1==0)
    {
      printf("-1\n");
      return 0;
    }
  }
  else if (n==1)
  {
    b1=0;
    for (i=0;i<m;i++)
    {
      for (j=0;j<m;j++)
        if (a1[i][0]==a2[j][0])
        {
          if (i==j) continue;
          for (k=0;k<m;k++)
            a3[k][0]=a1[k][0];
          dire=(j-i)/abs(j-i);
          for (k=i;k!=j;k+=dire)
          {
            swap(a3[k][0],a3[k+dire][0]);
          }
          b1=1;
          for (k=0;k<m;k++)
            if (a2[k][0]!=a3[k][0]) b1=0;

          if (b1==1)
          {
            for (k=i;k!=j+dire;k+=dire)
              ans.push_back(make_pair(k,0));
          }
        }
      if (b1==1) break;
    }
    if (b1==0)
    {
      printf("-1\n");
      return 0;
    }
  }
  else
  {
    v1.clear();
    v2.clear();
    for (i=0;i<m;i++)
      for (j=0;j<n;j++)
      {
        v1.push_back(make_pair(a1[i][j],make_pair(i,j)));
        v2.push_back(make_pair(a2[i][j],make_pair(i,j)));
      }
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    for (i=0;i<v1.size();i++)
    {
      d[v1[i].second.first][v1[i].second.second]=v2[i].second;
      pos[v2[i].second.first][v2[i].second.second]=v1[i].second;
    }
    p=pos[0][0];
    ans.push_back(p);
    while ((m>2)||(n>2))
    {
      if (m>n)
      {
        mlim=m-1;
        nlim=n;
        for (i=0;i<n;i++)
        {
          p1=pos[m-1][i];
          move1(p1,make_pair(m-1,i));
        }
        m--;
      }
      else
      {
        nlim=n-1;mlim=m;
        for (i=0;i<m;i++)
        {
          p1=pos[i][n-1];
          move1(p1,make_pair(i,n-1));
        }
        n--;
      }
    }
    mlim=2;
    nlim=2;
    //cout<<pos[1][0].first<<" "<<pos[1][0].second<<endl;
    move1(pos[1][0],make_pair(1,0));
    move1(pos[1][1],make_pair(1,1));
    move1(pos[0][1],make_pair(0,1));
  }
  printf("%d\n",(int)ans.size()-1);
  for (i=0;i<ans.size();i++)
    printf("%d %d\n",ans[i].first+1,ans[i].second+1);
  return 0;
}