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
138
139
140
141
142
143
144
#ifndef NDEBUG
  #ifdef EMBE_DEBUG
    #include "embe-debug.hpp"
  #else
    #define NDEBUG
  #endif
#endif
#ifdef NDEBUG
  #define LOG_INDENT(...) do {} while (false)
  #define LOG(...) do {} while (false)
  #define DUMP(...) do {} while (false)
#endif

#include <iostream>
#include <vector>
#include <map>
#include <cassert>
#include <ranges>
using namespace std;

#include "dzilib.h"

namespace {

long long mod(long long a, long long b)
{
  if (a >= 0) return a % b;
  return (a % b + b) % b;
}

map<long long, long long> cache;

long long cached_ask(long long x)
{
  if (cache.contains(x)) return cache[x];
  return cache[x] = Ask(x);
}

bool is_prime(int x)
{
  if (x < 2) return false;
  for (int d = 2; d <= x / d; ++d) {
    if (x % d == 0) return false;
  }
  return true;
}

long long find(long long offset, long long skip, int count, int exp)
{
  int div = exp + 1;
  bool fast = skip > (1LL << 20) && is_prime(div);
  int repeats = 0;
  vector<long long> cnts;
  auto start = offset;
  while (true) {
    ++repeats;
    vector<long long> candidates;
    for (int i = 0; i < count; ++i) {
      // DUMP(offset + i * skip + debug_peek());
      auto cur = offset + i * skip;
      auto cnt = cached_ask(cur);
      // DUMP(exp);
      // DUMP(cnt);
      cnts.push_back(cnt);
      // if (skip >= (1LL << 20) && exp < 22 && cnt % 23 == 0) {
      //   DUMP(23 - exp);
      //   assert((cur + debug_peek()) % (1LL << 22) == 0);
      //   return cur + (1LL << exp);
      // }
      // if (skip >= (1LL << 20) && exp < 28 && cnt % 29 == 0) {
      //   DUMP(29 - exp);
      //   return cur + (1LL << exp);
      // }
      if (cnt % div == 0) {
        candidates.emplace_back(cur);
      }
      if (fast && !candidates.empty()) return candidates[0];
    }
    assert(!candidates.empty());
    if (candidates.size() == 1) {
      if (repeats > 1) {
        DUMP(start, start+debug_peek(), skip, count, exp, candidates, cnts, repeats, (offset - start) / skip);
      }
      return candidates.front();
    }
    offset += skip * 13;
    if (candidates.size() == 2) {
      offset = candidates[candidates.size() - 2] + skip;
    }
  }
}

void jump(long long& offset, int& exp, int to)
{
  int jump = to - exp;
  assert((debug_peek() + offset) % (1LL << exp) == 0);
  offset = find(offset, 1LL << exp, 2 << jump, exp + jump);
  exp += jump;
  offset += (1LL << exp);
  ++exp;
}

long long solve(long long n, long long c)
{
  cache.clear();
  // long long offset = c * 4 / 5 - 320;
  // long long offset = 3*3*3*3*5*5*5*5 + 1;
  // long long offset = 6983776800 + 1;
  // long long offset = 1444035528936000 + 1;
  long long offset = 0;
  int exp = 0;

  // while ((debug_peek() + offset) % 8 != 0) ++offset; exp = 3;
  jump(offset, exp, 2);

  while ((1LL << exp) < n) {
    jump(offset, exp, exp + 1);
  }

  auto res = mod(-offset, (1LL << exp));
  if (res == 0) res = n;

  DUMP(cache.size());

  return res;
}

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int t = GetT();
  long long c = GetC();
  long long n = GetN();

  for (int i = 0; i < t; ++i) {
    Answer(solve(n, c));
  }

  return 0;
}