12
12 3
zazasqzqsasq
1 1
z
1 1
a
15 4
abccbaabccbazaa
16 8
cadjfdsljfdkljds
6 2
baazaa
6 3
bosszb
6 1
qwerty
7 7
daaaaaa
14 2
acabcbdeffewzd
18 6
zzzzzzzzzzzzyyyyyz
9 4
aabaabbbc

zazasqzqsasq
z
a
-1
cbbbbbbbbccccccc
baazbz
bpbbpp
qwerty
ddddddd
acabcbdeffexdx
zzzzzzzzzzzzzzzzzz
-1

import java.util.*;
import java.io.*;
public class vivek{   
static final Random random=new Random(); 
    static class FastReader{ 

        BufferedReader br; 
        StringTokenizer st; 

        public FastReader() 
        { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 

        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 

        char nextChar()
        {
            return next().charAt(0);
        }

        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 

        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 

        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 

        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 

        int[] nextArray(int rows)
        {
            int arr[] = new int[rows];            
            for (int i = 0; i < rows; i++)
            arr[i] = nextInt();
            return arr;
        }

        long[] nextLongArray(int rows)
        {
            long arr[] = new long[rows];            
            for (int i = 0; i < rows; i++)
            arr[i] = nextLong();
            return arr;
        }

        char[] nextCharArray(int rows)
        {
            char arr[] = new char[rows];
            for (int i = 0; i < rows; i++)
            arr[i] = next().charAt(0);
            return arr;
        }

        int[][] nextMatrix(int rows, int columns)
        {
            int mat[][] = new int[rows][columns];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    mat[i][j] = nextInt();
                }
            }
            return mat;
        }
    }
    static void sort(int a[]){
        int n=a.length;
        for(int i=0;i<n;i++){
            int oi=random.nextInt(n) , temp=a[oi];
            a[oi]=a[i];
            a[i]=temp;
        }
        Arrays.sort(a);
    }
    static int gcd(int a,int b){
        if(b==0)
            return 1;
        else 
            return gcd(b,a%b);
    }
    static boolean check(int aa){
        int a[]={0,1,2,5,8};
        int f=0;
        int aa1=aa;
        while(aa>0){
            int v=aa%10;
            aa=aa/10;
            for(int i=0;i<5;i++){
                if(a[i]==v)
                    f++;
            }
        }
        if(aa1==100){
            if(f==3)
                return true;
        }
        else if(aa1>=10){
            if(f==2)
                return true;
        }
        else{
            if(f==1)
                return true;
        }
        return false;
    }
    static int flip(int a){
        int aa=0;
        while(a>0){
            int f=a%10;
            if(f==2)
                f=5;
            if(f==5)
                f=2;
            aa=(aa*10)+f;
            a/=10;
        }
        if(aa<10)
            aa*=10;
        return aa;
    }
    public static void main(String args[]){
        FastReader sc=new FastReader();
        PrintWriter p=new PrintWriter(System.out);
        int t=sc.nextInt();
        while(t-->0){
            int h=sc.nextInt();
            int m=sc.nextInt();
            String s=sc.next();
            int f=0,hh=0,mm=0;
            for(int i=0;i<s.length();i++){
                char c=s.charAt(i);
                if(c==':'){
                    hh=f;
                    f=0;
                    c='0';
                }
                f=(f*10)+(c-48);
                mm=f;
                //p.println(mm);
            }
            //p.println(hh+" "+mm);
            int hh1=0,hh2=0,mm1=0,mm2=0;
            for(int i=hh;i<h && flip(i)<m;i++){
                if(check(i)){
                    hh1=i;
                    break;
                }
                if(!check(hh) && i==(h-1))
                    hh1=0;
            }
            if(hh1!=hh){
                mm1=0;
            }
            else{
                for(int i=mm;i<m && flip(i)<h;i++){
                    if(check(i)){
                        mm1=i;
                        break;
                    }
                    if(!check(mm) && i==(m-1))
                        mm1=0;
                }
            }
            if(mm1==0 && mm!=0){
                for(int i=hh+1;i<h && flip(i)<m;i++){
                    if(check(i)){
                        hh1=i;
                        break;
                    }
                    if(!check(hh) && i==(h-1))
                        hh1=0;
                }
            }
            /*for(int i=mm;i>=0;i--){
                if(check(i)){
                    mm2=i;
                    break;
                }
            }
            for(int i=hh;i>=0;i--){
                if(check(i)){
                    hh2=i;
                    break;
                }
            }*/
            int total=(hh*m)+mm;
            int total1=(hh1*m)+mm1;
            //int total2=(hh2*m)+mm2;
            //int diff1=Math.abs(total-total1);
            //int diff2=Math.abs(total-total2);
            //if(diff1<diff2){
                if(hh1<10)  
                    p.print("0"+hh1+":");
                else
                    p.print(hh1+":");
                if(mm1<10)  
                    p.println("0"+mm1);
                else
                    p.println(mm1);
            /*}
            else{
                if(hh2<10)  
                    p.print("0"+hh2+":");
                else
                    p.print(hh2+":");
                if(mm2<10)  
                    p.println("0"+mm2);
                else
                    p.println(mm2);
            }*/
        }
        p.flush();
    }
}