#include<bits/stdc++.h>
#include"dizlib.h"
#define ll long long
using namespace std;
int T,bit;
ll mx;
map<ll,ll> f;
inline int calc(ll x){return 63-__builtin_clzll(x);}
int getT();
ll getN();
ll getC();
ll Ask(ll x);
int getQ();
void Answer(ll x);
inline ll query(ll x){return f.count(x)?f[x]:f[x]=Ask(x);}
inline ll dfs(ll x,int nw)
{
if(nw == bit) return x;
ll res=0;
if(query(mx-x)%(nw+1) == 0) res|=dfs(x,nw+1);
if(query(mx-x-(1ll<<nw))%(nw+1) == 0) res|=dfs(x+(1ll<<nw),nw+1);
return res;
}
int main()
{
T=getT(),bit=calc(getN())+1;
getC(),getQ(),mx=1ll<<bit;
while(T--) f.clear(),Answer(dfs(0,0));
return 0;
}