#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0)
#define scl(n) scanf("%lld", &n)
#define pcl(n) printf("%lld\n", n)
#define pcl1(n) printf("%lld ", n)
#define nln printf("\n")
#define yes printf("YES\n")
#define no printf("NO\n")
#define dev(x) cout << #x << " " << x << " ";
#define PTT pair<ll, ll>

map<ll, ll> mp, mp1;
map<ll, ll>::iterator itr;

int main()
{
    ll t, n, k, x, y, a, b, d;
    while (cin >> t)
    {
        vector<ll> vm;
        vm.push_back(0);
        vm.push_back(1);
        vm.push_back(2);
        vm.push_back(5);
        vm.push_back(8);
        for (int i = 0; i < vm.size(); i++)
        {
            for (int j = 0; j < vm.size(); j++)
            {
                x = vm[i] * 10 + vm[j];
                mp[x] += 1;
            }
        }
        vector<PTT> v;
        PTT p;
        for (auto k : mp)
        {
            if (k.first < 10)
            {
                p.first = k.first;
                p.second = k.first * 10;
                if (p.first == 2)
                {
                    p.second = 5 * 10;
                }
                else if (p.first == 5)
                {
                    p.second = 2 * 10;
                }

                v.push_back(p);
                //mp[k.first] = k.first * 10;
            }
            else
            {
                d = k.first % 10;
                if (d == 2)
                {
                    d = 5;
                }
                else if (d == 5)
                {
                    d = 2;
                }

                x = k.first / 10;
                if (x == 2)
                {
                    x = 5;
                }
                else if (x == 5)
                {
                    x = 2;
                }
                y = d * 10 + x;
                //mp[k.first] = y;
                p.first = k.first;
                p.second = y;
                v.push_back(p);
            }
            //cout << p.first << " " << p.second << endl;
        }
        //nln;

        for (int cs = 0; cs < t; cs++)
        {
            ll h, m;
            string s;
            cin >> h >> m;
            cin >> s;
            ll hr = 0, mn = 0;
            hr += ((ll)(s[0] - '0') * 10 + (ll)(s[1] - '0'));
            mn += ((ll)(s[3] - '0') * 10 + (ll)(s[4] - '0'));
            //cout << hr << " " << mn << "-->";
            a = 0, b = 0;
            ll c = 0, d = 0;
            x = hr, y = mn;
            for (int i = 0; i < v.size(); i++)
            {
                if (v[i].first < x)
                {
                    continue;
                }

                while (x < v[i].first)
                {
                    x++;
                }

                if (x == v[i].first && v[i].first <= h - 1 && v[i].second <= m - 1)
                {
                    a = v[i].first;
                    c = v[i].second;
                    break;
                }
            }

            for (int i = 0; i < v.size(); i++)
            {
                if (v[i].first < y)
                {
                    continue;
                }
                while (y < v[i].first)
                {
                    y++;
                }
                if (y <= v[i].first && v[i].first <= m - 1 && v[i].second <= h - 1)
                {
                    b = v[i].first;
                    d = v[i].second;
                    break;
                }
            }

            if (a < x && a == 0)
            {
                b = 0;
            }
            else if (b == 0 && (a == x && b < y))
            {
                a = 0;
                x = hr + 1;
                for (int i = 0; i < v.size(); i++)
                {
                    if (v[i].first < x)
                    {
                        continue;
                    }

                    while (x < v[i].first)
                    {
                        x++;
                    }
                    if (x <= v[i].first && v[i].first <= h - 1 && v[i].second <= m - 1)
                    {
                        a = v[i].first;
                        c = v[i].second;
                        break;
                    }
                }
            }
            if (a > hr)
            {
                b = 0;
            }

            //cout << a << " " << c << " " << b << " " << d << endl;
            if (a == 0)
            {
                cout << "00:";
            }
            else
            {
                if (a / 10 == 0)
                {
                    cout << "0" << a << ":";
                }
                else
                {
                    cout << a << ":";
                }
            }
            if (b == 0)
            {
                cout << "00";
            }
            else
            {
                if (b / 10 == 0)
                {
                    cout << "0" << b;
                }
                else
                {
                    cout << b;
                }
            }
            nln;
        }
    }

    return 0;
}
