1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <bits/stdc++.h>
#include "dzilib.h"
      
#define ll long long
#define fors(u, n, s) for(ll u = (s); u < (n); u++)
#define foru(u, n) fors(u, n, 0)
#define vec vector
#define pb push_back
#define f first
#define s second
#define ir(a, b, x) (((a) <= (x)) && ((x) <= (b)))
#define pint pair<int, int>
#define us unsigned
 
using namespace std;
     
long long numberOfDivisors(long long num) {
    long long total = 1;
    for (int i = 2; (long long)i * i <= num; i++) {
        if (num % i == 0) { 
            int e = 0;
            do {
                e++;
                num /= i;
            } while (num % i == 0);
            total *= e + 1;
        }
    }
    if (num > 1) {
        total *= 2;
    }
    return total;
}

const ll N = 1e14;
ll t, n, q, c;

bool possibly(int x, int p){
	fors(i, 50, p){
		if(x%p==0) return true;
	}
	return false;
}

int cnt = 0;

map<ll, int> mem;

ll random_offset;

int ask(ll x){
	x+=random_offset;
	if(mem.find(x) != mem.end()) return mem[x];
	cnt ++;
	mem[x] = Ask(x);
	return mem[x];
}

int find_pow_of_two(int p, ll start, ll step){
	ll d = 1LL<<p;
	ll m = d/step;
	vec<bool> possible_mod(m, true);
	vec<int> last_seen(m, 0);
	for(int i = 0; true; i++){
		if(!possible_mod[i%m]) continue;
		ll ans = ask(start+step*i);
		if(numberOfDivisors(ans) == 2) {
		}
		if(ans%(p+1)==0){
			if(numberOfDivisors(ans/(p+1)) == 2) {
			}
			last_seen[i%m]=0;
		}else{
			last_seen[i%m]++;
			if(last_seen[i%m]==2) possible_mod[i%m]=false;
		}
		int sum = 0;
		for(auto i : possible_mod) sum += i;
		if(sum == 1) break;
	}

	foru(i, d) if(possible_mod[i]) {
		start += step*i;
		while(start>=step*m) start -= step*m;
		return start;
	}
}

vec<int> p = {3, 5, 7, 9};

vec<int> dividors(int x){
	vec<int> out;
	fors(i, x, 1) if(x%i==0) out.pb(i);
	return out;
}

void solve(){
	mem.clear();
	foru(_i, 10){
		random_offset += ((ll)rand())*rand();
		random_offset %= n/2;
	}

	ll x = 0;

	x = find_pow_of_two(p[0]-1, x, 1);
	for(int i = 1; i < p.size(); i++){
		x = find_pow_of_two(p[i]-1, x, 1LL<<(p[i-1]-1));
	}

	int current_power = p[p.size()-1]-1;

	while(n+random_offset+x >= 2*(1L<<current_power)){
		x += (1LL<<current_power);
		vec<int> divs = dividors(ask(x));
		foru(i, divs.size()){
			if (divs[i] >= current_power+1){
				if(divs[i] == current_power + 1) break;
				current_power = divs[i]-1;
				while(x >= (1LL<<current_power)) x-=(1LL<<current_power);
				break;
			}
		}
	}

	Answer((1LL<<current_power)-x-random_offset);
}

int main(){

	t=GetT();
	n=GetN();
	q=GetQ();
	c=GetC();

	foru(_i, t) solve();
}