#include <bits/stdc++.h>
using namespace std;
int rok(string a) {
  int y = 0;
  int q = 1;
  for (int i = a.length() - 1; i > 3; i--) {
    int z;
    z = a[i] - 48;
    y = y + q * z;
    q = q * 10;
  }
  while (y < 1989) y += q;
  while (y - q >= 1989) {
    y -= q;
  }
  if (q == 1) return 0;
  string a2 = "IAO'";
  for (int i = 5; i < a.length(); i++) a2 += a[i];
  int M = rok(a2);
  while (y <= M) y += q;
  return y;
}
int main() {
  int n;
  cin >> n;
  for (int j = 0; j < n; j++) {
    string a;
    cin >> a;
    cout << rok(a) << endl;
  }
}
