#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MOD = 1e9 + 7;
void solve(){
ll n, k;
cin >> n >> k;
vector<pair<int, int>> a(n);
for(int i = 0; i < n; i++){
cin >> a[i].first;
}
for(int i = 0; i < n; i++){
cin >> a[i].second;
}
sort(a.begin(), a.end());
ll ans = 0;
int idx = -1;
for(int i = n - 1; i >= 0; i--){
if(a[i].second == 1){
if(i > (n - 1) / 2 - (n % 2)){
ans = max(ans, a[i].first + a[(n - 1) / 2 - (n % 2)].first + k);
}else{
ans = max(ans, a[i].first + k + a[(n - 1) / 2 + !(n % 2)].first);
}
}else{
idx = max(idx, i);
}
}
if(idx == -1){
cout << ans << "\n";
return;
}
ll s = 0, e = 1e10;
while(s <= e){
ll rem = k;
ll mid = s + (e - s) / 2;
vector<pair<int, int>> b;
for(int i = 0; i< n; i++){
if(i == idx)continue;
b.push_back(a[i]);
}
for(int i = n - 2; i >= 0; i--){
if(b[i].second == 1 && b[i].first < mid){
ll p = min(rem, mid - b[i].first);
b[i].first += p;
rem -= p;
}
}
sort(b.begin(), b.end());
if(b[(n - 2) / 2].first >= mid){
ans = max(ans, a[idx].first + mid);
s = mid + 1;
}else{
e = mid - 1;
}
}
cout << ans << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
cin >> t;
for(int i = 1; i <= t; i++){
solve();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGwgbG9uZyBsb25nCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1PRCA9IDFlOSArIDc7Cgp2b2lkIHNvbHZlKCl7CglsbCBuLCBrOwoJY2luID4+IG4gPj4gazsKCQoJdmVjdG9yPHBhaXI8aW50LCBpbnQ+PiBhKG4pOwoJCglmb3IoaW50IGkgPSAwOyBpIDwgbjsgaSsrKXsKCQljaW4gPj4gYVtpXS5maXJzdDsKCX0KCQoJZm9yKGludCBpID0gMDsgaSA8IG47I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