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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "dzilib.h"
#include <bits/stdc++.h>

typedef long long int lli;

using namespace std;

#define MAX4 10010
#define MAX5 100010
#define MAX6 1000010

#define FOR(n) for (int i = 0; i < n; i++)

using vti = vector<int>;
using vts = vector<string>;

template <typename T> void dbg(T last) { cout << last << "\n"; }

template <typename T, typename... args> void dbg(T current, args... next) {
  cout << current << ' ';
  dbg(next...);
}

template <typename T> void getInput(vector<T> &v, int n, int x = 0) {
  for (int i = x; i < n; i++) {
    cin >> v[i];
  }
}

//-----------------------------------------------------------------------------------------------//
const int LIM = 1000101;
vector<int> primes;
vector<int> divisors(LIM + 1);

map<vector<int>, vector<int>> m;
void init1() {
  int x = sqrt(LIM);
  vector<int> vis(LIM + 1, true);
  vis[1] = false;
  for (int i = 2; i <= x; i++) {
    if (vis[i]) {
      for (int j = i * 2; j <= LIM; j += i) {
        vis[j] = false;
      }
    }
  }

  for (int i = 1; i <= LIM; i++) {
    if (vis[i]) {
      primes.push_back(i);
    }
  }

  for (int i = 1; i <= LIM; i++) {
    for (int j = i; j <= LIM; j += i) {
      divisors[j]++;
    }
  }

  for (int x : primes) {
    vector<int> v;
    for (int i = 1; i <= 5; i++) {
      v.push_back(divisors[x - i > 0 ? x - i : -1]);
    }
    // if (x == 1051) {
    //   for (int z : v) {
    //     cout << z << " ";
    //   }
    //   cout << "\n\n\n";
    // }
    m[v].push_back(x);
  }
}

void solve() {
  int q = GetQ();
  long long c = GetC();
  long long n = GetN();

  vector<int> tmp;
  // ile dodac zeby meic liczbe pierwsza
  int x;

  for (int i = 50; i < 1000; i++) {
    int x1 = Ask(i);
    x = i;
    tmp.push_back(x1);
    // dbg(x, divisors[1000 + i], x1, " + ");
    // assert(divisors[i] == x1);
    if (x1 == 2) {
      break;
    }
  }
  // dbg("x", x);
  int cnt = tmp.size() - 2;
  vector<int> tmp2;
  while (cnt >= 0) {
    // dbg("& ", tmp.size(), cnt, tmp[cnt]);
    tmp2.push_back(tmp[cnt]);
    if (tmp2.size() == 5) {
      break;
    }
    cnt--;
  }
  int cnt2 = tmp2.size();
  // dbg("cnt2", cnt2);
  while (tmp2.size() < 5) {
    cnt2++;
    tmp2.push_back(x - cnt2 > 0 ? Ask(x - cnt2) : -1);
  }

  // dbg("cnt22", tmp2.size());

  auto &prx = m[tmp2];
  set<int> pr;
  for (int z : prx) {
    pr.insert(z);
  }
  // dbg("size", tmp.size(), tmp2.size(), pr.size());
  // for (int o : tmp2) {
  //   dbg("*", o);
  // }
  assert(prx.size() > 0);
  if (prx.size() == 1) {
    Answer(prx[0] - x);
    return;
  }
  while (true) {
    cnt2++;
    int nxt = Ask(x - cnt2);
    int howMany = 0, an;
    set<int> pr2;
    for (auto p : pr) {
      if (divisors[p - cnt2] == nxt) {
        howMany++;
        an = p;
        pr2.insert(p);
      }
    }
    if (howMany == 1) {
      Answer(an - x);
      return;
    }
    swap(pr, pr2);
  }

  // dbg(primes.size(), m.size());

  // dbg(primes.size());
  // for (int x : primes) {
  //   cout << x << ", ";
  // }
}

int main() {
  std::ios::sync_with_stdio(false);
  init1();
  int t = GetT();
  // std::cout << t << "\n";

  while (t--) {
    solve();
  }

  return 0;
}