#include<bits/stdc++.h>
using namespace std;
string s;
int n,m=0,a,b,a1[3],b1[3];
char c;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		int d=0,e=0;
		int flag=1;
		cin>>a>>b;
		cin>>s;
		for(int j=0;j<=1;j++){
			d*=10;
			d+=(s[j]-'0');
		}
		for(int j=3;j<=4;j++){
			e*=10;
			e+=(s[j]-'0');
		}
		for(int j=1;j<=a*b;j++){
			//cout<<d<<":"<<e<<endl;
			if((e%10==1||e%10==2||e%10==5||e%10==8||e%10==0)&&(e/10==1||e/10==2||e/10==5||e/10==8||e/10==0)&&(d%10==1||d%10==2||d%10==5||d%10==8||d%10==0)&&(d/10==1||d/10==2||d/10==5||d/10==8||d/10==0)){
				int f,g;
				a1[1]=e%10;
				a1[2]=e/10;
				b1[1]=d%10;
				b1[2]=d/10;
				if(a1[1]==2||a1[1]==5) a1[1]=7-a1[1];
				if(a1[2]==2||a1[2]==5) a1[2]=7-a1[2];
				if(b1[1]==2||b1[1]==5) b1[1]=7-b1[1];
				if(b1[2]==2||b1[2]==5) b1[2]=7-b1[2];
				f=a1[1]*10+a1[2];
				g=b1[1]*10+b1[2];
				
				if(f<a&&g<b){
					if(e>9&&d>9)cout<<d<<":"<<e<<endl;
					if(e<=9&&d>9) cout<<d<<":"<<0<<e<<endl;
					if(e>9&&d<=9) cout<<0<<d<<":"<<e<<endl;
					if(e<=9&&d<=9) cout<<0<<d<<":"<<0<<e<<endl;
					break;
				}
			}
			e++;
			if(e>=b) d+=(e/b),e%=b;
			if(d>=a) d%=a;
		}
	}
	return 0;
}