#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
using namespace std;
int a, b, c, d, n, m, k;
unordered_map<string, int> was;
char tmp[25], str[444];
inline void pt(char* c, int t) {
  int k = 0;
  while (t) {
    c[k++] = t % 10 + '0';
    t /= 10;
  }
  c[k] = 0;
  reverse(c, c + k);
}
inline string fnd(int t) {
  pt(tmp, t);
  int l = (int)strlen(tmp);
  string now;
  for (int i = (l - 1), _b = (0); i >= _b; i--) {
    reverse((now).begin(), (now).end());
    now += tmp[i];
    reverse((now).begin(), (now).end());
    if (!was.count(now)) return now;
  }
  return tmp;
}
long long solve(const char* str, bool cache = true) {
  if (cache) {
    auto it = was.find(str);
    if (it != was.end()) return it->second;
  }
  int l = (int)strlen(str);
  long long cur = 0;
  for (int _n((l - 4) - 1), i(0); i <= _n; i++) {
    cur = cur * 10 + 1;
  }
  cur = cur * 10000 + 3098;
  long long p = 0;
  for (int _n((l)-1), i(0); i <= _n; i++) {
    p = p * 10 + str[i] - '0';
  }
  if (p <= cur) {
    p = 1;
    for (int _n((l)-1), i(0); i <= _n; i++) {
      p = p * 10 + str[i] - '0';
    }
    return p;
  } else {
    return p;
  }
}
int main() {
  for (int _n(4000), i(1989); i <= _n; i++) {
    string t = fnd(i);
    was[t] = i;
  }
  scanf("%d", &n);
  for (int _n((n)-1), i(0); i <= _n; i++) {
    scanf("%s", str);
    long long res = solve(str + 4);
    printf("%I64d\n", res);
  }
}
