#include <bits/stdc++.h>
using namespace std;
long long mix(long long x, string s) {
  for (int i = 0; i < s.size(); i++) x *= 10;
  reverse(s.begin(), s.end());
  for (int i = 0; i < s.size(); i++) {
    long long n = s[i] - '0';
    for (int h = 0; h < i; h++) n *= 10;
    x += n;
  }
  return x;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string q = "";
    string s;
    cin >> s;
    long long val = 1988;
    while (1) {
      char c = s[s.size() - q.size() - 1];
      if (c == '\'') break;
      q = c + q;
      long long l = 0, r = 30000000, m, h;
      while (mix(l, q) <= val) l++;
      while (l < r) {
        m = l + (r - l) / 2;
        h = mix(m, q);
        if (h > val)
          r = m;
        else
          l = m + 1;
      }
      val = h;
    }
    cout << val << endl;
  }
}
