#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int first_time(int x, int y)
{
    int i,c=0;;
    for(i = x;i<=y;i++)
    {
        int t=i,check=1;
        while(t)
        {
            if(t%10>=5)
            {
                check=0;
                break;
            }
            t/=10;

        }
        if(check)c++;


}
    return c;

}


int main() {

    int n;

    scanf("%d",&n);
    while(n--)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        int i,value=0;

            value=first_time(x,y);
            printf("%d\n",value);
    }
}
