/*
 *  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;
}
