#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
typedef pair<int,int> P;
string itos(int n){
  stringstream ss;
  ss<<n;
  return ss.str();
}

int main(){
  vector<string> cand;
  FOR(n,123,10000){
    string s = itos(n);
    if(s.size() < 4) s = "0" + s;
    set<char> cnt;
    REP(i,4)cnt.insert(s[i]);
    if(cnt.size() == 4)cand.push_back(s);
  }
  int N;
  while(cin>>N, N){
    vector<string> input(N);
    vector<int> hit(N),brow(N);
    REP(i,N)cin>>input[i]>>hit[i]>>brow[i];
    vector<string> ans;
    FORIT(it, cand){
      string &s = *it;
      bool ok = true;
      REP(i,N){
        int h = 0, b = 0;
        REP(j,4)REP(k,4)if(s[j]==input[i][k]){
          if(j == k) h++;
          else b++;
        }
        if(h != hit[i] || b != brow[i]){
          ok = false;
          break;
        }
      }
      if(ok){
        ans.push_back(s);
      }
    }
    vector<string> ans2;
    FORIT(it, cand){
      string &s = *it;
      set<P> pset;
      REP(i,ans.size()){
        int h = 0, b = 0;
        REP(j,4)REP(k,4)if(s[j]==ans[i][k]){
          if(j == k) h++;
          else b++;
        }
        pset.insert(P(h,b));
      }
      if(pset.size() == ans.size()){
        ans2.push_back(s);
      }
    }
    if(ans.size() == 1) cout<<ans.front()<<endl;
    else if(ans2.size() > 0) cout<<ans2.front()<<endl;
    else cout<<"????"<<endl;
  }

  return 0;
}