import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } string COLOR(string s = "") { return "\x1b[" ~ s ~ "m"; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum D = [0, 31,29,31,30,31,30,31,31,30,31,30,31]; int U; int[5][5][] graph; string[] query, answer; int newNode() { const u = U++; graph.length += 1; query ~= "?"; answer ~= "?"; return u; } int dfs(int dep, string[] ss) { const u = newNode; if (ss.length <= 1) { answer[u] = ss.length ? ss[0] : "empty"; } else { assert(dep < 6); int mn = int.max; int qm = -1; foreach (q; 0 .. 10^^4) { const r = format("%04d", q); bool ok = true; foreach (i; 0 .. 4) foreach (j; i + 1 .. 4) ok = ok && (r[i] != r[j]); if (ok) { string[][5][5] tss; foreach (s; ss) { int hit, blow; foreach (i; 0 .. 4) foreach (j; 0 .. 4) if (r[i] == s[j]) (i == j) ? ++hit : ++blow; tss[hit][blow] ~= s; } int mx; foreach (hit; 0 .. 5) foreach (blow; 0 .. 5) chmax(mx, cast(int)(tss[hit][blow].length)); if (chmin(mn, mx)) qm = q; } } { const q = qm; const r = format("%04d", q); query[u] = r; // stderr.writeln(dep, " ", ss, ": ", r); bool ok = true; foreach (i; 0 .. 4) foreach (j; i + 1 .. 4) ok = ok && (r[i] != r[j]); if (ok) { string[][5][5] tss; foreach (s; ss) { int hit, blow; foreach (i; 0 .. 4) foreach (j; 0 .. 4) if (r[i] == s[j]) (i == j) ? ++hit : ++blow; tss[hit][blow] ~= s; } foreach (hit; 0 .. 5) foreach (blow; 0 .. 5) { const v = dfs(dep + 1, tss[hit][blow]); graph[u][hit][blow] = v; } } } } return u; } void main() { string[] ini; foreach (m; 1 .. 12 + 1) foreach (d; 1 .. D[m] + 1) { const s = format("%02d%02d", m, d); bool ok = true; foreach (i; 0 .. 4) foreach (j; i + 1 .. 4) ok = ok && (s[i] != s[j]); if (ok) { // stderr.writeln(s); ini ~= s; } } // stderr.writeln("|ini| = ", ini.length); const rt = dfs(0, ini); const numCases = readInt; foreach (caseId; 0 .. numCases) { int u = rt; for (; answer[u] == "?"; ) { writefln("? %s", query[u]); stdout.flush; const H = readInt; const B = readInt; if (H == -1 && B == -1) return; // if (H == -1 && B == -1) assert(false); u = graph[u][H][B]; } writefln("! %s", answer[u]); stdout.flush; const A = readInt; if (A == -1) return; // if (A == -1) assert(false); } }