/*
* B. Not Number Theory | SRBD Code Contest - 2024 (Round 1)
* Author: Sohom Sahaun | @sohomsahaun | CF: sahaun
*/
#include <bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
using ll = long long;
const int MX = 1000000;
int bits[MX];
int ans;
void pre() {
int i;
for (i = 0; i < MX; ++i) {
bits[i] = __builtin_popcount(i);
}
}
void go(string& s, int ind) {
if (ind >= (int)s.size()) {
int num = stoi(s);
if (bits[num] == bits[ans]) {
ans = max(ans, num);
} else if (bits[num] > bits[ans]) {
ans = num;
}
return;
}
if (s[ind] != '?') {
go(s, ind+1);
return;
}
for (int i = (ind == 0) ? 1 : 0; i <= 9; ++i) {
string t = s;
t[ind] = (char)(i+'0');
go(t, ind+1);
}
}
int main() {
FAST;
pre();
int tc = 1, ti;
cin >> tc;
for (ti = 1; ti <= tc; ++ti) {
string s;
cin >> s;
ans = 0;
go(s, 0);
cout << ans << "\n";
}
return 0;
}
LyoKICogIEIuIE5vdCBOdW1iZXIgVGhlb3J5IHwgU1JCRCBDb2RlIENvbnRlc3QgLSAyMDI0IChSb3VuZCAxKQogKiAgQXV0aG9yOiBTb2hvbSBTYWhhdW4gfCBAc29ob21zYWhhdW4gfCBDRjogc2FoYXVuCiAqLwogCiNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBGQVNUIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpO2Npbi50aWUoTlVMTCk7CnVzaW5nIGxsID0gbG9uZyBsb25nOwoKY29uc3QgaW50IE1YID0gMTAwMDAwMDsKaW50IGJpdHNbTVhdOwppbnQgYW5zOwoKdm9pZCBwcmUoKSB7CiAgaW50IGk7CiAgZm9yIChpID0gMDsgaSA8IE1YOyArK2kpIHsKICAgIGJpdHNbaV0gPSBfX2J1aWx0aW5fcG9wY291bnQoaSk7CiAgfQp9Cgp2b2lkIGdvKHN0cmlu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