#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
using namespace std;

#define all(c) (c).begin(),(c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(),(c).rend()
#define ifr(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define pof pop_front
#define pob pop_back
#define pb emplace_back
#define pf emplace_front
#define fstm(m,n,r) m.reserve(n);m.max_load_factor(r)
#define mp make_pair
#define mt make_tuple
#define inf LLONG_MAX
#define os tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).
const double PI = acos(-1);
typedef complex<double> cd;
typedef long long ll;
ll gcd(){return 0ll;} template<typename T,typename... Args> T gcd(T a,Args... args) { return __gcd(a,(__typeof(a))gcd(args...)); }
typedef map<ll,ll> mll;
typedef map<string,ll> msll;
typedef unordered_map<ll,ll> umap;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef long double ld;
#define mod 1000000007 

const int N = 1e5 + 2;

int h, m;

int refDigit(char ch) {
    int x = ch-'0';
    switch(x) {
        case 0:
            return 0;
        case 1:
            return 1;
        case 2:
            return 5;
        case 5:
            return 2;
        case 8:
            return 8;
    }
    return -1;
}

bool valid(string &tim) {
    int hh = (tim[0]-'0')*10 + (tim[1]-'0');
    int mm = (tim[3]-'0')*10 + (tim[4]-'0');
    // cout<<tim<<" "<<hh<<" "<<mm<<endl;
    return hh<h && mm<m;
}

bool check(string &tim) {
    string rev = tim;
    int x, j = 5;
    // cout<<tim<<endl;
    for(auto &it: tim) {
        j--;
        if(it == ':') continue;
        x = refDigit(it);
        // cout<<x<<" ";
        if(x == -1) 
            return 0;
        rev[j] = x + '0';
    }
    // cout<<rev<<endl;
    return valid(rev);
}

void inc(string &tim) {
    int hh = (tim[0]-'0')*10 + (tim[1]-'0');
    int mm = (tim[3]-'0')*10 + (tim[4]-'0');
    // cout<<tim<<endl;
    // cout<<hh<<" "<<mm<<endl;
    mm++;
    if(mm == m) {
        mm=0;
        hh++;
        if(hh == h)
            hh = 0;
    }
    // cout<<hh<<" "<<mm<<endl;
    tim[0] = (hh/10) + '0';
    tim[1] = (hh%10) + '0';
    tim[3] = (mm/10) + '0';
    tim[4] = (mm%10) + '0';
    
    // tim = to_string(hh) + ":" + to_string(mm);
}

int Solve() {
    string tim, in, de;
    cin>>h>>m>>tim;
    in = de = tim;

    if(check(tim)) {
        cout<<tim<<endl;
        return 0;
    }

    while(1) {
        inc(in);
        if(check(in)) {
            cout<<in<<endl;
            return 0;
        }
    }
    return 0;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // #ifndef ONLINE_JUDGE
    //     freopen("input.txt","r",stdin);
    // #endif
    
    int T;cin>>T;while(T--) 
    {
        Solve();
    }
    return 0;
}