#include <bits/stdc++.h>
using namespace std;
struct Data {
  int id, st, sst, sz;
};
Data step[10][50000];
char num[2][4];
int vv[10], nums[65000], sst = 0, val[4 + 1][4 + 1][10000], sc[10],
                         c[4 + 1][4 + 1];
void setNum(int id, int n) {
  int v = 4 - 1;
  while (v >= 0) {
    num[id][v--] = n % 10 + '0';
    n /= 10;
  }
}
bool valid(int i) {
  setNum(0, i);
  set<char> s;
  for (int i = int(0); i < int(4); i++) s.insert(num[0][i]);
  return s.size() == 4;
}
pair<int, int> getV() {
  pair<int, int> ret = pair<int, int>(0, 0);
  memset(vv, 0, sizeof(vv));
  for (int i = int(0); i < int(4); i++) {
    if (num[0][i] == num[1][i]) {
      ret.first++;
      vv[num[0][i] - '0'] |= 2;
    }
    vv[num[0][i] - '0'] |= 1;
  }
  for (int i = int(0); i < int(4); i++) {
    if (vv[num[1][i] - '0'] == 1) {
      vv[num[1][i] - '0'] = 0;
      ret.second++;
    }
  }
  return ret;
}
void solve(int s, int v) {
  if (step[s][v].sz == 0) return;
  if (step[s][v].sz == 1) {
    step[s][v].id = nums[step[s][v].sst];
    return;
  }
  int lo = 1000000000;
  memset(c, 0, sizeof(c));
  for (int i = int(0); i < int(step[s][v].sz); i++) {
    setNum(0, nums[step[s][v].sst + i]);
    for (int j = int(0); j < int(4 + 1); j++)
      for (int k = int(0); k < int(4 + 1); k++) c[j][k] = 0;
    for (int j = int(0); j < int(step[s][v].sz); j++) {
      setNum(1, nums[step[s][v].sst + j]);
      pair<int, int> t = getV();
      val[t.first][t.second][c[t.first][t.second]++] = nums[step[s][v].sst + j];
    }
    int hi = 0;
    for (int j = int(0); j < int(4 + 1); j++)
      for (int k = int(0); k < int(4 + 1); k++) hi = max(hi, c[j][k]);
    if (hi < lo) lo = hi, step[s][v].id = nums[step[s][v].sst + i];
  }
  setNum(0, step[s][v].id);
  for (int j = int(0); j < int(4 + 1); j++)
    for (int k = int(0); k < int(4 + 1); k++) c[j][k] = 0;
  for (int j = int(0); j < int(step[s][v].sz); j++) {
    setNum(1, nums[step[s][v].sst + j]);
    pair<int, int> t = getV();
    val[t.first][t.second][c[t.first][t.second]++] = nums[step[s][v].sst + j];
  }
  step[s][v].st = sc[s + 1];
  for (int j = int(0); j < int(4 + 1); j++)
    for (int k = int(0); k < int(4 + 1); k++) {
      step[s + 1][sc[s + 1]++] = Data{-1, -1, sst, c[j][k]};
      for (int kk = int(0); kk < int(c[j][k]); kk++) {
        nums[sst + kk] = val[j][k][kk];
      }
      sst += c[j][k];
    }
  for (int j = int(0); j < int(4 + 1); j++)
    for (int k = int(0); k < int(4 + 1); k++) {
      solve(s + 1, step[s][v].st + (4 + 1) * j + k);
    }
}
int main() {
  int tt = pow(10, 4);
  int ccc = 0;
  for (int i = int(0); i < int(tt); i++) {
    if (valid(i)) nums[ccc++] = i;
  }
  sst = tt;
  step[0][0] = Data{-1, -1, 0, ccc};
  solve(0, 0);
  int a, b, s = 0, v = 0;
  cout << setfill('0') << setw(4);
  cout << step[s][v].id << endl;
  while (cin >> a >> b && a != 4) {
    v = step[s][v].st + (4 + 1) * a + b;
    s++;
    cout << setfill('0') << setw(4) << step[s][v].id << endl;
  }
  return 0;
}
