import java.io.*;
import java.util.*;

public class pnumbers_va implements Runnable {
	public static void main(String[] args) {
		new Thread(new pnumbers_va()).run();
	}

	BufferedReader br;
	StringTokenizer in;
	PrintWriter out;

	public String nextToken() throws IOException {
		while (in == null || !in.hasMoreTokens()) {
			in = new StringTokenizer(br.readLine());
		}

		return in.nextToken();
	}

	public int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	public long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	public double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}

	ArrayList<Long> pn = new ArrayList<Long>();

	public boolean good(String s) {
		int i = s.length() - 1;
		while (s.charAt(i) == '0')
			i--;

		for (int j = 0; j <= i; j++) {
			if (s.charAt(i - j) != s.charAt(j))
				return false;
		}
		return true;
	}

	public boolean good(long x) {
		return good((x - 1) + "") && good((x + 1) + "");
	}

	public void precalc() {
		for (int i = 1; i < 10; i++) {
			pn.add((long) i);
		}

		long pow = 10;
		for (int i = 0; i < 18; i++) {
			pn.add(pow);
			pow *= 10;
		}

		for (int len = 2; len < 18; len++) {
			for (int i = 0; i < 1 << (len - 2); i++) {
				long x2 = 2;
				long x8 = 8;
				for (int j = 0; j < len - 2; j++) {
					x2 *= 10;
					x8 *= 10;
					if ((i & (1 << j)) == 0) {
						x2 += 2;
						x8 += 9;
					} else {
						x8 += 7;
					}
				}
				x2 = x2 * 10 + 1;
				x8 = x8 * 10 + 9;

				if (good(x2))
					pn.add(x2);
				if (good(x8))
					pn.add(x8);
			}
		}

		Collections.sort(pn);
	}

	public void solve() throws IOException {
		long l = nextLong();
		long r = nextLong();

		int ans = 0;
		for (long x : pn) {
			if (x >= l && x <= r)
				ans++;
		}
		out.println(ans);
	}

	public void run() {
		try {
			br = new BufferedReader(new FileReader("pnumbers.in"));
			out = new PrintWriter("pnumbers.out");

			precalc();

			int t = nextInt();
			for (int i = 0; i < t; i++)
				solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
}
