#include <iostream>
#include <math.h>
#include <string>
#include <map>
#include <string.h>
#include <vector>
#include <time.h>
#include <algorithm>
using namespace std;
int toint(char c){
return c-'0';
}
bool validtime(string s){
if (toint(s[0]) !=3 && toint(s[1]) !=3 && toint(s[3]) !=3 && toint(s[4]) !=3 &&
toint(s[0]) !=4 && toint(s[1]) !=4 && toint(s[3]) !=4 && toint(s[4]) !=4 &&
toint(s[0]) !=7 && toint(s[1]) !=7 && toint(s[3]) !=7 && toint(s[4]) !=7){
if (toint(s[4]) > 2) return false;
if (toint(s[4]) == 2 && toint(s[3]) > 3) return false;
return true;
}
else return false;
}
int formattoint(string s){
return (toint(s[0])*10+toint(s[1]))*60+(toint(s[3])*10+toint(s[4]));
}
string toformat(int time){
string s;
s+=to_string(time/600);
s+=to_string((time/60)%10);
s+=":";
s+=to_string((time%60)/10);
s+=to_string((time%60)%10);
return s;
}
int main() {
string s1,s2;
char ch;
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int count=0;
//getline(cin, s);
cin>>s1>>ch>>s2;
if (formattoint(s1)<=formattoint(s2)){
for (int time = formattoint(s1);time<=formattoint(s2);time++) {
if (validtime(toformat(time))){cout<<toformat(time)<<"\n"; count++;}
}
}
else{
for (int time = formattoint(s1);time<1440;time++) {
if (validtime(toformat(time))) count++;
}
for (int time = 0;time<=formattoint(s2);time++) {
if (validtime(toformat(time))) count++;
}
}
cout<<count<<endl;
}
return 0;
}