#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.1415926535897932384626433832795;
const long double EPS = 1e-11;
int n;
set<string> st;
string s, ts;
map<string, int> mapp;
string tos(signed long long i) {
  string s = "";
  while (i > 0) {
    s = char(i % 10 + '0') + s;
    i /= 10;
  }
  return s;
}
signed long long toi(string s) {
  signed long long res = 0;
  for (int i = 0; i < (s.size()); i++) {
    if (s[i] >= '0' && s[i] <= '9') res = res * 10 + (s[i] - '0');
  }
  return res;
}
void gen() {
  st.insert("");
  for (int i = 1989; i < 50000; i++) {
    string ts = "";
    int tt = i;
    while (st.find(ts) != st.end()) {
      ts = char(tt % 10 + '0') + ts;
      tt /= 10;
    }
    mapp[ts] = i;
    st.insert(ts);
  }
}
int main() {
  gen();
  cin >> n;
  signed long long suf;
  for (int i = 0; i < (n); i++) {
    cin >> s;
    suf = toi(s);
    string tmp = "";
    for (int j = 0; j < (s.size()); j++)
      if (s[j] >= '0' && s[j] <= '9') tmp += s[j];
    if (s.size() < 8) {
      cout << mapp[tmp] << endl;
      continue;
    }
    signed long long miny = 1989;
    while (miny % 10 != suf % 10) miny++;
    signed long long st = 10;
    for (int j = 0; j < (s.size() - 4); j++) {
      if (miny % st < suf % st)
        miny = miny - miny % st + suf % st;
      else if ((miny % st == suf % st))
        miny = miny + st;
      else if (miny % st > suf % st)
        miny = miny - miny % st + suf % st + st;
      st *= 10;
    }
    if (miny % 100000 >= 13099 && miny % 100000 <= 13108 && miny > st / 10)
      miny -= st / 10;
    if (tos(miny).size() < s.size() - 4) miny += st / 10;
    cout << miny << endl;
    continue;
    if (miny != mapp[tmp])
      cout << s << " : " << miny << " cor=" << mapp[tmp] << endl;
  }
  return 0;
}
