这题就是模拟时间的加法就行。
但忘记了5和2轴对称是互相转换的情况,卡了很久。而这个条件之前就写道了草稿纸上,结果忘记了。

#include <iostream>
#include<stdio.h>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <queue>
//#include <bits/stdc++.h>
#define pb push_back
#define qc std::ios::sync_with_stdio(0);
#define maxn 0x3f3f3f
#define debug(x) cout<<"debug"<<x<<endl;
using namespace std;
const int max_n=35;
const int min_n=10000000;
typedef long long ll;
typedef unsigned long long int ul;
typedef double dl;
int flag=1,flag1=1; 
int check(int n)
{
    if(n!=0&&n!=1&&n!=2&&n!=5&&n!=8) flag=0;
    if(n==2) return 5;
    else {
        if(n==5) return 2; 
    }
    return n;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int h,m;
        int a,b;
        char c;
        cin>>h>>m;
        cin>>a>>c>>b;
        int mm=0,hh=0;
        flag=1;
        int x1,x2,x3,x4;
        x1=check(b%10);x2=check(b/10);x3=check(a%10);x4=check(a/10);
        mm=x1*10+x2;
        hh=x3*10+x4;
        if(mm>=h||hh>=m) flag=0;//转换后是否合法
        if(flag==1) {
            printf("%02d:%02d\n",a,b);
        }
        else 
        {
            while(true)
            {
                flag=1;
                int x1,x2,x3,x4;
                x1=check(b%10);x2=check(b/10);x3=check(a%10);x4=check(a/10);
                mm=x1*10+x2;
                hh=x3*10+x4;
                if(mm>=h||hh>=m) flag=0;//转换后是否合法 
                //debug(mm) debug(hh)
                if(flag==1) {
                printf("%02d:%02d\n",a,b);break;
                }
                if(b<m-1) b++;
                else if(b==m-1)
                {
                b=0;
                if(a<h-1) a++;
                else if(a==h-1)
                {
                    printf("00:00\n");
                    break;
                }
                }
            }
        }
    }
}