#include <iostream>

using namespace std;

int n , i , x , y , z , t , ora , Min;

char xx , yy , zz , tt , r;

bool ok;

bool putere2(int nr)
{
    return (nr & (nr - 1) == 0);
}

int main()
{
    //freopen("pelicular.in","r",stdin);
    //freopen("pelicular.out","w",stdout);

    cin >> n;

    for (i = 1; i <= n; ++i)
    {
        cin >> xx >> yy >> r >> zz >> tt;

        x = xx - '0'; y = yy - '0'; z = zz - '0'; t = tt - '0';

        ora = x * 10 + y; Min = z * 10 + t;

        ok = false;

        if (ora >= 0 && ora < 24 && Min >= 0 && Min < 60)
        {
            if (Min == 0) ok =true;
            if (ora == Min) ok = true;
            if (x == t && y == x) ok = true;
            if (y == x + 1 && z == y + 1 && t == z + 1) ok = true;
            if (x != 0 && putere2(ora * 100 + Min)) ok = true;
        }

        if (ok) cout << "YES\n";
        else cout<<"NO\n";
    }

    return 0;
}