#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 
int main()
{  
    IOS
    ll t;
    cin>>t;
    while(t--)
    {
      ll h,m;
      cin>>h>>m;
      string s;
      cin>>s;
      ll h1=(s[0]-'0')*10+(s[1]-'0'),m1=(s[3]-'0')*10+(s[4]-'0');
      vector<ll>v;
      v.push_back(0);
      v.push_back(1);
      v.push_back(8);
      v.push_back(2);
      v.push_back(5);
      map<ll,ll>mx;
      mx[1]=1;
      mx[0]=0;
      mx[2]=5;
      mx[8]=8;
      mx[5]=2;
      sort(v.begin(),v.end());ll f=0;
      for(ll i=0;i<h;i++)
      {
        for(ll j=0;j<m;j++)
        {
          
          ll x=(i+h1)%h;ll y=(j+m1);if(y>=m){y%=m;x=(x+1)%h;}
          ll x1=x/10;ll x2=x%10;ll y1=y/10;ll y2=y%10;
          if(binary_search(v.begin(),v.end(),x1)&&binary_search(v.begin(),v.end(),x2)&&binary_search(v.begin(),v.end(),y1)&&binary_search(v.begin(),v.end(),y2))
          {
              x1=mx[x1];x2=mx[x2];y1=mx[y1];y2=mx[y2];
              ll x3=y2*10+y1;ll y3=x2*10+x1;
              if(x3<h&&y3<m)
              { string xx=to_string(x);string yy=to_string(y);
                if(xx.size()==1){xx='0'+xx;}if(yy.size()==1){yy='0'+yy;}
                cout<<xx<<":"<<yy<<"\n";f=1;break;
              }
          }
        }
        if(f){break;}
      }


    }
         return 0;
 }