#include <iostream>
using namespace std;
#include<string>
#include<memory.h>
int r[120000];
int tt=0;
string start,end;
int st,ed;
int middlest(int x,int y)
{
if(x>y){
if(r[x]>=st)
return x;
else
return -1;
}
int m=(y+x)/2;
if(r[m]==st)
return m;
else if(r[m]>st)
return middlest(x,m-1);
else
return middlest(m+1,y);
}
int middleed(int x,int y)
{
if(x>y){
if(r[x]>ed)
return x-1;
else
return -1;
}
int m=(y+x)/2;
if(r[m]==ed)
return m;
else if(r[m]>ed)
return middleed(x,m-1);
else
return middleed(m+1,y);
}
int calNum()
{
int i=middlest(0,tt-1);
int j=middleed(0,tt-1);
return j-i+1;
}
bool check(char a)
{
if(a=='0' || a=='1' || a=='2' || a=='3' || a=='4' || a=='5' || a=='6' || a=='7' || a=='8' || a=='9')
return true;
return false;
}
void cal()
{
int i,j,k;
int c=0;
for(i=0;i<24;i++){
if(i!=0)
c+=10000;
for(j=0;j<60;j++){
if(j!=0)
c+=100;
for(k=0;k<60;k++){
c+=k;
if(c%3==0){
r[tt]=c;
tt++;
}
c-=k;
}
}
c-=59*100;
}
}
int main()
{
int i,j;
int num;
cin>>num;
memset(r,0,sizeof(r));
cal();
// for(int i=0;i<10;i++)
// cout<<r[i]<<' ';
while(num--){
cin>>start>>end;
string temp="";
i=0;
while(start[i]=='0' || start[i]==':')
i++;
for(;i<start.length();i++){
if(check(start[i]))
temp.append(1,start[i]);
}
st=0;
j=1;
for(i=temp.length()-1;i>=0;i--){
st+=j*(temp[i]-'0');
j=j*10;
}
temp="";
i=0;
while(end[i]=='0' || end[i]==':')
i++;
for(;i<end.length();i++){
if(check(end[i]))
temp.append(1,end[i]);
}
ed=0;
j=1;
for(i=temp.length()-1;i>=0;i--){
ed+=j*(temp[i]-'0');
j=j*10;
}
if(st<=ed)
cout<<calNum()<<endl;
else{
int t=ed;
ed=235959;
int f1=calNum();
st=0;
ed=t;
int f2=calNum();
cout<<f1+f2<<endl;
}
}
return 0;
}