//                                      LAZY_LAD_69

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

            //DEFINE

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long int
#define nl "\n"
#define forf(i,a,n) for(ll i=a;i<=n;i++)
#define forb(i,b,n) for(ll i=b;i>=n;i--)
#define pb push_back
#define vll vector<ll>
#define all(qw) qw.begin(),qw.end()
#define read(vec) ll qwe;cin>>qwe;vec.pb(qwe)
#define pll pair<ll,ll>
#define vpll vector<pll>
#define ff first
#define ss second
#define mp make_pair
#define sll set<ll>
#define inset(sett) ll qwer;cin>>qwer;sett.insert(qwer);

            //FUNCTIONS

ll hcf(ll a, ll b)
{
	if (b==0)return a;
	return hcf(b, a % b);
}

ll lcm(ll a,ll b)
{
    return (a*b)/hcf(a,b);
}

bool isprime(ll n) {
	if(n==1)
	{
	    return false;
    }
    for (ll i=2;i*i<=n;++i)
	{
        if(n%i==0)
		{
            return false;
        }
    }
    return true;
}

void onlinejudge(){
	 #ifndef ONLINE_JUDGE

     freopen("Input.txt", "r", stdin);

     freopen("Output.txt", "w", stdout);

     #endif // ONLINE_JUDGE
}

			//SOLVE


void solve(){
    ll h,m;
    cin>>h>>m;
    string t;
    cin>>t;
    ll hr,min;ll irmm=0,irhh=0;
    string hh="ll",mm="ll";
    string rmm="ll",rhh="ll";
    hh[0]=t[0];hh[1]=t[1];
    mm[0]=t[3];mm[1]=t[4];
    hr=stoi(hh);
    min=stoi(mm);
    ll inhr=hr;
   // cout<<hh<<nl;
    while(1){
			rhh[0]=mm[1];
			rhh[1]=mm[0];
			if(rhh[0]=='5') rhh[0]='2';
			else if(rhh[0]=='2') rhh[0]='5';
			if(rhh[1]=='5') rhh[1]='2';
			else if(rhh[1]=='2') rhh[1]='5';
			irhh=stoi(rhh);
			
			//cout<<"IIRhh "<<rhh<<nl;
			//cout<<min<<" "<<irhh<<nl;
    	if(mm[0]!='3' and mm[0]!='4' and mm[0]!='6' and mm[0]!='7' and mm[0]!='9' and
	       mm[1]!='3' and mm[1]!='4' and mm[1]!='6' and mm[1]!='7' and mm[1]!='9' and
	       min<m and irhh<h) break;
		else {
			min++;
			mm=to_string(min);
			if(mm.length()==1) mm="0"+mm;
			if(min>=m){
				mm="00";
				min=0;
				hr++;
				hh=to_string(hr);
			}
		} if(hh.length()==1) hh="0"+hh;if(mm.length()==1) mm="0"+mm;
	//	cout<<"YES111"<<nl;
	}
//	cout<<"YES"<<nl;
//cout<<"hh"<<hh<<"!"<<nl;
	while(1){
		rmm[0]=hh[1];
		rmm[1]=hh[0];
	//	cout<<"yss1 "<<rmm<<nl;
		    if(rmm[0]=='5') rmm[0]='2';
			else if(rmm[0]=='2') rmm[0]='5';
			if(rmm[1]=='5') rmm[1]='2';
			else if(rmm[1]=='2') rmm[1]='5';
			
	//	cout<<"yss "<<rmm<<nl;
		//cout<<"hh"<<hh[1]<<"!"<<nl;
	//	cout<<"rmm"<<rmm<<nl;
		irmm=stoi(rmm);
    	if(hh[0]!='3' and hh[0]!='4' and hh[0]!='6' and hh[0]!='7' and hh[0]!='9' and
	       hh[1]!='3' and hh[1]!='4' and hh[1]!='6' and hh[1]!='7' and hh[1]!='9' and
	        hr<h and irmm<m
		) break;
		else {
			hr++;
		    hh=to_string(hr);
		    if(hh.length()==1) hh="0"+hh;
			if(hr>=h){
				hh="00";
				hr=0;
			}
		}if(hh.length()==1) hh="0"+hh;if(mm.length()==1) mm="0"+mm;
	//	cout<<"YS "<<hh<<nl;
	}
//	cout<<"YES"<<nl;
if(hr!=inhr) mm="00";
	cout<<hh<<":"<<mm<<nl;
}

			//MAIN

int main(){
  //onlinejudge();
 fastio
  ll ttt;ttt=1;
  cin>>ttt;
  while(ttt--) solve();
return 0;
}

// CHECK FOR EXTREME CASES AND ZEROS;











