#include <bits/stdc++.h>
#include <unordered_set>

using namespace std;
///// Change default type here :D /////
typedef long long num;
//////////////////////////////
void PrintBool(bool a);

num a, b, c, d, e, f, g, h, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
// Does not contain i and j
string s1, s2, t1;
vector<num> nums;

num MirrorDigit(int digit)
{
	switch (digit)
	{
		case 0:
			return 0;
		case 1:
			return 1;
		case 2:
			return 5;
		case 5:
			return 2;
		case 8:
			return 8;
		default:
			return -1;
	}
}

num MirrorNumber(int number)
{
	int first_digit = number % 10;
	int second_digit = number / 10;
	if (MirrorDigit(first_digit) == -1 || MirrorDigit(second_digit) == -1) return -1;

	return MirrorDigit(first_digit) * 10 + MirrorDigit(second_digit);
}

void Solve()
{
	cin >> h >> m;
	cin >> a;
	char c;
	cin >> c;
	cin >> b;

	if (!(MirrorNumber(a) != -1 && MirrorNumber(a) < m)){
		b = 0;
	}
	while (true){
		if (MirrorNumber(b) != -1 && MirrorNumber(b) < h){
			break;
		}
		b++;
		if (b == m) {
			b = 0;
			a ++;
			if (a == h) a = 0;
		}
	}
	while (true){
		if (MirrorNumber(a) != -1 && MirrorNumber(a) < m){
			string str_b = to_string(b);
			if (str_b.size() < 2) str_b = "0" + str_b;
			string str_a = to_string(a);
			if (str_a.size() < 2) str_a = "0" + str_a;

			cout << str_a << ":" << str_b << endl;
			return;
		}
		a++;
		if (a == h) a = 0;
	}
}

int main()
{
	int query;

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	// Comment out either of the two lines below
	// depending on the problem type.
	cin >> query; // Use when there are multiple test cases per test.
	//query = 1; // Use when there are 1 test case per test.

	while (query--)
	{
		Solve();
	}
	return 0;
}

void PrintBool(bool a)
{
	string s = (a) ? "YES" : "NO";
	cout << s << endl;
}