#include <iostream>

using namespace std;

void numberCalls(int &n)
{
    cin>>n;
    if(n>50) numberCalls(n);
}

void convert(char clock[], int &x,int &y, int &z, int &t)
{
    x=(int)clock[0];
    y=(int)clock[1];
    z=(int)clock[3];
    t=(int)clock[4];
}

void call(char clock[])
{
        cin>>clock;
        /*convert(clock,x,y,z,t);
        if(x>2 || (x==2 && y>4) || z>6)
            call(clock)*/
}



bool peculiar(int x, int y, int z, int t)
{
    bool code= false;
    if(z==0 && t==0)
        code=true;
    else if(x==z && y==t)
        code=true;
    else if(x==t && y==z)
        code=true;
    else if(x+1==y && y+1==z && z+1==t)
        code=true;
    else if(x>2)
        code=false;
    else if(x==2 && y>4)
        code=false;
    else if(z>6)
        code=false;
    return code;


}


int main()
{
    int x,y,z,t;
    char clock[1000];
    bool code[1000];
    int n;
    numberCalls(n);
    for (int i=1; i<=n;i++)
    {
        call(clock);
        convert(clock,x,y,z,t);
        code[i]= peculiar(x,y,z,t);
    }
    for( int i=1; i<=n;i++)
    {
    if(code[i]==true)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    }

}