    /************
    * LIBRARIES *
    ************/

    #include <bits/stdc++.h>

    using namespace std;

    /**********
    * ALIASES *
    **********/

    typedef long long ll;
    typedef unsigned long long llu;
    typedef pair<int, int> ii;
    typedef vector<int> vi;
    typedef vector<ii> vii;
    typedef pair<int, ii> iii;

    /*********
    * MACROS *
    *********/

    #ifndef _LOCAL_DEBUG
    #define _LOCAL_DEBUG 0
    #endif // !_LOCAL_DEBUG

    //? Easier debugging macro for safe and quick prints
    //* The optimizer will erase the if statement if _LOCAL_DEBUG is 0
    #define debug(X) if(_LOCAL_DEBUG){cerr << "[" << #X << ':' << __LINE__ << "]: " << X << endl;}

    /************
    * CORE CODE *
    ************/

    //? Pre-process anything needed
    void preProcess(){

    }

    const int MS = 1e6 + 7;

    list<int> adj[MS];

    // bool dfs(int x, vector<bool> &vis, int p){
    //   if(vis[x]){
    //     return false;
    //   }

    //   vis[x] = true;

    //   for(int y : adj[x]){
    //     if(y != p && !dfs(y, vis, x)){
    //       return false;
    //     }
    //   }

    //   return true;
    // }

    bool bfs(int x, vector<bool> &vis){
      queue<ii> q;
      q.push({x, -1});

      while(!q.empty()){
        ii c = q.front();
        q.pop();
        vis[c.first] = true;

        for(int y : adj[c.first]){
          if(y == c.second){
            continue;
          }

          if(vis[y]){
            return false;
          }

          q.push({y, c.first});
        }
      }

      return true;
    }

    struct Coord{
      int xs, xe, idx;
      friend bool operator<(const Coord& a, const Coord& b){
        return a.xe < b.xe;
      }
      friend bool operator==(const Coord& a, const Coord& b){
        return a.idx == b.idx;
      }
    };

    //? Solve a case
    void solve(){
      int n;
      cin >> n;

      int x, y;

      vii p(n);
      set<Coord> op;
      // vector<Coord> pq(n*2, {0, 0, 0});

      for(int i = 0; i < n; i++){
        cin >> x >> y;
        p[i] = make_pair(x, y);
      }

      sort(p.begin(), p.end());

      for(int i = 0; i < n; i++){
        ii cur = p[i];
        auto it = op.begin();
        while (!op.empty() && (*it).xe <= cur.first)
        {
          op.erase(*it);
          it = op.begin();
        }

        for(; it != op.end(); it++){
          if((*it).xe >= cur.second){
            break;
          }
          adj[(*it).idx].push_back(i);
          adj[i].push_back((*it).idx);
        }

        // push_heap(pq.begin(), pq.end(), )
        op.insert({cur.first, cur.second, i});
      }

      vector<bool> vis(n, false);

      bool poss = bfs(0, vis);

      if(!poss){
        cout << "NO\n";
        return;
      }

      for(int i = 0; i < n; i++){
        if(!vis[i]){
          poss = false;
          break;
        }
      }

      if(!poss){
        cout << "NO\n";
        return;
      }

      cout << "YES\n";
    }

    int main(){
      //? STDIN & STDOUT redirect
      #if _LOCAL_DEBUG
      freopen("in.txt", "r", stdin);
      // freopen("out.txt", "w", stdout);
      #endif

      // ? Pre-processing
      // preProcess();

      //? Single case
      solve();

      //? Multi case
      // int t;
      // cin >> t;

      // while(t--){
      //   solve();
      // }

      return 0;
    }


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<cstring>
#include<cstdlib>
#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i<n; i++)
    {
        ll num = 0;
        char st[100];
        scanf("%s", st);
        int len = strlen(st);
        ll now = 1988;
        for(int j = len-1; j >= 4; j--)
        {
            int d = now % 10;
            now/=10;
            if(d >= st[j] - '0'){
                now++;
            }
        }
        if(now) printf("%lld", now);
        for(int j = 4; j<len; j++)
            printf("%c", st[j]);
        printf("\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define scan(x) scanf("%d",&x)
#define scanll(y) scanf("%lld",&y)
#define print(x) printf("%d\n",x)
#define printll(y) printf("%lld\n",y)

typedef pair<int,int>pii;
const int maxn=2e5+55;

int main() {
//	freopen("Task.in","r",stdin);freopen("Task.out","w",stdout);
	int n,num;
	scan(n);
	string s;
	while(n--) {
		cin>>s;
		num=0;
		int maxi=0,dig=0,val=10;
		for(int i=4 ; i<s.size() ; i++) {
			num=num*10+(s[i]-'0');
			++dig;
		}
		for(int i=1 ; i<dig ; i++) {
			maxi=maxi+val;
			val=val*10;
		}
		// cout<<num<<" ";
		int ans=num;
		while(ans<1989+maxi) {
			ans+=val;
		}
		print(ans);
	}
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

bool inside(string s, vector<ll> &l, vector<ll> &r) {
	stringstream ss(s);
	ll cur; ss>>cur;
	int sz=l.size();
	for (int i=0; i<sz; i++)
		if(l[i]<=cur&&cur<=r[i]) return true;
	return false;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	set<string> used;
	map<string,string> ret;
	for (int i=1989; i<=5000; i++) {
		stringstream ss; ss<<i;
		string s=ss.str();
		int sz=s.size();
		for (int j=0; j<sz; j++) {
			string cur=s.substr(sz-1-j);
			if(used.find(cur)==used.end()) {
				used.insert(cur);
				ret[cur]=s;
				break;
			}
		}
	}

	vector<ll> left, right;
	left.push_back(2099); right.push_back(3098);
	ll r=3098;
	for (ll i=10000; i<(1LL<<50); i*=10) {
		r+=i;
		left.push_back(i), right.push_back(r);
	}

	int n; cin>>n;
	string s;
	for (int i=0; i<n; i++) {
		cin>>s;
		s=s.substr(4);
		if(ret.find(s)!=ret.end()) cout << ret[s] << endl;
		else {
			string t='1'+s;
			if(inside(t,left,right)) cout << t << endl;
			else cout << s << endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

bool inside(string s, vector<ll> &l, vector<ll> &r) {
	stringstream ss(s);
	ll cur; ss>>cur;
	int sz=l.size();
	for (int i=0; i<sz; i++)
		if(l[i]<=cur&&cur<=r[i]) return true;
	return false;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	set<string> used;
	map<string,string> ret;
	for (int i=1989; i<=5000; i++) {
		stringstream ss; ss<<i;
		string s=ss.str();
		int sz=s.size();
		for (int j=0; j<sz; j++) {
			string cur=s.substr(sz-1-j);
			if(used.find(cur)==used.end()) {
				used.insert(cur);
				ret[cur]=s;
				break;
			}
		}
	}

	vector<ll> left, right;
	left.push_back(2099); right.push_back(3098);
	for (ll i=10000; i<(1LL<<50); i*=10)
		left.push_back(i), right.push_back(i+3098);

	int n; cin>>n;
	string s;
	for (int i=0; i<n; i++) {
		cin>>s;
		s=s.substr(4);
		if(ret.find(s)!=ret.end()) cout << ret[s] << endl;
		else {
			string t='1'+s;
			if(inside(t,left,right)) cout << t << endl;
			else cout << s << endl;
		}
	}
	return 0;
}


/*
PROG: cf664c
LANG: C++
debug my code
*/
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cassert>
#include <complex>
#include <iomanip>

using namespace std;

int N;
void solve(int a, string b)
{
	int x = b.length();
	//between 1989 + (10^(x - 1)) and 1989 + (10^(x) - 1)
	int hpw = 1, lpw;
	for (int i = 0; i < x; i++)
	{
		hpw *= 10;
		hpw++;
	}
//	cerr << hpw << endl;
	lpw = hpw/10;
	int low = 1989 + lpw - 1;
	int high = 1989 + hpw - 2;
//	cerr << low << ' ' << high << endl;
	int d = high - low + 1;
	int val = (low/d) * d + a;
	if (low <= val && val <= high)
	{
		cout << val << '\n';
	}
	else
	{
		cout << val + d << '\n';
	}
	return;
//	cerr << d << endl;
	//10, 100, 1000, 10000
	//11, 111, 1111
	//10, 110, 1
	//find the number that is in this range that ends with b
	//find the number that is b mod pw

}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	if (fopen("cf664c.in", "r"))
	{
		freopen ("cf664c.in", "r", stdin);
		freopen ("cf664c.out", "w", stdout);
	}
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		string s;
		cin >> s;
		s.erase(0, 4);
		int t = stoi(s);
		solve(t, s);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define PI 3.141592653589793238462
typedef long long ll;
typedef double db;
ll num[25],a[25];
unordered_map<string,int> p;
int main(){
    int q;
    cin>>q;
    for(int i=1989;i<=15000;i++){
        int x=i;string s="";
        while(x){
            char k=(char)('0'+x%10);
            s=k+s;x/=10;
            if(!p[s]) {
                p[s]=i;break;
            }
        }
    }num[0]=1;
    for(int i=1;i<=15;i++){
        num[i]=num[i-1]*10;
    }a[4]=13098;
    for(int i=5;i<=10;i++){
        a[i]=a[i-1]+num[i];
    }
    while(q--){
        string s,k="";
        cin>>s;
        for(int i=0;i<s.length();i++){
            if(s[i]>='0'&&s[i]<='9'){
                k=s.substr(i);break;
            }
        }
        ll m;
        stringstream ss;
        ss<<k;ss>>m;
        if(k.length()<=4){
            cout<<p[k]<<endl;
        }
        else{
            m+=num[k.length()];
            ll pos=upper_bound(a+4,a+11,m)-a-1;
            if(pos>k.length()) cout<<k<<endl;
            else cout<<1<<k<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define PI 3.141592653589793238462
typedef long long ll;
typedef double db;
ll num[25],a[25];
unordered_map<string,int> p;
int main(){
    int q;
    cin>>q;
    for(int i=1989;i<=15000;i++){
        int x=i;string s="";
        while(x){
            char k=(char)('0'+x%10);
            s=k+s;x/=10;
            if(!p[s]) {
                p[s]=i;break;
            }
        }
    }num[0]=1;
    for(int i=1;i<=15;i++){
        num[i]=num[i-1]*10;
    }a[4]=13098;
    for(int i=5;i<=10;i++){
        a[i]=a[i-1]+num[i];
    }
    while(q--){
        string s,k="";
        cin>>s;
        for(int i=0;i<s.length();i++){
            if(s[i]>='0'&&s[i]<='9'){
                k=s.substr(i);break;
            }
        }
        ll m;
        stringstream ss;
        ss<<k;ss>>m;
        if(k.length()<=4){
            cout<<p[k]<<endl;
        }
        else{
            m+=num[k.length()];
            ll pos=lower_bound(a+4,a+11,m)-a;
            if(pos>k.length()) cout<<k<<endl;
            else cout<<1<<k<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define PI 3.141592653589793238462
typedef long long ll;
typedef double db;
ll num[25];
unordered_map<string,int> p;
int main(){
    int q;
    cin>>q;
    for(int i=1989;i<=15000;i++){
        int x=i;string s="";
        while(x){
            char k=(char)('0'+x%10);
            s=k+s;x/=10;
            if(!p[s]) {
                p[s]=i;break;
            }
        }
    }
    while(q--){
        string s,k="";
        cin>>s;
        for(int i=0;i<s.length();i++){
            if(s[i]>='0'&&s[i]<='9'){
                k=s.substr(i);break;
            }
        }
        ll m;
        stringstream ss;
        ss<<k;ss>>m;
        if(k.length()<=4){
            cout<<p[k]<<endl;
        }
        else{
            m%=100000;int tag=0;
            for(int i=0;i<k.length()-5;i++){
                if(k[i]>'1') tag=1;
            }
            if(m<=13098&&!tag){
                cout<<1<<k<<endl;
            }
            else cout<<k<<endl;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#define PI 3.141592653589793238462
typedef long long ll;
typedef double db;
ll num[25];
unordered_map<string,int> p;
int main(){
    int q;
    cin>>q;
    for(int i=1989;i<=15000;i++){
        int x=i;string s="";
        while(x){
            char k=(char)('0'+x%10);
            s=k+s;x/=10;
            if(!p[s]) {
                p[s]=i;break;
            }
        }
    }
    while(q--){
        string s,k="";
        cin>>s;
        for(int i=0;i<s.length();i++){
            if(s[i]>='0'&&s[i]<='9'){
                k=s.substr(i);break;
            }
        }
        ll m;
        stringstream ss;
        ss<<k;ss>>m;
        if(k.length()<=4){
            cout<<p[k]<<endl;
        }
        else{
            if(m<=13098){
                cout<<1<<k<<endl;
            }
            else cout<<k<<endl;
        }
    }
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define Max 111111
int main()
{
   char ch[50];
   int n;
   scanf("%d",&n);
   getchar ();
   for(int i=0;i<n;i++)
   {

     scanf("%s",ch);
     long long int len=strlen(ch);
     long long sum=0;
        int num=0;
        for(int i=len-1;i>=4;i--)
        {
            sum+=(int)(ch[i]-'0')*(pow(10,num++));
        }
     if(len==5)
     {
        if(ch[4]=='9')
        {
          printf("1989\n");
        }
        else
        {
          printf("199%d\n",ch[4]-'0');
        }
     }
     else if(len==6)
     {
        if(ch[4]=='9'&&ch[5]=='9')
        {
          printf("1999\n");
        }
        else
        {
          printf("20%c%c\n",ch[4],ch[5]);
        }
     }
     else if(len==7)
     {
         if(sum<99)
        {
          printf("3%c%c%c\n",ch[4],ch[5],ch[6]);
        }
        else
        {
          printf("2%c%c%c\n",ch[4],ch[5],ch[6]);
        }
     }
     else if(len==8)
     {
        if(sum>3098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==9)
     {
        if(sum>13098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==10)
     {
        if(sum>113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==11)
     {
        if(sum>1113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==12)
     {
        if(sum>11113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==13)
     {
        if(sum>111113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     getchar();
   }
   return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define Max 111111
int main()
{
   char ch[50];
   int n;
   scanf("%d",&n);
   getchar ();
   for(int i=0;i<n;i++)
   {

     scanf("%s",ch);
     int len=strlen(ch);
     long long sum=0;
        int num=0;
        for(int i=len-1;i>=4;i--)
        {
            sum+=(int)(ch[i]-'0')*(pow(10,num++));
        }
     if(len==5)
     {
        if(ch[4]=='9')
        {
          printf("1989\n");
        }
        else
        {
          printf("199%d\n",ch[4]-'0');
        }
     }
     else if(len==6)
     {
        if(ch[4]=='9'&&ch[5]=='9')
        {
          printf("1999\n");
        }
        else
        {
          printf("20%c%c\n",ch[4],ch[5]);
        }
     }
     else if(len==7)
     {
         if(sum<99)
        {
          printf("3%c%c%c\n",ch[4],ch[5],ch[6]);
        }
        else
        {
          printf("2%c%c%c\n",ch[4],ch[5],ch[6]);
        }
     }
     else if(len==8)
     {
        if(sum>3098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==9)
     {
        if(sum>13098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==10)
     {
        if(sum>113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==11)
     {
        if(sum>1113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==12)
     {
        if(sum>11113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==13)
     {
        if(sum>111113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     getchar();
   }
   return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define Max 111111
int main()
{
   char ch[50];
   int n;
   scanf("%d",&n);
   getchar ();
   for(int i=0;i<n;i++)
   {

     scanf("%s",ch);
     int len=strlen(ch);
     long long sum=0;
        int num=0;
        for(int i=len-1;i>=4;i--)
        {
            sum+=(int)(ch[i]-'0')*(pow(10,num++));
        }
     if(len==5)
     {
        if(ch[4]=='9')
        {
          printf("1989\n");
        }
        else
        {
          printf("199%d\n",ch[4]-'0');
        }
     }
     else if(len==6)
     {
        if(ch[4]=='9'&&ch[5]=='9')
        {
          printf("1999\n");
        }
        else
        {
          printf("20%c%c\n",ch[4],ch[5]);
        }
     }
     else if(len==7)
     {
        if((ch[4]!='0'))
        {
          printf("2%c%c%c\n",ch[4],ch[5],ch[6]);
        }
        else if((ch[4]=='0'&&ch[5]=='9'&&ch[6]=='9'))
        {
          printf("2%c%c%c\n",ch[4],ch[5],ch[6]);
        }
        else
        {
          printf("3%c%c%c\n",ch[4],ch[5],ch[6]);
        }
     }
     else if(len==8)
     {
        if(sum>3098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==9)
     {
        if(sum>13098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==10)
     {
        if(sum>113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==11)
     {
        if(sum>1113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==12)
     {
        if(sum>11113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==13)
     {
        if(sum>111113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     getchar();
   }
   return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define Max 111111
int main()
{
   char ch[50];
   int n;
   scanf("%d",&n);
   getchar ();
   for(int i=0;i<n;i++)
   {

     scanf("%s",ch);
     int len=strlen(ch);
     long long sum=0;
        int num=0;
        for(int i=len-1;i>=4;i--)
        {
            sum+=(int)(ch[i]-'0')*(pow(10,num++));
        }
     if(len==5)
     {
        if(ch[4]=='9')
        {
          printf("1989\n");
        }
        else
        {
          printf("199%d\n",ch[4]-'0');
        }
     }
     else if(len==6)
     {
        if(ch[4]=='9'&&ch[5]=='9')
        {
          printf("1999\n");
        }
        else
        {
          printf("20%c%c\n",ch[4],ch[5]);
        }
     }
     else if(len==7)
     {
        if((ch[4]!=0))
        {
          printf("2%c%c%c\n",ch[4],ch[5],ch[6]);
        }
        else if((ch[4]=='0'&&ch[5]=='9'&&ch[6]=='9'))
        {
          printf("2%c%c%c\n",ch[4],ch[5],ch[6]);
        }
        else
        {
          printf("3%c%c%c\n",ch[4],ch[5],ch[6]);
        }
     }
     else if(len==8)
     {
        if(sum>3098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==9)
     {
        if(sum>13098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==10)
     {
        if(sum>113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==11)
     {
        if(sum>1113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==12)
     {
        if(sum>11113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==13)
     {
        if(sum>111113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     getchar();
   }
   return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define Max 111111
int main()
{
   char ch[50];
   int n;
   scanf("%d",&n);
   getchar ();
   for(int i=0;i<n;i++)
   {

     scanf("%s",ch);
     long long int len=strlen(ch);
     long long sum=0;
        int num=0;
        for(int i=len-1;i>=4;i--)
        {
            sum+=(int)(ch[i]-'0')*(pow(10,num++));
        }
     if(len==5)
     {
        if(ch[4]=='9')
        {
          printf("1989\n");
        }
        else
        {
          printf("199%d\n",ch[4]-'0');
        }
     }
     else if(len==6)
     {
        if(ch[4]=='9'&&ch[5]=='9')
        {
          printf("1999\n");
        }
        else
        {
          printf("20%c%c\n",ch[4],ch[5]);
        }
     }
     else if(len==7)
     {
        if((ch[4]!='0'))
        {
          printf("2%c%c%c\n",ch[4],ch[5],ch[6]);
        }
        else if((ch[4]=='0'&&ch[5]=='9'&&ch[6]=='9'))
        {
          printf("2%c%c%c\n",ch[4],ch[5],ch[6]);
        }
        else
        {
          printf("3%c%c%c\n",ch[4],ch[5],ch[6]);
        }
     }
     else if(len==8)
     {
        if(sum>3098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==9)
     {
        if(sum>13098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==10)
     {
        if(sum>113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==11)
     {
        if(sum>1113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==12)
     {
        if(sum>11113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     else if(len==13)
     {
        if(sum>111113098)
        {
          printf("%lld\n",sum);
        }
        else
        {
          printf("1");
          for(int i=4;i<len;i++)
          {
            printf("%c",ch[i]);
          }
          printf("\n");
        }
     }
     getchar();
   }
   return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define ZERO (1e-10)
#define INF (1<<29)
#define CL(A,I) (memset(A,I,sizeof(A)))
#define DEB printf("DEB!\n");
#define D(X) cout<<"  "<<#X": "<<X<<endl;
#define EQ(A,B) (A+ZERO>B&&A-ZERO<B)
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
#define IN(n) int n;scanf("%d",&n);
#define FOR(i, m, n) for (int i(m); i < n; i++)
#define F(n) FOR(i,0,n)
#define FF(n) FOR(j,0,n)
#define FT(m, n) FOR(k, m, n)
#define aa first
#define bb second
void ga(int N,int *A){F(N)scanf("%d",A+i);}
char s[1<<17];
int L,T,R,Y;
int main(void){
    IN(_)F(_){
        scanf("%s",s),Y=atoi(s+4),L=strlen(s+4),R=0,T=10;
        F(L-1)R+=T,T*=10;
        while(Y<1989+R)Y+=T;
        printf("%d\n",Y);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define sc scanf
#define pc printf
#define ll long long

int n;
ll yearCount[11];
char str[15];
char targetYear[11], originalYear[11], tempYear[11];

ll Pow[11];

void Init()
{
	yearCount[1] = 10LL;
    Pow[1] = 10LL;
	for (int i = 2; i <= 10; i++){
		yearCount[i] = yearCount[i - 1] * 10;
		Pow[i] = yearCount[i];
	}

	for(int i = 2; i <= 10; i++){
        yearCount[i] += yearCount[i-1];
	}
}

int isOriginalBoro(int targetLen, int originalLen)
{
	int boro = -1;

    int targetInd = 0;
    int originalInd = originalLen - targetLen;

    while(targetYear[targetInd] && originalYear[originalInd]){
        if(targetYear[targetInd] < originalYear[originalInd]) {boro = 1;break;}
        else if(targetYear[targetInd] > originalYear[originalInd]) {boro = 0;break;}
        targetInd++;
        originalInd++;
    }

	return boro;
}

int main()
{
	Init();

	sc("%d", &n);

	int k = 0;
	ll startYear = 1989;

	for (int i = 0; i < n; i++){
		sc("%s", str);

		k = 0;
		int targetInd = 0;
		for (int j = 0; str[j]; j++){
			if (str[j] >= '0' && str[j] <= '9'){
				//curYear = curYear * 10 + (str[j] - '0');
				targetYear[k++] = str[j];
				//k++;
				//targetArray[targetInd++] = str[j];
			}
		}

        targetYear[k] = '\0';

		ll fir = startYear + yearCount[k - 1];
		//pc("%lld\n",fir);
		ll last = fir + yearCount[k] - 1;

        int ind = 0;

        ll temp = fir;
        while(temp){
            originalYear[ind++] = '0' + (temp % 10);
            temp /= 10;
        }

        int tempInd = 0;
        int len = ind;
        ind--;
        while(tempInd < ind){
            swap(originalYear[tempInd], originalYear[ind]);
            tempInd++;
            ind--;
        }
        originalYear[len] = '\0';

		//pc("%s %s %d\n", targetYear, originalYear, isOriginalBoro(k, len));

        if(isOriginalBoro(k, len) == -1){
            pc("%s\n", originalYear);
        }
        else if(isOriginalBoro(k, len) == 1){
            int originalLen = len;
            int targetLen = k;

            ll curYear = 0;

            int ind = 0;
            while(originalYear[ind]){
                curYear = curYear * 10 + (originalYear[ind] - '0');
                ind++;
            }

            curYear = ((curYear / Pow[targetLen]) + 1 ) * Pow[targetLen];

            ll rest = 0;

            ind = 0;
            while(targetYear[ind]){
                rest = rest * 10 + (targetYear[ind] - '0');
                ind++;
            }

            curYear += rest;

            pc("%lld\n",curYear);

        }
        else{
            //pc("origin %s",originalYear);
            int originalLen = len;
            int targetLen = k;
            int stopPos = originalLen - targetLen;
            int ind = 0;

            while(ind < stopPos){
                tempYear[ind] = originalYear[ind];
                ind++;
            }

            int nowInd = 0;
            while(targetYear[nowInd]){
                tempYear[ind++] = targetYear[nowInd++];
            }
            tempYear[ind] = '\0';
            pc("%s\n", tempYear);
        }

	}

	//printf("%lld\n", yearCount[9] - yearCount[8]);
	//system("pause");

	return 0;

}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
typedef long double ld;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
void needForSpeed(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
ll gcd(ll a,ll b){while(b){a %= b; swap(a,b);}return a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
bool isPrime(ll a){if(a<2)return 0;for(ll i=2;i<=sqrt(a);i++)if(a%i==0) return 0;return 1;}
ll binpow(ll base, ll pow, ll mod){
    if(pow==0) return 1;
    else if(pow%2) return base*binpow(base,pow-1,mod)%mod;
    else{
        ll p = binpow(base,pow/2,mod);
        return p*p%mod;
    }
}

int solve(){
    string s;
    cin>>s;
    int k = s.length()-4, yr = stoi(s.substr(4)), f=0,lol=10;
    for(int i=1;i<k;++i){
        f+=lol;
        lol*=10;
    }
    while(yr < 1989 +f) yr +=lol;
    cout<<yr<<'\n';
    return 0;
}

int main(){
    needForSpeed();
    int tests=1;
    cin>>tests;
    while (tests--)
    {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
typedef long double ld;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
void needForSpeed(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
ll gcd(ll a,ll b){while(b){a %= b; swap(a,b);}return a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
bool isPrime(ll a){if(a<2)return 0;for(ll i=2;i<=sqrt(a);i++)if(a%i==0) return 0;return 1;}
ll binpow(ll base, ll pow, ll mod){
    if(pow==0) return 1;
    else if(pow%2) return base*binpow(base,pow-1,mod)%mod;
    else{
        ll p = binpow(base,pow/2,mod);
        return p*p%mod;
    }
}

int solve(){
    string s;
    cin>>s;
    int k = s.substr(0,s.find("'")).length(), yr = stoi(s.substr(4)), f=0,lol=10;
    for(int i=1;i<k;++i){
        f+=lol;
        lol*=10;
    }
    while(yr < 1989 +f) yr +=lol;
    cout<<yr<<'\n';
    return 0;
}

int main(){
    needForSpeed();
    int tests=1;
    cin>>tests;
    while (tests--)
    {
        solve();
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll fun(ll bot,char *c){
        ll N=atoi(c);
      int sz=strlen(c);
      ll ad=1;
      for(int i=0;i<sz;i++) ad*=10;
      //cout<<ad<<" "<<N<<endl;
      while(1){
        if(N>bot) break;
        N+=ad;
      }
      return N;
}
int main(){
    int T;
    cin>>T;
    //getchar();
    while(T--){
      //for(int i=0;i<4;i++) getchar();
      char c[16];
      cin>>c;
      //cout<<c;
      ll N;
      int sz=strlen(c);
      ll bot=1988;
      for(int i=sz-1;i>=4;i--){
          bot=fun(bot,&c[i]);
      }
      cout<<bot<<endl;
    }
  return 0;
}


/*2014300227    662D - 46    GNU C++11    Accepted    15 ms    2180 KB*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
typedef long long ll;
const ll mod=1e9+7;
ll dp[20];
int n;
char str[20];
ll fun(int x)
{
    ll ans=1;
    for(int i=1;i<=x;i++)
    {
        ans*=10;
    }
    return ans;
}
int solve()
{
    int len=strlen(str);
    ll s=0;
    for(int i=4;i<len;i++)
    {
        s*=10;
        s+=str[i]-'0';
    }
    for(int i=0;i<=200;i++)
    {
        ll num=s+(ll)i*fun(len-4);
        if(num>=dp[len-5]&&num<dp[len-4])
        {
            cout<<num<<"\n";
            return 0;
        }
    }
}

int main()
{
    dp[0]=1989;
    ll temp=1;
    for(int i=1;i<=12;i++)
    {
        temp*=10;
        dp[i]=dp[i-1]+temp;
    }
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%s",str);
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
using namespace std;
#define forall(i,n) for(int i=0;i<n;i++)
#define pb(x) push_back(x)
#define mp(a,b) make_pair(a,b)
#define bitcnt(x) __builtin_popcountll(x)
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN=100005;
const ll INF=1e18;
const ll MOD=1000000007;
char s[44];
int main()
{
	//string s;
	int n;
	cin>>n;
	while(n--)
	{
		scanf("%s",s);
		int k=strlen(s+4);
		int y=atoi(s+4);
		int F=0,ten=10;
		for(int i=0;i<k-1;i++)
		{
			F+=ten;
			ten*=10;
		}
		while(y<1989+F)
			y+=ten;
		printf("%d\n",y);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 1e6+1;

int main(){
//	freopen("read.txt","r",stdin);
	int n;
	string c;
	scanf("%d",&n);
	for(int i = 0 ; i < n ; ++i){
		  cin >> c;
		  string nm = "";
		  for(int j = c.size()-1; j >= 0; --j){
		  	   int x = c[j]-'0';
		  	   if(x >= 0 && x <= 9)
		  	   nm += c[j];
		       else break;
		  }
		  reverse(all(nm));
		  int num ;
		  stringstream ss;
		  ss << nm;
		  ss >> num;
		  if(nm.size() <= 3){
		  	  int y , ad = 0 ;
		  	  if(nm.size() == 1)y = 9;
		  	  if(nm.size() == 2)y = 89;
		  	  if(nm.size() == 3)y = 989, ad = 1;
		  	  y = y - num;
		  	  y = abs(y);
		  	  y += 1989;
              ss.clear();
              string t ;
              ss << y;
              ss >> t;
              t[0] += ad;
              for(int i = 0 ; i < 4-nm.size(); ++i)cout << t[i];
              cout << num << "\n";
		  }
		  else {
		  	  if(nm[0] == '2' || nm[0] == '0')cout << "1"<<nm<<"\n";
		  	  else if(nm[0] == '1')nm[0] = '2', cout << nm << "\n";
		  	  else cout << nm << "\n";
		  }
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 1e6+1;

int main(){
	//freopen("read.txt","r",stdin);
	int n;
	string c;
	scanf("%d",&n);
	for(int i = 0 ; i < n ; ++i){
		  cin >> c;
		  string nm = "";
		  for(int j = c.size()-1; j >= 0; --j){
		  	   int x = c[j]-'0';
		  	   if(x >= 0 && x <= 9)
		  	   nm += c[j];
		       else break;
		  }
		  reverse(all(nm));
		  int num ;
		  stringstream ss;
		  ss << nm;
		  ss >> num;
		  int p = 10, f = 0 ;
		  for(int i = 1; i < nm.size(); ++i){
		  	    f += p;
                p *= 10;
		  }
		  while(1989+f > num)num += p;
		  printf("%d\n",num);
	}
}


#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <map>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <list>
#include <unordered_set>
#include <cstdio>
#include <set>
#include <cctype>
#include <stack>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <iterator>
using namespace std;
#define ull  unsigned long long
#define rtn return 0
#include <math.h>
#include <deque>
//std::vector<size_t> a(2000002, 0);
//string a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//string a = "abcdefghijklmnopqrstuvwxyz";
//string alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
//const int base = 1000*1000*1000

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin.clear();
    fflush(stdin);
    //ifstream cin("input.txt");
    //ofstream cout("output.txt");
    int n;
    cin >> n;
    while(n--) {
        string s;
        cin >> s;
        int year = stoi(s.substr(4,s.size()-4)),p = 1,x = 1989;
        for(int i = 0;i < s.size()-5;i++) {
            p *= 10;
            x += p;
        }
        p *= 10;
        while(year < x) {
            year += p;
        }
        cout << year << endl;
    }
    return 0;
 }


#include <bits/stdc++.h>
using namespace std;

int t, ss, res;
string s;

void cal() {
	if (ss == 1) {
		if (res == 9)
			cout << "1989\n";
		else
			cout << 1990 + res << '\n';
	}
	else if (ss == 2) {
		if (res == 99)
			cout << "1999\n";
		else
			cout << res + 2000 << '\n';
	}
	else if (ss == 3) {
		if (res == 99)
			cout << "2099\n";
		else
			cout << 2000 + res << '\n';
	}
	else if (res % 10000 < 3000)
		cout << 1 << res << '\n';
	else if (s[4] == '0') {
		cout << 1;
		for (int i = 4; i < ss + 4; i++)
			cout << s[i];
		cout << '\n';
	}

	else
		cout << res << '\n';

}

int main() {
	ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> t;
	while (t--) {
		res = 0;
		cin >> s;
		ss = s.size();
		for (int i = 4; i < ss; i++)
			res *= 10, res += s[i] - '0';
		ss -= 4;
		cal();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int t, ss, res;
string s;

void cal() {
	if (ss == 1) {
		if (res == 9)
			cout << "1989\n";
		else
			cout << 1990 + res << '\n';
	}
	else if (ss == 2) {
		if (res == 99)
			cout << "1999\n";
		else
			cout << res + 2000 << '\n';
	}
	else if (ss == 3) {
		if (res == 99)
			cout << "2099\n";
		else
			cout << 2000 + res << '\n';
	}
	else if (res % 10000 < 3000)
		cout << 1 << res << '\n';
	else
		cout << res << '\n';

}

int main() {
	ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> t;
	while (t--) {
		res = 0;
		cin >> s;
		ss = s.size();
		for (int i = 4; i < ss; i++)
			res *= 10, res += s[i] - '0';
		ss -= 4;
		cal();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int t, ss, res;
string s;

void cal() {
	if (ss == 1) {
		if (res == 9)
			cout << "1989\n";
		else
			cout << 1990 + res << '\n';
	}
	else if (ss == 2) {
		if (res == 99)
			cout << "1999\n";
		else
			cout << res + 2000 << '\n';
	}
	else if (ss == 3) {
		if (res == 99)
			cout << "2099\n";
		else
			cout << 2000 + res << '\n';
	}
	else if (ss == 4 && res < 3000)
		cout << 1 << res << '\n';
	else
		cout << res << '\n';

}

int main() {
	ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> t;
	while (t--) {
		res = 0;
		cin >> s;
		ss = s.size();
		for (int i = 4; i < ss; i++)
			res *= 10, res += s[i] - '0';
		ss -= 4;
		cal();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int t, ss, res;
string s;

void output() {
	cout << 1;
	for (int i = 4; i < ss + 4; i++)
		cout << s[i];
	cout << '\n';
}

void cal() {
	if (ss == 1) {
		if (res == 9)
			cout << "1989\n";
		else
			cout << 1990 + res << '\n';
	}
	else if (ss == 2) {
		if (res == 99)
			cout << "1999\n";
		else
			cout << res + 2000 << '\n';
	}
	else if (ss == 3) {
		if (res >= 0 && res < 99)
			cout << 3000 + res << '\n';
		else
			cout << 2000 + res << '\n';
	}
	else if (ss == 4 && res >= 0 && res < 3099)
		cout << 1 << res << '\n';
	else if (ss == 5 && res >= 0 && res < 13099)
		cout << 1 << res << '\n';
	else if (ss == 6 && res >= 0 && res < 113099)
		cout << 1 << res << '\n';
	else if (ss == 7 && res >= 0 && res < 1113099)
		cout << 1 << res << '\n';
	else if (ss == 8 && res >= 0 && res < 11113099)
		cout << 1 << res << '\n';
	else if (ss == 9 && res >= 0 && res < 111113099)
		cout << 1 << res << '\n';
	else
		cout << res << '\n';
}

int main() {
	ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> t;
	while (t--) {
		res = 0;
		cin >> s;
		ss = s.size();
		for (int i = 4; i < ss; i++)
			res *= 10, res += s[i] - '0';
		ss -= 4;
		cal();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int t, ss, res;
string s;

bool check() {
	for (int i = 4; i < ss + 4; i++)
		if (s[i] != '1')
			return (ss - i + 1 <= 4? true: false);
	return true;
}

void cal() {
	if (ss == 1) {
		if (res == 9)
			cout << "1989\n";
		else
			cout << 1990 + res << '\n';
	}
	else if (ss == 2) {
		if (res == 99)
			cout << "1999\n";
		else
			cout << res + 2000 << '\n';
	}
	else if (ss == 3) {
		if (res == 99)
			cout << "2099\n";
		else
			cout << 2000 + res << '\n';
	}
	else if (res % 10000 < 3099 && check())
		cout << 1 << res << '\n';
	else if (s[4] == '0') {
		cout << 1;
		for (int i = 4; i < ss + 4; i++)
			cout << s[i];
		cout << '\n';
	}
	else
		cout << res << '\n';
}

int main() {
	ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> t;
	while (t--) {
		res = 0;
		cin >> s;
		ss = s.size();
		for (int i = 4; i < ss; i++)
			res *= 10, res += s[i] - '0';
		ss -= 4;
		cal();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int t, ss, res;
string s;

void cal() {
	if (ss == 1) {
		if (res == 9)
			cout << "1989\n";
		else
			cout << 1990 + res << '\n';
	}
	else if (ss == 2) {
		if (res == 99)
			cout << "1999\n";
		else
			cout << res + 2000 << '\n';
	}
	else if (ss == 3) {
		if (res >= 0 && res < 99)
			cout << 3000 + res << '\n';
		else
			cout << 2000 + res << '\n';
	}
	else if (ss == 4 && res >= 0 && res < 3099)
		cout << 1 << res << '\n';
	else if (ss == 5 && res >= 0 && res < 13099)
		cout << 1 << res << '\n';
	else if (ss == 6 && res >= 0 && res < 113099)
		cout << 1 << res << '\n';
	else if (ss == 7 && res >= 0 && res < 1113099)
		cout << 1 << res << '\n';
	else if (ss == 8 && res >= 0 && res < 11113099)
		cout << 1 << res << '\n';
	else if (ss == 9 && res >= 0 && res < 111113099)
		cout << 1 << res << '\n';
	else
		cout << res << '\n';
}

int main() {
	ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> t;
	while (t--) {
		res = 0;
		cin >> s;
		ss = s.size();
		for (int i = 4; i < ss; i++)
			res *= 10, res += s[i] - '0';
		ss -= 4;
		cal();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int t, ss, res;
string s;

void output() {
	cout << 1;
	for (int i = 4; i < ss + 4; i++)
		cout << s[i];
	cout << '\n';
}

void cal() {
	if (ss == 1)
		cout << (res == 9? 1989: 1990 + res) << '\n';
	else if (ss == 2)
		cout << (res == 99? 1999: 2000 + res) << '\n';
	else if (ss == 3)
		cout << (res >= 0 && res < 99? 3000 + res: 2000 + res) << '\n';
	else if (ss == 4 && res >= 0 && res < 3099)
		output();
	else if (ss == 5 && res >= 0 && res < 13099)
		output();
	else if (ss == 6 && res >= 0 && res < 113099)
		output();
	else if (ss == 7 && res >= 0 && res < 1113099)
		output();
	else if (ss == 8 && res >= 0 && res < 11113099)
		output();
	else if (ss == 9 && res >= 0 && res < 111113099)
		output();
	else
		cout << res << '\n';
}

int main() {
	ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> t;
	while (t--) {
		res = 0;
		cin >> s;
		ss = s.size();
		for (int i = 4; i < ss; i++)
			res *= 10, res += s[i] - '0';
		ss -= 4;
		cal();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int t, ss, res;
string s;

void cal() {
	if (ss == 1) {
		if (res == 9)
			cout << "1989\n";
		else
			cout << 1990 + res << '\n';
	}
	else if (ss == 2) {
		if (res == 99)
			cout << "1999\n";
		else
			cout << res + 2000 << '\n';
	}
	else if (ss == 3) {
		if (res == 999)
			cout << "2999\n";
		else
			cout << 3000 + res << '\n';
	}
	else if (ss == 4 && res >= 0 && res < 3099)
		cout << 1 << res << '\n';
	else if (ss == 5 && res >= 0 && res < 13099)
		cout << 1 << res << '\n';
	else if (ss == 6 && res >= 0 && res < 113099)
		cout << 1 << res << '\n';
	else if (ss == 7 && res >= 0 && res < 1113099)
		cout << 1 << res << '\n';
	else if (ss == 8 && res >= 0 && res < 11113099)
		cout << 1 << res << '\n';
	else if (ss == 9 && res >= 0 && res < 111113099)
		cout << 1 << res << '\n';
	else
		cout << res << '\n';
}

int main() {
	ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> t;
	while (t--) {
		res = 0;
		cin >> s;
		ss = s.size();
		for (int i = 4; i < ss; i++)
			res *= 10, res += s[i] - '0';
		ss -= 4;
		cal();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int t, ss, res;
string s;

bool check() {
	for (int i = 4; i < ss + 4; i++)
		if (s[i] != '1')
			return (ss - i + 1 <= 4? true: false);
	return true;
}

void cal() {
	if (ss == 1) {
		if (res == 9)
			cout << "1989\n";
		else
			cout << 1990 + res << '\n';
	}
	else if (ss == 2) {
		if (res == 99)
			cout << "1999\n";
		else
			cout << res + 2000 << '\n';
	}
	else if (ss == 3) {
		if (res == 99)
			cout << "2099\n";
		else
			cout << 2000 + res << '\n';
	}
	else if (res % 10000 < 3000 && check())
		cout << 1 << res << '\n';
	else if (s[4] == '0') {
		cout << 1;
		for (int i = 4; i < ss + 4; i++)
			cout << s[i];
		cout << '\n';
	}
	else
		cout << res << '\n';
}

int main() {
	ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> t;
	while (t--) {
		res = 0;
		cin >> s;
		ss = s.size();
		for (int i = 4; i < ss; i++)
			res *= 10, res += s[i] - '0';
		ss -= 4;
		cal();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int t, ss, res;
string s;

void output() {
	cout << 1;
	for (int i = 4; i < ss + 4; i++)
		cout << s[i];
	cout << '\n';
}

void cal() {
	if (ss == 1) {
		if (res == 9)
			cout << "1989\n";
		else
			cout << 1990 + res << '\n';
	}
	else if (ss == 2) {
		if (res == 99)
			cout << "1999\n";
		else
			cout << res + 2000 << '\n';
	}
	else if (ss == 3) {
		if (res >= 0 && res < 99)
			cout << 3000 + res << '\n';
		else
			cout << 2000 + res << '\n';
	}
	else if (ss == 4 && res >= 0 && res < 3099)
		output();
	else if (ss == 5 && res >= 0 && res < 13099)
		output();
	else if (ss == 6 && res >= 0 && res < 113099)
		output();
	else if (ss == 7 && res >= 0 && res < 1113099)
		output();
	else if (ss == 8 && res >= 0 && res < 11113099)
		output();
	else if (ss == 9 && res >= 0 && res < 111113099)
		output();
	else
		cout << res << '\n';
}

int main() {
	ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> t;
	while (t--) {
		res = 0;
		cin >> s;
		ss = s.size();
		for (int i = 4; i < ss; i++)
			res *= 10, res += s[i] - '0';
		ss -= 4;
		cal();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int t, ss, res;
string s;

void cal() {
	if (ss == 1) {
		if (res == 9)
			cout << "1989\n";
		else
			cout << 1990 + res << '\n';
	}
	else if (ss == 2) {
		if (res == 99)
			cout << "1999\n";
		else
			cout << res + 2000 << '\n';
	}
	else if (ss == 3) {
		if (res == 99)
			cout << "2099\n";
		else
			cout << 2000 + res << '\n';
	}
	else if (res % 10000 < 3000 || s[4] == '0')
		cout << 1 << res << '\n';
	else
		cout << res << '\n';

}

int main() {
	ios:: sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
	cin >> t;
	while (t--) {
		res = 0;
		cin >> s;
		ss = s.size();
		for (int i = 4; i < ss; i++)
			res *= 10, res += s[i] - '0';
		ss -= 4;
		cal();
	}
	return 0;
}


#include<iostream>
#include<iomanip>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<utility>
#include<limits.h>
#include<functional>
#include<algorithm>

using namespace std;

int xx[8]={-1,-1,-1,0,0,1,1,1};
int yy[8]={-1,0,1,-1,1,-1,0,1};
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
const double pi = 3.14159265358;

#define DEBUG(x) 	cout << '>' << #x << ':' << x << endl;
#define MOD 		1000000007
#define MAX 		1e9
#define MIN    		-1e9

typedef long long lli;
typedef long li;

int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
li t,n,i,year,tenpow;
string s;

cin>>t;
while(t--){
cin>>s;

    li k=s.size();
s=s.substr(4,k-4);
 k=s.size();

stringstream ss(s);
ss>>year;

li sum=0;
tenpow=10;

for(i=1;i<k;i++){
sum+=tenpow;
tenpow*=10;
}

while(sum+1989>year)
    year+=tenpow;

    cout<<year<<endl;
    }
return 0;
}


/*
PROG: cf664c
LANG: C++
debug my code
*/
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#include <numeric>
#include <functional>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cassert>
#include <complex>
#include <iomanip>

using namespace std;

int N;
void solve(int a, string b)
{
	int x = b.length();
	//between 1989 + (10^(x - 1)) and 1989 + (10^(x) - 1)
	int hpw = 1, lpw;
	for (int i = 0; i < x; i++)
	{
		hpw *= 10;
		hpw++;
	}
//	cerr << hpw << endl;
	lpw = hpw/10;
	int low = 1989 + lpw - 1;
	int high = 1989 + hpw - 2;
//	cerr << low << ' ' << high << endl;
	int d = high - low + 1;
	int val = (low/d) * d + a;
	if (low <= val && val <= high)
	{
		cout << val << '\n';
	}
	else
	{
		cout << val + d << '\n';
	}
	return;
//	cerr << d << endl;
	//10, 100, 1000, 10000
	//11, 111, 1111
	//10, 110, 1
	//find the number that is in this range that ends with b
	//find the number that is b mod pw

}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	if (fopen("cf664c.in", "r"))
	{
		freopen ("cf664c.in", "r", stdin);
		freopen ("cf664c.out", "w", stdout);
	}
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		string s;
		cin >> s;
		s.erase(0, 4);
		int t = stoi(s);
		solve(t, s);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define f(i,x,y) for(int i = (x);i < (y);++i)
#define F(i,x,y) for(int i = (x);i > (y);--i)
int mod(int n){if(n < 0)return ((-1)*n);else return n;}
int min(int a,int b){return ((a > b)?b:a);}
int max(int a,int b){return ((a < b)?b:a);}

string func(string str)
{
    if(str.size() == 1)
    {
        if(str == "9")return "1989";
        else return ("199"+str);
    }
    else if(str.size() == 2)
    {
        if(str == "99")return "1999";
        else return ("20"+str);
    }
    else if(str.size() == 3)
    {
        if(str >= "099")return ("2"+str);
        else return ("3"+str);
    }
    else
    {
        int c = str.size()-4;
        string st = "";
        f(i,0,c)st += '1';
        string chk = st+"3099";
        if(str >= chk)return str;
        else return ("1"+str);
    }
}

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        string str;
        cin >> str;
        string cr = "";
        for(int i = 4;i < str.size();++i)
         cr += str[i];
        cout << func(cr) << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=1e5+5;

void show_year(string str){
     ll i, j, k;
     ll l=str.length(), sm=1989;
     for(i=1, j=10; i<l; ++i){
          sm+=j;
          j*=10;
     }
     ll cur=atoi(str.c_str());
     while(cur<sm) cur+=j;
     cout << cur << endl;
}

int main(){
     ll i, j, k;
     ll n;
     cin >> n;
     while(n--){
          string str;
          cin >> str;
          show_year(str.substr(4, str.length()-4));
     }
}


#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
char s[35] ;
int main()
{
    int n ;
    scanf("%d",&n);
    while(n--){
        char ch ;
        while(scanf("%c",&ch) == 1 && ch != 39 ) ;
        scanf("%s",s);
        int len = strlen(s) ;
        if(len < 4 ){
            if(len == 1){
                if(s[0] == '9')     printf("1989\n") ;
                else printf("199%c\n",s[0]) ;
            }
            else if(len == 2){
                if(strcmp("99",s) == 0)     printf("1999\n") ;
                else if(strcmp("00" , s) == 0)      printf("2000\n");
                else printf("20%s\n",s) ;
            }
            else{
                if(strcmp(s ,"098") <= 0)   printf("3%s\n" , s ) ;
                else    printf("2%s\n", s ) ;
            }
        }
        else{
           if(strcmp( s + len - 4 , "3098" ) <= 0 )     printf("1%s\n" , s ) ;
           else         printf("%s\n" , s ) ;
        }
    }
    return 0;
}


#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
char s[35] ;
int main()
{
    int n ;
    scanf("%d",&n);
    while(n--){
        char ch ;
        while(scanf("%c",&ch) == 1 && ch != 39 ) ;
        scanf("%s",s);
        int len = strlen(s) ;
        if(len < 4 ){
            if(len == 1){
                if(s[0] == '9')     printf("1989\n") ;
                else printf("199%c\n",s[0]) ;
            }
            else if(len == 2){
                if(strcmp("99",s) == 0)     printf("1999\n") ;
                else if(strcmp("00" , s) == 0)      printf("2000\n");
                else printf("20%s\n",s) ;
            }
            else{
                if(strcmp(s ,"098") <= 0)   printf("3%s\n" , s ) ;
                else    printf("2%s\n", s ) ;
            }
        }
        else{
            if(s[0] == '0' ){
                printf("1%s\n",s);
                continue;
            }
            int flag = 1;
            for(int i = 0 ; i<len - 4 ; i++){
                if(len == 4)    break;
                if(s[i] != '1' ){
                    flag = 0;   break;
                }
            }
            if(flag){
                if(strcmp( s + len - 4 , "3098" ) <= 0 )     printf("1%s\n" , s ) ;
                else        printf("%s\n", s ) ;
            }
           else         printf("%s\n" , s ) ;
        }
    }
    return 0;
}


#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
char s[35] ;
int main()
{
    int n ;
    scanf("%d",&n);
    while(n--){
        char ch ;
        while(scanf("%c",&ch) == 1 && ch != 39 ) ;
        scanf("%s",s);
        int len = strlen(s) ;
        if(len < 4 ){
            if(len == 1){
                if(s[0] == '9')     printf("1989\n") ;
                else printf("199%c\n",s[0]) ;
            }
            else if(len == 2){
                if(strcmp("99",s) == 0)     printf("1999\n") ;
                else if(strcmp("00" , s) == 0)      printf("2000\n");
                else printf("20%s\n",s) ;
            }
            else{
                if(strcmp(s ,"098") <= 0)   printf("3%s\n" , s ) ;
                else    printf("2%s\n", s ) ;
            }
        }
        else{
            int flag = 1;
            for(int i = 0 ; i<len - 4 ; i++){
                if(len == 4)    break;
                if(s[i] != '1' ){
                    flag = 0;   break;
                }
            }
            if(flag){
                if(strcmp( s + len - 4 , "3098" ) <= 0 )     printf("1%s\n" , s ) ;
                else        printf("%s\n", s ) ;
            }
           else         printf("%s\n" , s ) ;
        }
    }
    return 0;
}


#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
char s[35] ;
int main()
{
    int n ;
    scanf("%d",&n);
    while(n--){
        char ch ;
        while(scanf("%c",&ch) == 1 && ch != 39 ) ;
        scanf("%s",s);
        int len = strlen(s) ;
        if(len < 4 ){
            if(len == 1){
                if(s[0] == '9')     printf("1989\n") ;
                else printf("199%c\n",s[0]) ;
            }
            else if(len == 2){
                if(strcmp("99",s) == 0)     printf("1999\n") ;
                else if(strcmp("00" , s) == 0)      printf("2000\n");
                else printf("20%s\n",s) ;
            }
            else{
                if(strcmp(s ,"098") <= 0)   printf("3%s\n" , s ) ;
                else    printf("2%s\n", s ) ;
            }
        }
        else{
            if(len == 4){
                if(strcmp(s , "3098") <= 0 )    printf("1%s\n",s);
                else    printf("%s\n" , s ) ;
            }
            else if(len == 5){
                if(strcmp(s , "13098") <= 0 )    printf("1%s\n",s);
                else    printf("%s\n" , s ) ;
            }
            else if(len == 6){
                if(strcmp(s , "113098") <= 0 )    printf("1%s\n",s);
                else    printf("%s\n" , s ) ;
            }
            else if(len == 7){
                if(strcmp(s , "1113098") <= 0 )    printf("1%s\n",s);
                else    printf("%s\n" , s ) ;
            }
            else if(len == 8){
                if(strcmp(s , "11113098") <= 0 )    printf("1%s\n",s);
                else    printf("%s\n" , s ) ;
            }
            else if(len == 9){
                if(strcmp(s , "111113098") <= 0 )    printf("1%s\n",s);
                else    printf("%s\n" , s ) ;
            }
        }
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <list>
#include <cmath>
#include <cstdlib>
#include <locale>
#include <map>
#include <set>
using namespace std;
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, s, n) for(int i = (s); i < (n); ++i)
#define per1(i, a, b) for(int i = a; i >= b; --i)
#define mkp make_pair
#define pb push_back
#define F first
#define S second
#define ALL(I) (I).begin(), (I).end()
#define mod 1000000007
#define MAX 10005
typedef const void CV;
typedef long long LL;
typedef unsigned long long ULL;
char check[10][100]={ "0", "9", "99", "099", "3099", "13099", "113099", "1113099", "11113099", "111113099"};
LL cmp[10] = {0, 1989, 1999, 2099, 3099, 13099, 113099, 1113099, 11113099, 111113099};
char in[100];
int main(void){
    int n; scanf("%d", &n);
    rep(i, n){
        scanf("%s", in);
        int l = strlen(in) - 4;
        LL v = atol(&in[4]);
        LL vv = atol(check[l]);

        if(strcmp(check[l], &in[4]) > 0){
            LL p = 1; rep(j, l) p *= 10;
            printf("%lld\n",  cmp[l] + p + v - vv);
        }
        else printf("%lld\n", cmp[l] + v - vv);

    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <list>
#include <cmath>
#include <cstdlib>
#include <locale>
#include <map>
#include <set>
using namespace std;
#define MS0(I) memset(I, 0, sizeof(I));
#define MS1(I) memset(I, -1, sizeof(I));
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, s, n) for(int i = (s); i < (n); ++i)
#define per1(i, a, b) for(int i = a; i >= b; --i)
#define mkp make_pair
#define pb push_back
#define F first
#define S second
#define ALL(I) (I).begin(), (I).end()
#define mod 1000000007
#define MAX 10005
typedef const void CV;
typedef long long LL;
typedef unsigned long long ULL;
char check[10][100]={ "0", "9", "99", "099", "3099", "13099", "113099", "1113099", "11130999", "111130999"};
LL cmp[10] = {0, 1989, 1999, 2099, 3099, 13099, 113099, 1113099, 11130999, 111130999};
char in[100];
int main(void){
    int n; scanf("%d", &n);
    rep(i, n){
        scanf("%s", in);
        int l = strlen(in) - 4;
        LL v = atol(&in[4]);
        LL vv = atol(check[l]);

        if(strcmp(check[l], &in[4]) > 0){
            LL p = 1; rep(j, l) p *= 10;
            printf("%lld\n",  cmp[l] + p + v - vv);
        }
        else printf("%lld\n", cmp[l] + v - vv);

    }
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<int, int> m;

void build_map() {
	int d[4]{10, 100, 1000, 10000};
	for (int n = 1989; n <= 9999; n++) {
		for (int i = 0; i < 4; i++) {
			int r = n % d[i];
			if (!m[r]) {
				m[r] = n;
				break;
			}
		}
	}
}

int recover(int x) {
	if (x <= 999) return m[x];
	int n = (int)pow(10, (int)log10(x));
	return recover(x % n) < x ? x : x + 10 * n;
}

int main() {
	build_map();
    cout << m[9];
    int n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(stoi(year)) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<int, int> m;

void build_map() {
	int d[4]{10, 100, 1000, 10000};
	for (int n = 1989; n <= 9999; n++) {
		for (int i = 0; i < 4; i++) {
			int r = n % d[i];
			if (!m[r]) {
				m[r] = n;
				break;
			}
		}
	}
}

int recover(int x) {
	if (x <= 999) return m[x];
	int n = (int)pow(10, (int)log10(x));
	return recover(x % n) < x ? x : x + 10 * n;
}

int main() {
	build_map();

    int n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(stoi(year)) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	int k = (int)str.size() - 4;
    	int year = stoi(str.substr(4, k));

    	int power = 10, min = 1989;
    	for (int i = 1; i < k; i++) {
    		min += power;
    		power *= 10;
    	}

    	while (year < min) year += power;
    	cout << year << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<string, string> m;

void build_map() {
	for (int n = 1989; n <= 9999; n++) {
		string year = to_string(n);
		for (int i = 3; i >= 0; i--){
			string tail = year.substr(i, 4 - i);
			if (m.find(tail) != m.end()) {
				m[tail] = year;
				break;
			}
		}
	}
}

string recover(string s) {
	if ((int)s.size() <= 3) return m[s];
	string tail = s.substr(1, (int)tail.size() - 1);
	string tail_year = recover(tail);

	if (tail_year.size() < s.size() || tail_year[0] < s[0]) {
		return s;
	} else {
		return "1" + s;
	}
}

int main() {
	build_map();

    int n;
    cin >> n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(year) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<string, string> m;

void build_map() {
	for (int n = 1989; n <= 9999; n++) {
		string year = to_string(n);
		for (int i = 3; i >= 0; i--){
			string tail = year.substr(i, 4 - i);
			if (m.find(tail) != m.end()) {
				m[tail] = year;
				break;
			}
		}
	}
}

string recover(string s) {
	if ((int)s.size <= 3) return m[s];
	string tail = s.substr(1, (int)tail.size() - 1);
	string tail_year = recover(tail);

	if (tail_year.size() < s.size() || tail_year[0] < s[0]) {
		return s;
	} else {
		return "1" + s;
	}
}

int main() {
	build_map();

    int n;
    cin >> n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(year) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<int, int> m;

void buid_map() {
	int d[4]{10, 100, 1000, 10000};
	for (int n = 1989; n <= 9999; n++) {
		for (int i = 0; i < 4; i++) {
			int r = n % d[i];
			if (!m[r]) {
				m[r] = n;
				break;
			}
		}
	}
}

int recover(int x) {
	if (x <= 999) return m[x];
	int n = (int)pow(10, (int)log10(x));
	return recover(x % n) < x ? x : x + 10 * n;
}

int main() {
	build_map();

    int n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(stoi(year)) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<int, int> map;

void buid_map() {
	int d[4]{10, 100, 1000, 10000};
	for (int n = 1989; n <= 9999; n++) {
		for (int i = 0; i < 4; i++) {
			int r = n % d[i];
			if (!map[r]) {
				map[r] = n;
				break;
			}
		}
	}
}

int recover(int x) {
	if (x <= 999) return map[x];
	int n = (int)pow(10, (int)log10(x));
	return recover(x % n) < x ? x : x + 10 * n;
}

int main() {
	build_map();

    int n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(stoi(year)) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<string, string> m;

void build_map() {
	for (int n = 1989; n <= 9999; n++) {
		string year = to_string(n);
		for (int i = 3; i >= 0; i--){
			string tail = year.substr(i, 4 - i);
			if (m.find(tail) != m.end()) {
				m[tail] = year;
				break;
			}
		}
	}
}

string recover(string s) {
	if ((int)s.size <= 3) return m[x];
	string tail = s.substr(1, (int)tail.size() - 1);
	string tail_year = recover(tail);

	if (tail_year.size() < s.size() || tail_year[0] < s[0]) {
		return s;
	} else {
		return "1" + s;
	}
}

int main() {
	build_map();

    int n;
    cin >> n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(year) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<int, int> m;

void build_map() {
	int d[4]{10, 100, 1000, 10000};
	for (int n = 1989; n <= 9999; n++) {
		for (int i = 0; i < 4; i++) {
			int r = n % d[i];
			if (!m[r]) {
				m[r] = n;
				break;
			}
		}
	}
}

int recover(int x) {
	if (x <= 999) return m[x];
	int n = (int)pow(10, (int)log10(x));
	return recover(x % n) < x ? x : x + 10 * n;
}

int main() {
	build_map();

    int n;
    cin >> n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(stoi(year)) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    while(n--) {
    	string str;
    	cin >> str;
    	int k = (int)str.size() - 4;
    	int year = stoi(str.substr(4, k));

    	int power = 10, min = 1989;
    	for (int i = 1; i < k; i++) {
    		min += power;
    		power *= 10;
    	}

    	while (year < min) year += power;
    	cout << year << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<string, string> m;

void build_map() {
	for (int n = 1989; n <= 9999; n++) {
		string year = to_string(n);
		for (int i = 3; i >= 0; i--){
			string tail = year.substr(i, 4 - i);
			if (m.find(tail) == m.end()) {
				m[tail] = year;
				break;
			}
		}
	}
}

string recover(string s) {
	if ((int)s.size() <= 3) return m[s];
	string tail = s.substr(1, (int)tail.size() - 1);
	string tail_year = recover(tail);

	if (s == "111112940") cout << "TOMDEBUG tail_year: "<< tail_year << endl;
	if (tail_year.size() < s.size() || tail_year[0] < s[0]) {
		return s;
	} else {
		return "1" + s;
	}
}

int main() {
	build_map();

    int n;
    cin >> n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(year) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<string, string> m;

void build_map() {
	for (int n = 1989; n <= 9999; n++) {
		string year = to_string(n);
		for (int i = 3; i >= 0; i--){
			string tail = year.substr(i, 4 - i);
			if (m.find(tail) == m.end()) {
				m[tail] = year;
				break;
			}
		}
	}
}

string recover(string s) {
	if ((int)s.size() <= 3) return m[s];
	string tail = s.substr(1, (int)tail.size() - 1);
	string tail_year = recover(tail);

	if (s == "2940") cout << "TOMDEBUG tail_year: "<< tail_year << endl;
	if (tail_year.size() < s.size() || tail_year[0] < s[0]) {
		return s;
	} else {
		return "1" + s;
	}
}

int main() {
	build_map();

    int n;
    cin >> n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(year) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<string, string> m;

void build_map() {
	for (int n = 1989; n <= 9999; n++) {
		string year = to_string(n);
		for (int i = 3; i >= 0; i--){
			string tail = year.substr(i, 4 - i);
			if (m.find(tail) == m.end()) {
				m[tail] = year;
				break;
			}
		}
	}
}

string recover(string s) {
	if ((int)s.size() <= 3) return m[s];
	string tail = s.substr(1, (int)s.size() - 1);
	string tail_year = recover(tail);

	if (tail_year.size() < s.size() || tail_year[0] < s[0]) {
		return s;
	} else {
		return "1" + s;
	}
}

int main() {
	build_map();

    int n;
    cin >> n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(year) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<string, string> m;

void build_map() {
	for (int n = 1989; n <= 9999; n++) {
		string year = to_string(n);
		for (int i = 3; i >= 0; i--){
			string tail = year.substr(i, 4 - i);
			if (m.find(tail) == m.end()) {
				m[tail] = year;
				break;
			}
		}
	}
}

string recover(string s) {
	if ((int)s.size() <= 3) return m[s];
	string tail = s.substr(1, (int)tail.size() - 1);
	string tail_year = recover(tail);

	if (tail_year.size() < s.size() || tail_year[0] < s[0]) {
		return s;
	} else {
		return "1" + s;
	}
}

int main() {
	build_map();

    int n;
    cin >> n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(year) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<string, string> m;

void build_map() {
	for (int n = 1989; n <= 9999; n++) {
		string year = to_string(n);
		for (int i = 3; i >= 0; i--){
			string tail = year.substr(i, 4 - i);
			if (!m[tail]) {
				m[tail] = year;
				break;
			}
		}
	}
}

string recover(string s) {
	if ((int)s.size <= 3) return m[x];
	string tail = s.substr(1, (int)tail.size() - 1);
	string tail_year = recover(tail);

	if (tail_year.size() < s.size() || tail_year[0] < s[0]) {
		return s;
	} else {
		return "1" + s;
	}
}

int main() {
	build_map();

    int n;
    cin >> n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(year) << endl;
    }
}


#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cstring>
#include <vector>
#include <math.h>
#include <stack>
#include <map>
#include <algorithm>

using namespace std;

map<string, string> m;

void build_map() {
	for (int n = 1989; n <= 9999; n++) {
		string year = to_string(n);
		for (int i = 3; i >= 0; i--){
			string tail = year.substr(i, 4 - i);
			if (m.find(tail) == m.end()) {
				m[tail] = year;
				break;
			}
		}
	}
}

string recover(string s) {
	if ((int)s.size() <= 3) return m[s];
	string tail = s.substr(1, (int)s.size() - 1);
	string tail_year = recover(tail);

	if ((tail_year.size() < s.size() && s[0] != '0') || tail_year[0] < s[0]) {
		return s;
	} else {
		return "1" + s;
	}
}

int main() {
	build_map();

    int n;
    cin >> n;
    while(n--) {
    	string str, year;
    	cin >> str;
    	year = str.substr(4, (int)str.size() - 4);
    	cout << recover(year) << endl;
    }
}


#include <iostream>
using namespace std;
int l;
char n[100];
long long m(int x,long long u)
{
	int z=l-x;
	long long k=1,u1=0;
	for (int i=0;i<z;i++)
	{
		u1+=(n[l-1-i]-'0')*k;
		k*=10;
	}
	u1+=(u/k)*k;
	if (u1<=u)
	{
		u1+=k;
	}
	return u1;
}
int main()
{
	int t;
	cin>>t;
	while (t--)
	{
	    cin>>n;
	    l=strlen(n);
	    long long u=1988;
		for (int i=l-1;i>=4;i--)
	    {
	    	u=m(i,u);
		}
		cout<<u<<endl;
    }
	return 0;
}


#include <iostream>
#include <cstring>
using namespace std;
int l;
char n[100];
long long m(int x,long long u)
{
	int z=l-x;
	long long k=1,u1=0;
	for (int i=0;i<z;i++)
	{
		u1+=(n[l-1-i]-'0')*k;
		k*=10;
	}
	u1+=(u/k)*k;
	if (u1<=u)
	{
		u1+=k;
	}
	return u1;
}
int main()
{
	int t;
	cin>>t;
	while (t--)
	{
	    cin>>n;
	    l=strlen(n);
	    long long u=1988;
		for (int i=l-1;i>=4;i--)
	    {
	    	u=m(i,u);
		}
		cout<<u<<endl;
    }
	return 0;
}


#include <bits/stdc++.h>
#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define VAR(x) #x ": " << x << " "
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define gcd __gcd
#define x first
#define y second
#define st first
#define nd second
#define pb push_back

using namespace std;

template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; }
template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; }

typedef long long LL;
typedef pair<int, int> PII;
typedef long double K;
typedef vector<int> VI;

const int dx[] = {0,0,-1,1}; //1,1,-1,1};
const int dy[] = {-1,1,0,0}; //1,-1,1,-1};

long long n, k;

int len(long long x){
	int c = 0;
	do{
		++c;
		x /= 10;
	}while(x);
	return c;
}

int main(){
	scanf("%d\n", &n);
	while(n--){
		scanf("IAO'%lld\n", &k);
		int d = len(k);
		long long s = 0, p = 10;
		FWD(i,1,d){
			s += p;
			p *= 10;
		}
		k += (max(0, 1989+s-k+p-1))/p*p;
		printf("%lld\n", k);
	}
	return 0;
}


#include <bits/stdc++.h>
#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define VAR(x) #x ": " << x << " "
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define gcd __gcd
#define x first
#define y second
#define st first
#define nd second
#define pb push_back

using namespace std;

template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; }
template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; }

typedef long long LL;
typedef pair<int, int> PII;
typedef long double K;
typedef vector<int> VI;

const int dx[] = {0,0,-1,1}; //1,1,-1,1};
const int dy[] = {-1,1,0,0}; //1,-1,1,-1};

long long n, k;

int len(long long x){
	int c = 0;
	do{
		++c;
		x /= 10;
	}while(x);
	return c;
}

int main(){
	scanf("%d\n", &n);
	while(n--){
		scanf("IAO'%lld\n", &k);
		int d = len(k);
		long long s = 0, p = 10;
		FWD(i,1,d){
			s += p;
			p *= 10;
		}
		k += (max(0LL, 1989+s-k+p-1))/p*p;
		printf("%lld\n", k);
	}
	return 0;
}


#include <bits/stdc++.h>
#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define VAR(x) #x ": " << x << " "
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define gcd __gcd
#define x first
#define y second
#define st first
#define nd second
#define pb push_back

using namespace std;

template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; }
template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; }

typedef long long LL;
typedef pair<int, int> PII;
typedef long double K;
typedef vector<int> VI;

const int dx[] = {0,0,-1,1}; //1,1,-1,1};
const int dy[] = {-1,1,0,0}; //1,-1,1,-1};

long long n, k;
char buff[23];

int main(){
	scanf("%d\n", &n);
	while(n--){
		scanf("%s", buff);
		sscanf(buff+4, "%lld", &k);
		int d = strlen(buff)-4;
		long long s = 0, p = 10;
		FWD(i,1,d){
			s += p;
			p *= 10;
		}
		k += (max(0LL, 1989+s-k+p-1))/p*p;
		printf("%lld\n", k);
	}
	return 0;
}


#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <bitset>
#include <string>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define inf (0x3f3f3f3f)
#define lnf (0x3f3f3f3f3f3f3f3f)
#define eps (1e-6)
int sgn(double a) {
    return a < -eps ? -1 : a < eps ? 0 : 1;
}

void debug(string a, auto b) {
    cout << "[debug] ";
    cout << a << " = " << b << endl;
}

//--------------------------

const ll mod = 1000000007;
const int maxn = 100010;

struct Edge {
    int to;
    char col;
};

vector<Edge> edge[maxn];
bool vis[maxn];

char s[42];

void go(char tar) {

}

int  dfs(char tar, int u, int par) {

}

int n, m;

void solve() {
    int n;
    scanf("%d", &n);
    while (n--) {
        scanf(" %s", s);
        int k = strlen(s + 4), year = atoi(s + 4), F = 0, tenPow = 10;
        for (int i = 1; i < k; i++) {
            F += tenPow;
            tenPow *= 10;
        }
        while (year < 1989 + F) year += tenPow;
        printf("%d\n", year);
    }
}

int main() {

#ifndef ONLINE_JUDGE
    freopen("1.in", "r", stdin);
    //freopen("1.out", "w", stdout);
#endif
    // iostream::sync_with_stdio(false);
    solve();
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

ll string_to_ll(string x)
{
	ll ans_local = 0;
	for (ll i = 0; i < x.size(); i++)
		ans_local = ans_local*10 + x[i] - '0';
	return ans_local;
}

void solve(string x)
{
	ll n = x.size(), ans = string_to_ll(x), lim1 = 1989;
	for (ll i = 1; i < n; i++)
		lim1 += (ll)pow(10, i);
	ll pre = lim1/(ll)pow(10, n);
	while (pre*(ll)pow(10, n) + ans < lim1)
		pre++;
	cout<<pre*(ll)pow(10, n) + ans<<endl;
}

int main()
{
	ios::sync_with_stdio(false);
	ll t;
	cin>>t;
	while (t--)
	{
		string s;
		cin>>s;
		string temp = "";
		for (ll i = 4; i < s.size(); i++)
			temp += s[i];
		solve(temp);
	}
	return 0;
}


/*input
4 3
1 2 R
2 3 B
1 3 B
*/
#include <bits/stdc++.h>

using namespace std;

#define INF (int)(1e8)

void show_set(set <int> x)
{
	cout<<x.size()<<endl;
	for (auto a: x)
		cout<<a<<" ";
	cout<<endl;
}

struct node
{
	vector <int> adj;
	vector <char> ec;
	int color;
}graph[1000001];

int n, m;
vector <int> temp1, temp2;

void initialize()
{
	temp1.clear(), temp2.clear();
	for (int i = 1; i <= n; i++)
		graph[i].color = -1;
}

bool dfs_visit(int u, char x)
{
	bool ans = 1;
	for (int i = 0; i < graph[u].adj.size(); i++)
	{
		int v = graph[u].adj[i];
		char c = graph[u].ec[i];
		if (graph[v].color == -1)
		{
			if (c == x)
				graph[v].color = graph[u].color;
			else
				graph[v].color = !graph[u].color;

			ans &= dfs_visit(v, x);
		}
		else
		{
			if ((c == x && graph[v].color != graph[u].color) ||
				(c != x && graph[v].color == graph[u].color))
				ans = 0;
		}
	}
	return ans;
}

set <int> for_red, for_blue;
int solve_for(char x)
{
	initialize();

	bool flag = 1;
	for (int i = 1; i <= n; i++)
		if (graph[i].color == -1)
		{
			if (graph[i].adj.size())
			{
				graph[i].color = 0;
				flag &= dfs_visit(i, x);
			}
		}

	if (!flag)
		return INF;
	else
	{
		int ans = 0;
		for (int i = 1; i <= n; i++)
			if (graph[i].color == 1)
				temp1.push_back(i);
			else if (graph[i].color == 0)
				temp2.push_back(i);

		if (temp1.size() < temp2.size())
		{
			if (x == 'R')
				for (auto u: temp1)
					for_red.insert(u);
			else
				for (auto u: temp1)
					for_blue.insert(u);
		}
		else
		{
			if (x == 'R')
				for (auto u: temp2)
					for_red.insert(u);
			else
				for (auto u: temp2)
					for_blue.insert(u);
		}

		if (x == 'R')
			return for_red.size();
		else
			return for_blue.size();
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>m;
	int u, v; char c;
	while (m--)
	{
		cin>>u>>v>>c;
		graph[u].adj.push_back(v); graph[v].adj.push_back(u);
		graph[u].ec.push_back(c); graph[v].ec.push_back(c);
	}

	int r = solve_for('R'), b = solve_for('B');
	if (r == INF && b == INF)
		cout<<"-1"<<endl;
	else if (r < b)
		show_set(for_red);
	else
		show_set(for_blue);
	return 0;
}


/*input
4 3
1 2 R
2 3 B
1 3 B
*/
#include <bits/stdc++.h>

using namespace std;

#define INF (int)(1e8)

void show_set(set <int> x)
{
	cout<<x.size()<<endl;
	for (auto a: x)
		cout<<a<<" ";
	cout<<endl;
}

struct node
{
	vector <int> adj;
	vector <char> ec;
	int color;
}graph[1000001];

int n, m;
vector <int> temp1, temp2;

void initialize()
{
	temp1.clear(), temp2.clear();
	for (int i = 1; i <= n; i++)
		graph[i].color = -1;
}

bool dfs_visit(int u, char x)
{
	bool ans = 1;
	for (int i = 0; i < graph[u].adj.size(); i++)
	{
		int v = graph[u].adj[i];
		char c = graph[u].ec[i];
		if (graph[v].color == -1)
		{
			if (c == x)
				graph[v].color = graph[u].color;
			else
				graph[v].color = !graph[u].color;

			ans &= dfs_visit(v, x);
		}
		else
		{
			if ((c == x && graph[v].color != graph[u].color) ||
				(c != x && graph[v].color == graph[u].color))
				ans = 0;
		}
	}
	return ans;
}

set <int> for_red, for_blue;
int solve_for(char x)
{
	initialize();

	bool flag = 1;
	for (int i = 1; i <= n; i++)
		if (graph[i].color == -1)
		{
			if (graph[i].adj.size())
			{
				graph[i].color = 0;
				flag &= dfs_visit(i, x);
			}
		}

	if (!flag)
		return INF;
	else
	{
		int ans = 0;
		for (int i = 1; i <= n; i++)
			if (graph[i].color == 1)
				temp1.push_back(i);
			else if (graph[i].color == 0)
				temp2.push_back(i);

		if (temp1.size() < temp2.size())
		{
			if (x == 'R')
			{
				for_red.clear();
				for (auto u: temp1)
					for_red.insert(u);
			}
			else
			{
				for_blue.clear();
				for (auto u: temp1)
					for_blue.insert(u);
			}
		}
		else
		{
			if (x == 'R')
			{
				for_red.clear();
				for (auto u: temp2)
					for_red.insert(u);
			}
			else
			{
				for_blue.clear();
				for (auto u: temp2)
					for_blue.insert(u);
			}
		}

		if (x == 'R')
			return for_red.size();
		else
			return for_blue.size();
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>m;
	int u, v; char c;
	while (m--)
	{
		cin>>u>>v>>c;
		graph[u].adj.push_back(v); graph[v].adj.push_back(u);
		graph[u].ec.push_back(c); graph[v].ec.push_back(c);
	}

	int r = solve_for('R'), b = solve_for('B');
	if (r == INF && b == INF)
		cout<<"-1"<<endl;
	else if (r < b)
		show_set(for_red);
	else
		show_set(for_blue);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int string_to_int(string x)
{
	int ans_local = 0;
	for (int i = 0; i < x.size(); i++)
		ans_local = ans_local*10 + x[i] - '0';
	return ans_local;
}

void solve(string x)
{
	int n = x.size(), ans = string_to_int(x), lim1 = 1989;
	for (int i = 1; i < n; i++)
		lim1 += pow(10, i);
	int pre = lim1/pow(10, n);
	while (pre*pow(10, n) + ans < lim1)
		pre++;
	cout<<pre*pow(10, n) + ans<<endl;
}

int main()
{
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while (t--)
	{
		string s;
		cin>>s;
		string temp = "";
		for (int i = 4; i < s.size(); i++)
			temp += s[i];
		solve(temp);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long

void solve(string x)
{
	ll n = x.size(), ans = stoull(x), lim1 = 1989;
	for (ll i = 1; i < n; i++)
		lim1 += (ll)pow(10, i);
	ll pre = lim1/(ll)pow(10, n);
	while (pre*(ll)pow(10, n) + ans < lim1)
		pre++;
	cout<<pre*(ll)pow(10, n) + ans<<endl;
}

int main()
{
	ios::sync_with_stdio(false);
	ll t;
	cin>>t;
	while (t--)
	{
		string s;
		cin>>s;
		string temp = "";
		for (ll i = 4; i < s.size(); i++)
			temp += s[i];
		solve(temp);
	}
	return 0;
}


 #include<bits/stdc++.h>
#define ll long long
using namespace std;
ll E(ll a)
{
	ll ans=1;for(ll i=0;i<a;i++) ans*=10;
	return ans;
}
ll make_num(string s)
{
	ll ans=0;
	for(ll i=0;i<s.size();i++) ans=ans*10+s[i]-'0';
	return ans;
}
ll f(string s)//whitch year makes num?
{
	if(s.size()==1) for(ll i=1989;;i++) if(i%10==s[0]-'0') return i;
	ll num2=f(s.substr(1));
	ll T=E(s.size()-1);
	ll num=make_num(s);
	for(ll i=num2+T;;i+=T) if(i%(T*10)==num) return i;
}
int main()
{
	ll n;cin>>n;for(ll i=0;i<n;i++) {string s;cin>>s;cout<<f(s.substr(4))<<endl;}
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 1111
map<ll,int>m;
char s[maxn];
int main()
{
	int T;
	scanf("%d\n",&T);
	m.clear();
	while(T--)
	{
		scanf("%s",s);
		ll a=0,p=1;
		for(int i=4;s[i];i++)a=10ll*a+s[i]-'0';
		while(p<=a)p*=10;
		for(ll i=0;;i++)
		{
			ll b=i*p+a;
			if(b>=1989&&m.find(b)==m.end())
			{
				printf("%I64d\n",b);
				m[b]=1;
				break;
			}
		}
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 1111
map<ll,int>m;
char s[maxn];
int main()
{
	int T;
	scanf("%d\n",&T);
	m.clear();
	while(T--)
	{
		scanf("%s",s);
		ll a=0,p=1;
		for(int i=4;s[i];i++)a=10ll*a+s[i]-'0';
		while(p<=a)p*=10;
		if(a==0)p=10;
		for(ll i=1;;i++)
		{
			ll b=i*p+a;
			if(b>=1989&&m.find(b)==m.end())
			{
				printf("%I64d\n",b);
				m[b]=1;
				break;
			}
		}
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 1111
map<ll,int>m;
char s[maxn];
int main()
{
	int T;
	scanf("%d\n",&T);
	m.clear();
	while(T--)
	{
		scanf("%s",s);
		ll a=0,p=1;
		for(int i=4;s[i];i++)a=10ll*a+s[i]-'0';
		while(p<=a)p*=10;
		if(a==0)p=10;
		for(ll i=0;;i++)
		{
			ll b=i*p+a;
			if(b>=1989&&m.find(b)==m.end())
			{
				printf("%I64d\n",b);
				m[b]=1;
				break;
			}
		}
	}
	return 0;
}


#include <bits/stdc++.h>
#define adj_list vector<vi>
#define endl "\n"
#define INF_INT 2e9
#define INF_LL 2e18
#define matmax 25
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pair<int, int> >
#define pl pair<ll, ll>
#define pll pair<ll, pair<ll, ll> >
#define vi vector<int>
#define vl vector<ll>
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long int ll;

int lsone(int n)
{
	return (n&-n);
}

void mult(ll a[matmax][matmax], ll b[matmax][matmax], ll c[matmax][matmax], int m, int n, int p)
{
	for (int i = 1; i <= m; i++)
	{
		for (int j = 1; j <= p; j++)
		{
			c[i][j] = 0;
			for (int k = 1; k <= n; k++)
			{
				c[i][j] += (a[i][k]*b[k][j])%mod;
				c[i][j] %= mod;
			}
		}
	}
}

void mat_pow(ll a[matmax][matmax], ll c[matmax][matmax], int n, ll p)
{
	if (p == 0)
	{
		for (int i = 1; i <= n; i++)
			c[i][i] = 1;
	}
	else if (p == 1)
	{
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
				c[i][j] = a[i][j];
		}
	}
	else
	{
		ll d[matmax][matmax];
		mat_pow(a, d, n, p/2);
		if (p % 2)
		{
			ll e[matmax][matmax];
			mult(d, d, e, n, n, n);
			mult(e, a, c, n, n, n);
		}
		else
		{
			mult(d, d, c, n, n, n);
		}
	}
}

ll pow1(ll a, ll b)
{
	if (b == 0)
		return 1ll;
	else if (b == 1)
		return a;
	else
	{
		ll x = pow1(a, b/2);
		x *= x;
		x %= mod;
		if (b%2)
		{
			x *= a;
			x %= mod;
		}
		return x;
	}
}

int t;
map<string, int> m1;
string s;

void init()
{
	for (int i = 1989; i <= 9999; i++)
	{
		string s = "";
		int temp = i;
		s += char(temp%10 + '0');
		while (m1.count(s))
		{
			temp /= 10;
			s = char(temp%10 + '0') + s;
		}
		m1[s] = i;
	}
}

int main()
{
	fastio;
	init();
	cin>>t;
	while (t--)
	{
		cin>>s;
		string t = "";
		for (int i = 4; i < s.size(); i++)
			t += s[i];
		if (m1.count(t))
		{
			cout<<m1[t]<<endl;
		}
		else
		{
			int num = 0;
			for (int i = 0; i < t.size(); i++)
			{
				num *= 10;
				num += (t[i]-'0');
			}

			if (num < 10000)
				num += pow(10, t.size());

			string t1 = "";
			int flag = 0;
			for (int i = t.size()-1; i >= 0; i--)
			{
				t1 = t[i] + t1;
				if (m1.count(t1) == 0 and flag)
					num += pow(10, t.size());
				else if (m1.count(t1) == 0)
					flag = 1;
			}
			cout<<num<<endl;
		}
	}

	return 0;
}


#include <bits/stdc++.h>
#define adj_list vector<vi>
#define endl "\n"
#define INF_INT 2e9
#define INF_LL 2e18
#define matmax 25
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pair<int, int> >
#define pl pair<ll, ll>
#define pll pair<ll, pair<ll, ll> >
#define vi vector<int>
#define vl vector<ll>
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long int ll;

int lsone(int n)
{
	return (n&-n);
}

void mult(ll a[matmax][matmax], ll b[matmax][matmax], ll c[matmax][matmax], int m, int n, int p)
{
	for (int i = 1; i <= m; i++)
	{
		for (int j = 1; j <= p; j++)
		{
			c[i][j] = 0;
			for (int k = 1; k <= n; k++)
			{
				c[i][j] += (a[i][k]*b[k][j])%mod;
				c[i][j] %= mod;
			}
		}
	}
}

void mat_pow(ll a[matmax][matmax], ll c[matmax][matmax], int n, ll p)
{
	if (p == 0)
	{
		for (int i = 1; i <= n; i++)
			c[i][i] = 1;
	}
	else if (p == 1)
	{
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
				c[i][j] = a[i][j];
		}
	}
	else
	{
		ll d[matmax][matmax];
		mat_pow(a, d, n, p/2);
		if (p % 2)
		{
			ll e[matmax][matmax];
			mult(d, d, e, n, n, n);
			mult(e, a, c, n, n, n);
		}
		else
		{
			mult(d, d, c, n, n, n);
		}
	}
}

ll pow1(ll a, ll b)
{
	if (b == 0)
		return 1ll;
	else if (b == 1)
		return a;
	else
	{
		ll x = pow1(a, b/2);
		x *= x;
		x %= mod;
		if (b%2)
		{
			x *= a;
			x %= mod;
		}
		return x;
	}
}

int t;
map<string, int> m1;
string s;

void init()
{
	for (int i = 1989; i <= 99999; i++)
	{
		string s = "";
		int temp = i;
		s += char(temp%10 + '0');
		while (m1.count(s))
		{
			temp /= 10;
			s = char(temp%10 + '0') + s;
		}
		m1[s] = i;
	}
}

int main()
{
	fastio;
	init();
	cin>>t;
	while (t--)
	{
		cin>>s;
		string t = "";
		for (int i = 4; i < s.size(); i++)
			t += s[i];
		cout<<m1[t]<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
#define adj_list vector<vi>
#define endl "\n"
#define INF_INT 2e9
#define INF_LL 2e18
#define matmax 25
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define pi pair<int, int>
#define pii pair<int, pair<int, int> >
#define pl pair<ll, ll>
#define pll pair<ll, pair<ll, ll> >
#define vi vector<int>
#define vl vector<ll>
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long int ll;

int lsone(int n)
{
	return (n&-n);
}

void mult(ll a[matmax][matmax], ll b[matmax][matmax], ll c[matmax][matmax], int m, int n, int p)
{
	for (int i = 1; i <= m; i++)
	{
		for (int j = 1; j <= p; j++)
		{
			c[i][j] = 0;
			for (int k = 1; k <= n; k++)
			{
				c[i][j] += (a[i][k]*b[k][j])%mod;
				c[i][j] %= mod;
			}
		}
	}
}

void mat_pow(ll a[matmax][matmax], ll c[matmax][matmax], int n, ll p)
{
	if (p == 0)
	{
		for (int i = 1; i <= n; i++)
			c[i][i] = 1;
	}
	else if (p == 1)
	{
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
				c[i][j] = a[i][j];
		}
	}
	else
	{
		ll d[matmax][matmax];
		mat_pow(a, d, n, p/2);
		if (p % 2)
		{
			ll e[matmax][matmax];
			mult(d, d, e, n, n, n);
			mult(e, a, c, n, n, n);
		}
		else
		{
			mult(d, d, c, n, n, n);
		}
	}
}

ll pow1(ll a, ll b)
{
	if (b == 0)
		return 1ll;
	else if (b == 1)
		return a;
	else
	{
		ll x = pow1(a, b/2);
		x *= x;
		x %= mod;
		if (b%2)
		{
			x *= a;
			x %= mod;
		}
		return x;
	}
}

int n, p = 1, m, qm;
char c;
vector< char > v;
int main()
{
	fastio;
	while (cin>>c)
	{
		if (c == '=')
			break;
		else if (c == '+')
			p++;
		else if (c == '-')
			m++;
		else if (c == '?')
			qm++;
		if (c == '+' or c == '-')
			v.pb(c);
	}
	cin>>n;

	if (qm == 0)
	{
		cout<<"Impossible\n";
		return 0;
	}

	int orgn = n;
	n += m;
	int cnt = 1;
	int min = 1;
	while (p > n and cnt < orgn)
	{
		n += m;
		min++;
	}

	if (p*orgn >= n and p <= n)
	{
		cout<<"Possible\n";
		int avg = n/p;
		cout<<(avg + (n%p ? 1 : 0))<<" ";
		if (n%p)
			n--;
		for (int i = 0; i < v.size(); i++)
		{
			if (v[i] == '+')
			{
				cout<<v[i]<<" "<<(avg + (n%p ? 1 : 0))<<" ";
				if (n%p)
					n--;
			}
			else
			{
				cout<<v[i]<<" "<<min<<" ";
			}
		}

		cout<<"= "<<orgn<<endl;
	}
	else
	{
		cout<<"Impossible\n";
	}

	return 0;
}


#include <bits/stdc++.h>
#define adj_list vector<vi>
#define endl "\n"
#define INF_INT 2e9
#define INF_LL 2e18
#define matmax 25
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define pi pair<ll, ll>
#define pii pair<ll, pair<ll, ll> >
#define pl pair<ll, ll>
#define pll pair<ll, pair<ll, ll> >
#define vi vector<ll>
#define vl vector<ll>
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long int ll;

ll lsone(ll n)
{
	return (n&-n);
}

void mult(ll a[matmax][matmax], ll b[matmax][matmax], ll c[matmax][matmax], ll m, ll n, ll p)
{
	for (ll i = 1; i <= m; i++)
	{
		for (ll j = 1; j <= p; j++)
		{
			c[i][j] = 0;
			for (ll k = 1; k <= n; k++)
			{
				c[i][j] += (a[i][k]*b[k][j])%mod;
				c[i][j] %= mod;
			}
		}
	}
}

void mat_pow(ll a[matmax][matmax], ll c[matmax][matmax], ll n, ll p)
{
	if (p == 0)
	{
		for (ll i = 1; i <= n; i++)
			c[i][i] = 1;
	}
	else if (p == 1)
	{
		for (ll i = 1; i <= n; i++)
		{
			for (ll j = 1; j <= n; j++)
				c[i][j] = a[i][j];
		}
	}
	else
	{
		ll d[matmax][matmax];
		mat_pow(a, d, n, p/2);
		if (p % 2)
		{
			ll e[matmax][matmax];
			mult(d, d, e, n, n, n);
			mult(e, a, c, n, n, n);
		}
		else
		{
			mult(d, d, c, n, n, n);
		}
	}
}

ll pow1(ll a, ll b)
{
	if (b == 0)
		return 1ll;
	else if (b == 1)
		return a;
	else
	{
		ll x = pow1(a, b/2);
		x *= x;
		x %= mod;
		if (b%2)
		{
			x *= a;
			x %= mod;
		}
		return x;
	}
}

ll t;
map<string, ll> m1;
string s;

void init()
{
	for (ll i = 1989; i <= 9999; i++)
	{
		string s = "";
		ll temp = i;
		s += char(temp%10 + '0');
		while (m1.count(s))
		{
			temp /= 10;
			s = char(temp%10 + '0') + s;
		}
		m1[s] = i;
	}
}

ll solve(string inp)
{
	if (m1.count(inp))
		return m1[inp];

	ll ans = solve(inp.substr(1, inp.size()-1));
	ll num = 0;

	for (int i = 0; i < inp.size(); i++)
	{
		num *= 10;
		num += (inp[i]-'0');
	}

	while (num <= ans)
	{
		num += pow(10, inp.size());
	}

	return num;
}

int main()
{
	fastio;
	init();
	cin>>t;
	while (t--)
	{
		cin>>s;
		string t = "";
		for (ll i = 4; i < s.size(); i++)
			t += s[i];
		cout<<solve(t)<<endl;
	}

	return 0;
}