#include <bits/stdc++.h>
using namespace std;
string mp[10005];
map<string, int> tk;
void init() {
  for (int i = 1989; i <= 10000; i++) {
    int x = i;
    string cur = "";
    cur += ((x % 10) + '0');
    x /= 10;
    while (tk.count(cur)) {
      cur += x % 10 + '0';
      x /= 10;
    }
    tk[cur] = i;
    reverse(cur.begin(), cur.end());
    mp[i] = cur;
  }
}
int solve(string x) {
  reverse(x.begin(), x.end());
  if (tk.count(x)) return tk[x];
  reverse(x.begin(), x.end());
  string red = "";
  for (int i = 1; i < x.size(); i++) red += x[i];
  int pot = 0;
  if (x[0] == '0') pot = 1;
  for (int i = 0; i < x.size(); i++) {
    pot *= 10;
    pot += x[i] - '0';
  }
  int rs = solve(red);
  if (rs < pot) {
    return pot;
  }
  int ans = 1;
  for (int i = 0; i < x.size(); i++) {
    ans *= 10;
    ans += x[i] - '0';
  }
  return ans;
}
char s[15];
int main() {
  init();
  int n;
  scanf("%d", &n);
  while (n--) {
    scanf("%s", s);
    string ab = "";
    for (int i = 0; s[i] != '\0'; i++) {
      if (s[i] >= '0' && s[i] <= '9') ab += s[i];
    }
    int res = solve(ab);
    printf("%d\n", res);
  }
}
