def is_correct(hmax, mmax, time):
h, m = time.split(":")
mirrored = {"0": "0", "1":"1", "2": "5", "3": "",
"4": "", "5": "2", "6": "", "7": "", "8": "8", "9": ""}
mirrored_h = mirrored[h[1]]+mirrored[h[0]]
mirrored_m = mirrored[m[1]]+mirrored[m[0]]
if len(mirrored_h) == 2 and int(mirrored_h) < mmax \
and len(mirrored_m) == 2 and int(mirrored_m) < hmax:
return mirrored_m+":"+mirrored_h
return False
def main():
t = int(input())
for _ in range(t):
hmax, mmax = map(int, input().split())
hnow, mnow = map(int, input().split(":"))
result = "00:00"
end = False
for hour in range(hnow, hmax):
start = mnow if hour == hnow else 0
for minute in range(start, mmax):
time = str(hour).rjust(2, "0")+":"+str(minute).rjust(2, "0")
mirrored = is_correct(hmax, mmax, time)
if mirrored:
result = time
end = True
break
if end: break
print(result)
main()
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL);
const int mod = 1000000007;
const ll M = 998244353;
ll mul(ll a, ll b){
return ((a%M)*(b%M))%M;
}
ll modPower(ll a, ll b){
if(b==0){
return 1;
}
a = a%M;
ll res=1;
while(b>0){
if(b&1){
res = mul(res, a);
}
b = b>>1;
a = mul(a, a);
}
return res;
}
ll fac(ll n){
ll res=1;
for(ll i=2; i<=n; i++){
res = mul(res, i);
}
return res;
}
ll dv(ll a, ll b){
return mul(a, modPower(b, M-2));
}
ll nCr(ll n, ll r){
return dv(fac(n), mul(fac(r), fac(n-r)));
}
vi v = {0, 1, 5, -1, -1, 2, -1, -1, 8, -1};
int INF = 1e9+7;
int fun(int x) {
string s = to_string(x);
if(x<10){
s="0"+s;
}
string res="";
for(int i=1; i>=0; i--){
if (v[s[i]-'0']==-1){
return INF;
}
res += char(v[s[i]-'0'] + '0');
}
return stoi(res);
}
string good(int n){
string s=to_string(n);
if(n<10){
s = "0"+s;
}
return s;
}
void solve(){
int h, m;
cin>>h>>m;
string s;
cin>>s;
int a=(s[0]-'0')*10+(s[1]-'0');
int b=(s[3]-'0')*10+(s[4]-'0');
while(true){
a+=(b/m);
b%=m;
a%=h;
if(fun(a)<m && fun(b)<h){
cout<<good(a)<<":"<<good(b)<<'\n';
return;
}
b++;
}
}
int main(){
fast_io
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
43. Multiply Strings | 34. Find First and Last Position of Element in Sorted Array |
33. Search in Rotated Sorted Array | 17. Letter Combinations of a Phone Number |
5. Longest Palindromic Substring | 3. Longest Substring Without Repeating Characters |
1312. Minimum Insertion Steps to Make a String Palindrome | 1092. Shortest Common Supersequence |
1044. Longest Duplicate Substring | 1032. Stream of Characters |
987. Vertical Order Traversal of a Binary Tree | 952. Largest Component Size by Common Factor |
212. Word Search II | 174. Dungeon Game |
127. Word Ladder | 123. Best Time to Buy and Sell Stock III |
85. Maximal Rectangle | 84. Largest Rectangle in Histogram |
60. Permutation Sequence | 42. Trapping Rain Water |
32. Longest Valid Parentheses | Cutting a material |
Bubble Sort | Number of triangles |
AND path in a binary tree | Factorial equations |
Removal of vertices | Happy segments |
Cyclic shifts | Zoos |