#include <iostream> 
#include <iomanip> 
#include <cctype> 
#include <string> 
#include <cmath> 
#include <algorithm> 
#include <vector> 
#include <set> 
#include <map>  
#define _GLIBCXX_DEBUG 
#define rep(i, n) for (int i = 0; i < (int)(n); i++) 
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) 
#define ALL(v) v.begin(), v.end() 
using namespace std; 

int main() { 
	ios::sync_with_stdio(false); 

    int n, count = 0; cin >> n;
    
    rep2(i, 1, n + 1){
    	int n10 = i;
    	
    	while(n10 != 0){
    		if(n10 % 10 == 7) break;
    		
    		n10 /= 10;
    	}
    	
    	int n8 = i;
    	
    	while(n8 != 0){
    		if(n8 % 8 == 7) break;
    		
    		n8 /= 8;
    	}
    	
    	if(n10 == 0 && n8 == 0) count++;
    }
    
    cout << count << "\n";
    
	return 0; 
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"
template<class T> void chmax(T &a, const T b){ a = max(a, b); }
template<class T> void chmin(T &a, const T b){ a = min(a, b); }
long long gcd(long long a, long long b) { return (a % b) ? gcd(b, a % b) : b; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

void solve() {
    ll l, r;
    cin >> l >> r;
    ll val = r - 2 * l + 1;
    ll ans = ((val > 0) ? (1 + val) * val / 2 : 0);
    cout << ans << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll t;
    cin >> t;
    rep(i, t) solve();
    return 0;
}
