#include<stdio.h>
int main(){
int T,L,R,i,s,count,count_1=0,m;
scanf("%d",&T);
while(T--){
count_1=0;
scanf("%d%d",&L,&R);
for(i=L;i<=R;i++){
m=i;
count=0;
while(m){
s=m%10;
if(s!=7) {
m=m/10;
count=count+s;}
else break;}
if((count%7!=0)&&(i%7!=0))
count_1=count_1+i*i;
}
printf("%d\n",count_1);
}
}