#include <bits/stdc++.h>
using namespace std;
long long k1, k2, n;
unordered_map<string, int> pop_back;
string pq[100000];
string s;
char p[20];
void my_assert(bool ok) {
  if (!ok)
    while (true)
      ;
}
signed main() {
  scanf("%d", &n);
  for (int i = 1989; i <= 500000; i++) {
    string s = "", t = "";
    int k = i;
    while (k) {
      t = "";
      t += char(k % 10 + '0');
      t += s;
      s = t;
      k /= 10;
      if (pop_back[s] == 0) {
        if (i < 100000) {
          pq[i] = s;
        }
        pop_back[s] = i;
        break;
      }
    }
  }
  for (int i = 0; i < int(n); i++) {
    scanf("%s", p);
    s = p;
    s.erase(s.begin(), s.begin() + 4);
    if ((int)s.size() >= 6) {
      bool ok = true;
      for (int i = 0; i < (int)s.size(); i++) {
        if (i + 4 == (int)s.size()) {
          int k = (((s[i] - '0') * 10 + s[i + 1] - '0') * 10 + s[i + 2] - '0') *
                      10 +
                  s[i + 3] - '0';
          my_assert(k < 100000);
          if (k >= 1989 && (int)pq[k].size() == 4) ok = false;
          break;
        }
        if (s[i] == '1') continue;
        if (s[i] == '0') break;
        ok = false;
      }
      if (ok) cout << "1";
      cout << s << "\n";
      continue;
    }
    cout << pop_back[s] << "\n";
  }
  return 0;
}
