// BABITHA'S CODE.//

#include<bits/stdc++.h>

using namespace std;

#define int long long
#define int_max 9223372036854775807
#define float double
#define pii pair<int, int>
#define fl(i, a, b) for (int i = a; i < b; i++)
#define dy(a, n) int *a = new int[n]
#define mii map<int, int>
#define vec(a) vector<a>
#define MAXN 1004
#define endl "\n"
#define mod 1000000007
#define yes cout << "Yes\n"
#define no cout << "No\n"

static const int int_min = -9223372036854775807;

int spf[MAXN];
void sieve()
{
	spf[1] = 1;
	for (int i = 2; i < MAXN; i++)
		spf[i] = i;
	for (int i = 4; i < MAXN; i += 2)
		spf[i] = 2;

	for (int i = 3; i * i < MAXN; i++)
	{
		if (spf[i] == i)
		{
			for (int j = i * i; j < MAXN; j += i)
				if (spf[j] == j)
					spf[j] = i;
		}
	}
}

vector<int> getFactorization(int x)
{
	vector<int> ret;
	while (x != 1)
	{
		ret.push_back(spf[x]);
		x = x / spf[x];
	}
	return ret;
}

int gcd(int a, int b)
{
	if (b == 0)
	{
		return a;
	}
	return gcd(b, a % b);
}

int __gcd(int a, int b)
{
	if (b > a)
	{
		swap(a, b);
	}
	return gcd(a, b);
}

int lcm(int a, int b)
{
	int lcm1 = (a / __gcd(a, b)) * b;
	return lcm1;
}

int power(int x, int y)
{
	int res = 1; // Initialize result

	while (y > 0)
	{
		// If y is odd, multiply x with result
		if (y & 1)
		{
			res = res * x;
			//res %= mod;
		}
		// y must be even now
		y = y >> 1; // y = y/2
		x = x * x;	// Change x to x^2
					//x %= mod;
	}
	return res;
}

int powermod(int x, int y, int p)
{
	int res = 1; // Initialize result

	x = x % p; // Update x if it is more than or
	// equal to p

	while (y > 0)
	{
		// If y is odd, multiply x with result
		if (y & 1)
			res = (res * x) % p;

		// y must be even now
		y = y >> 1; // y = y/2
		x = (x * x) % p;
	}
	return res;
}

// Returns n^(-1) mod p
int modInverse(int n, int p)
{
	return powermod(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
int nCrModPFermat(int n, int r, int p)
{
	// Base case
	if (r == 0)
		return 1;

	// Fill factorial array so that we
	// can find all factorial of r, n
	// and n-r
	int fac[n + 1];
	fac[0] = 1;
	for (int i = 1; i <= n; i++)
		fac[i] = (fac[i - 1] * i) % p;

	return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}

int32_t main()
{

#ifndef ONLINE_JUDGE

	// For getting input from input.txt file
	freopen("input.txt", "r", stdin);

	// Printing the Output to output.txt file
	freopen("output.txt", "w", stdout);

#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t = 1;
	cin >> t;
	while (t--)
	{
		int h, m;
		cin >> h >> m;
		string s;
		cin >> s;
		int i = 0;
		int hh = 0;
		hh = (s[0] - '0');
		hh *= 10;
		hh += (s[1] - '0');
		int mm = 0;
		mm = (s[3] - '0');
		mm *= 10;
		mm += (s[4] - '0');
		//cout << hh << " " << mm << endl;
		bool status = false;
		while (!status)
		{
			int k1 = (mm + i) / m;
			int k2 = (mm + i) % m;
			int cuhh = hh + k1;
			cuhh %= h;
			int cumi = k2;
			int p[4];
			p[1] = cuhh % 10;
			p[0] = (cuhh / 10) % 10;
			p[3] = cumi % 10;
			p[2] = (cumi / 10) % 10;
			//cout << p[0] << p[1] << ":" << p[2] << p[3] << endl;
			bool check = true;
			fl(j, 0, 4)
			{
				if(p[j]!=1 && p[j]!=2 && p[j]!=5 &&p[j]!=0 &&p[j]!=8){
					check = false;
					break;
				}
			}
			if(!check){
				i++;
				continue;
			}
			int p1[4];
			fl(j,0,4){
				if(p[j]==2){
					p1[j] = 5;
				}
				else if(p[j]==5){
					p1[j] = 2;
				}
				else{
					p1[j] = p[j];
				}
			}
			int temph = p1[3] * 10 + p1[2];
			int tempm = p1[1] * 10 + p1[0];
			if(temph<h && tempm<m){
				status = true;
				cout << p[0] << p[1] << ":" << p[2] << p[3] << endl;
			}
			//cout << p[0] << p[1] << ":" << p[2] << p[3] << endl;
			//cout << i << endl;
			i++;
		}
	}
	return 0;
}