Sorry but now I have not enough time to explain the approach.

I hope the code is clear enough.

Otherwise, I will go back here later.

```
public static long solve(int from, int n, long m, long[] cnt) {
if(m < 0)
return -1;
if(from == -1)
return 0;
long done = 0;
for(int e = from; e >= 0; e --) {
long kk = solve(e - 1, n, m - ((1L << e) * (n - cnt[e]) + done), cnt);
if(kk == -1) {
done += (1L << e) * cnt[e];
continue;
}
return (1L << e) | kk;
}
return done <= m ? 0 : -1;
}
public static void main(String[] args) throws IOException {
final int MAXE = 50;
int t = in.nextInt();
for(int tc = 1; tc <= t; tc ++) {
int n = in.nextInt();
long m = in.nextLong();
long[] a = in.nextLongArray(n);
long[] cnt = new long[MAXE + 1];
for(int e = MAXE; e >= 0; e --)
for(long ai : a)
if((ai & (1L << e)) != 0)
cnt[e] ++;
out.write("Case #" + tc + ": " + solve(MAXE, n, m, cnt) + "\n");
}
out.flush();
}
```