#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <vector>
#include <unordered_map>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;


int n;
typedef vector<vector<pii> > Graph;

void NFAtoDFA(Graph &g, map<pii, unsigned char> &G){
	int n = g.size();
	unordered_map<int, unsigned char> vis;
	vis[1] = 1;
	queue<int> q;
	q.push(1);
	while(!q.empty()){
		int i = q.front();
		q.pop();
		REP(j, 10){
			int tar = 0;
			REP(k, n){
				if(((i>>k)&1) == 0) continue;
				FOR(e, g[k]){
					if(e->second & (1<<j)) tar |= 1<<e->first;
				}
			}
			if(tar){
				G[pii(i, tar)] ++;
				if(!vis[tar]){
					vis[tar] = 1;
					q.push(tar);
				}
			}
		}
	}
}

ll dfs(int s, unordered_map<int, ll> &memo, map<pii, unsigned char> &G, int accept){
	if(memo[s]) return memo[s];
	auto it = G.lower_bound(pii(s, 0));
	auto it2 = G.lower_bound(pii(s+1, 0));
	ll res = !!(s&accept);
	for(;it!=it2;++it){
		res += it->second * dfs(it->first.second, memo, G, accept);
	}
	return memo[s] = res;
}

int main(){
	while(cin >> n, n){
		int low[6], hi[6];
		REP(i, n) cin >> low[i] >> hi[i];
		Graph g(n*4+1);
		REP(i, n){
			if(low[i]/10 == hi[i]/10){
				int ev = 0;
				REP(j, 10) if(low[i]%10 <= j && j <= hi[i]%10) ev |= 1<<j;
				if(low[i] < 10){
					g[4*i].push_back(pii(4*i+4, ev));
				}else{
					g[4*i].push_back(pii(4*i+1, 1<<(low[i]/10)));
					g[4*i+1].push_back(pii(4*i+4, ev));
				}
			}else{
				int ev = 0;
				REP(j, 10) if(low[i]%10 <= j) ev |= 1<<j;
				if(low[i] < 10){
					g[4*i].push_back(pii(4*i+4, ev));
				}else{
					g[4*i].push_back(pii(4*i+1, 1<<(low[i]/10)));
					g[4*i+1].push_back(pii(4*i+4, ev));
				}	

				ev = 0;
				REP(j, 10) if(low[i]/10 < j && j < hi[i]/10) ev |= 1<<j;
				if(ev){
					g[4*i].push_back(pii(4*i+2, ev));
					g[4*i+2].push_back(pii(4*i+4, (1<<10)-1));
				}
				
				ev = 0;
				REP(j, 10) if(hi[i]%10 >= j) ev |= 1<<j;
				g[4*i].push_back(pii(4*i+3, 1<<(hi[i]/10)));
				g[4*i+3].push_back(pii(4*i+4, ev));
			}
		}
		map<pii, unsigned char> g2;
		NFAtoDFA(g, g2);
		unordered_map<int, ll> memo;
		cout << dfs(1, memo, g2, 1<<(4*n)) << endl;
	}
	return 0;
}