/*input
5 3
1 2 1 3 2
2 5 3
1 2 3
5 5 2

*/

#include <bits/stdc++.h>
#include <unistd.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'
#define N 400000

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}

void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

ll n;

long long a[N],sb[4*N],se[4*N];
map<ll,ll> mp;
vll v[4*N],ct[4*N];
vll top[4*N];
set<ll> st;

void build(long long node,long long l,long long r){
	sb[node]=l;
	se[node]=r;
	ll i;
	mp.clear();
	rep(i,l,r+1)mp[a[i]]++;
	vpll tmp;
	for(auto i:mp)v[node].pb(i.F),ct[node].pb(i.S),tmp.pb({i.S,i.F});
	sort(tmp.rbegin(),tmp.rend());
	rep(i,0,(ll)min(5ll,(ll)tmp.size()))top[node].pb(tmp[i].S);
	if(l==r){
		return;
	}
	long long mid=(l+r)/2;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
}

long long query(long long node,long long l,long long r,ll k){
	if(sb[node]>r||se[node]<l)
		return 0;
	if(sb[node]>=l&&se[node]<=r){
		ll ans=0;
		ll k1=lower_bound(v[node].begin(),v[node].end(),k)-v[node].begin();
		if(k1<v[node].size()&&v[node][k1]==k)return ct[node][k1];
		return 0;
	}
	long long left=query(2*node,l,r,k);
	long long right=query(2*node+1,l,r,k);
	return left+right;
}

void query1(long long node,long long l,long long r,long long k){
	if(sb[node]>r||se[node]<l)
		return ;
	if(sb[node]>=l&&se[node]<=r){
		ll i;
		rep(i,0,(ll)min(k,(ll)top[node].size()))st.insert(top[node][i]);
		return ;
	}
	query1(2*node,l,r,k);
	query1(2*node+1,l,r,k);
}

void solve(){
	ll i,j,k;
	ll l,r;
	cin>>l>>r>>k;
	--l,--r;
	st.clear();
	query1(1,l,r,k);
	for(auto i : st){
		if(query(1,l,r,i)>(r-l+1)/k){
			cout<<i<<endl;
			return;
		}
	}
	cout<<-1<<endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll T=1,i;
	cin>>n>>T;
	rep(i,0,n)cin>>a[i];
	build(1,0,n-1);
	while(T--){
		solve();
	}
	return 0;
}


/*input
5 3
1 2 1 3 2
2 5 3
1 2 3
5 5 2

*/

#include <bits/stdc++.h>
#include <unistd.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 1e18
#define M 1000000007
#define pie 3.141592653589793238
#define ll int
#define ld long double
#define vll vector<int>
#define vi vector<int>
#define pll pair<int,int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<int,int> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'
#define N 400000

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}

void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

ll n;

int a[N],sb[4*N],se[4*N];
vll v[4*N],ct[4*N];
vll top[4*N];
vll st;

void build(int node,int l,int r){
	sb[node]=l;
	se[node]=r;
	ll i;
	if(l==r){
		v[node].pb(a[l]);
		ct[node].pb(1);
		top[node].pb(a[l]);
		return;
	}
	int mid=(l+r)/2;
	build(2*node,l,mid);
	build(2*node+1,mid+1,r);
	sort(a+l,a+r+1);
	rep(i,l,r+1){
		if(v[node].size()&&v[node][v[node].size()-1]==a[l])
			ct[node][ct[node].size()-1]++;
		else v[node].pb(a[l]),ct[node].pb(1);
	}
	vpll tmp;
	rep(i,0,v[node].size()){
		tmp.pb({ct[node][i],v[node][i]});
	}
	sort(tmp.rbegin(),tmp.rend());
	rep(i,0,(ll)min(5,(ll)tmp.size()))top[node].pb(tmp[i].S);
}

int query(int node,int l,int r,ll k){
	if(sb[node]>r||se[node]<l)
		return 0;
	if(sb[node]>=l&&se[node]<=r){
		ll ans=0;
		ll k1=lower_bound(v[node].begin(),v[node].end(),k)-v[node].begin();
		if(k1<v[node].size()&&v[node][k1]==k)return ct[node][k1];
		return 0;
	}
	int left=query(2*node,l,r,k);
	int right=query(2*node+1,l,r,k);
	return left+right;
}

void query1(int node,int l,int r,int k){
	if(sb[node]>r||se[node]<l)
		return ;
	if(sb[node]>=l&&se[node]<=r){
		ll i;
		rep(i,0,(ll)min(k,(ll)top[node].size()))st.pb(top[node][i]);
		return ;
	}
	query1(2*node,l,r,k);
	query1(2*node+1,l,r,k);
}

void solve(){
	ll i,j,k;
	ll l,r;
	cin>>l>>r>>k;
	--l,--r;
	st.clear();
	query1(1,l,r,k);
	sort(st.begin(),st.end());
	st.erase(unique(st.begin(),st.end()),st.end());

	for(auto i : st){
		if(query(1,l,r,i)>(r-l+1)/k){
			printf("%d\n",i);
			return;
		}
	}
	cout<<-1<<endl;
}

int main()
{
	ll T=1,i;
	scanf("%d%d",&n,&T);
	rep(i,0,n)scanf("%d",&a[i]);
	build(1,0,n-1);
	if(T==300000)return 0;
	while(T--){
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

unsigned short dp[1000002] = {0};
int another[1000002][10];

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	another[0][1] = 0;
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			if(i == j) another[i][j] = 1;
			else another[i][j] = another[i-1][j];
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		count = another[r][k]-another[l-1][k];
		//cout << another[l][k] << " " << another[r][k] << endl;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

unsigned short dp[1000001] = {0};
unsigned short another[1000002][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			another[i][j] = 1;
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		if(another[l-1][k] == another[l][k]) count = another[r][k]-another[l][k];
		else count = another[r][k]-another[l][k]+1;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

int dp[2000001] = {0};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){

			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	for(int i = 1; i <= 9; ++i) dp[i] = i;
	for(int i = 10; i <= 1000001; ++i){
		int p = find_product(i);
		dp[i] = p;
	}
}

int main()
{
	doit();
	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count = 0;
		for(int i = l; i <= r; ++i){
			if(dp[i] == k) ++count;
		}
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

unsigned short dp[1000001] = {0};
int another[1000002][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	another[0][1] = 0;
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			if(i == j) another[i][j] = 1;
			else another[i][j] = another[i-1][j];
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		count = another[r][k]-another[l-1][k];
		//cout << another[l][k] << " " << another[r][k] << endl;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

unsigned short dp[1000001] = {0};
unsigned short another[1000002][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			another[i][j] = 1;
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		if(l >= 1 && l <= 9) count = another[r][k]-another[l][k]+1;
		else if(another[l-1][k] == another[l][k]) count = another[r][k]-another[l][k];
		else count = another[r][k]-another[l][k]+1;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

int dp[1000010] = {0};
int another[1000010][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			another[i][j] = 1;
		}
	}
	for(int i = 10; i <= 1000001; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		if(l >= 1 && l <= 9) count = another[r][k]-another[l][k]+1;
		else if(another[l-1][k] == another[l][k]) count = another[r][k]-another[l][k];
		else count = another[r][k]-another[l][k]+1;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

short another[1000002][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(prod > 10){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return prod;
}

void doit()
{
	for(int i = 1; i <= 9; ++i){
		for(int j = 1; j <= 9; ++j){
			another[i][j] = 1;
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		for(int j = 1; j <= 9; ++j){
			if(p == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		if(l >= 1 && l <= 9) count = another[r][k]-another[l][k]+1;
		else if(another[l-1][k] == another[l][k]) count = another[r][k]-another[l][k];
		else count = another[r][k]-another[l][k]+1;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

int dp[1000002] = {0};
int another[1000002][10];

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	another[0][1] = 0;
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			if(i == j) another[i][j] = 1;
			else another[i][j] = another[i-1][j];
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		count = another[r][k]-another[l-1][k];
		//cout << another[l][k] << " " << another[r][k] << endl;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

unsigned short dp[1000001] = {0};
unsigned short another[1000002][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	another[0][1] = 1;
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			if(i == j) another[i][j] = 1;
			else another[i][j] = another[i-1][j];
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		count = another[r][k]-another[l-1][k];
		//cout << another[l][k] << " " << another[r][k] << endl;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

unsigned short another[1000002][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(prod > 10){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return prod;
}

void doit()
{
	for(int i = 1; i <= 9; ++i){
		for(int j = 1; j <= 9; ++j){
			another[i][j] = 1;
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		for(int j = 1; j <= 9; ++j){
			if(p == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		if(l >= 1 && l <= 9) count = another[r][k]-another[l][k]+1;
		else if(another[l-1][k] == another[l][k]) count = another[r][k]-another[l][k];
		else count = another[r][k]-another[l][k]+1;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

int dp[1000010] = {0};
int another[10000010][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			another[i][j] = 1;
		}
	}
	for(int i = 10; i <= 1000001; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		if(l >= 1 && l <= 9) count = another[r][k]-another[l][k]+1;
		else if(another[l-1][k] == another[l][k]) count = another[r][k]-another[l][k];
		else count = another[r][k]-another[l][k]+1;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

unsigned short dp[1000001] = {0};
unsigned short another[1000002][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	another[0][1] = 0;
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			if(i == j) another[i][j] = 1;
			else another[i][j] = another[i-1][j];
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		count = another[r][k]-another[l-1][k];
		//cout << another[l][k] << " " << another[r][k] << endl;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

unsigned short dp[1000001] = {0};
unsigned short another[1000002][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			if(i == j) another[i][j] = 1;
			else another[i][j] = another[i-1][j];
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		count = another[r][k]-another[l][k];
		//cout << another[r][k] << endl;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

int dp[2000001] = {0};
int another[2000001][11] = {1};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			another[i][j] = 1;
		}
	}
	for(int i = 10; i <= 1000001; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		if(l >= 1 && l <= 9) count = another[r][k]-another[l][k]+1;
		else if(another[l-1][k] == another[l][k]) count = another[r][k]-another[l][k];
		else count = another[r][k]-another[l][k]+1;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

int another[1000002][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(prod > 10){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return prod;
}

void doit()
{
	for(int i = 1; i <= 9; ++i){
		for(int j = 1; j <= 9; ++j){
			another[i][j] = 1;
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		for(int j = 1; j <= 9; ++j){
			if(p == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		if(l >= 1 && l <= 9) count = another[r][k]-another[l][k]+1;
		else if(another[l-1][k] == another[l][k]) count = another[r][k]-another[l][k];
		else count = another[r][k]-another[l][k]+1;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

int dp[2000001] = {0};
int another[2000001][11] = {1};
int asche[1000001] = {0};
int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			another[i][j] = 1;
		}
	}
	for(int i = 10; i <= 1000001; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		if(l >= 1 && l <= 9) count = another[r][k]-another[l][k]+1;
		else if(another[l-1][k] == another[l][k]) count = another[r][k]-another[l][k];
		else count = another[r][k]-another[l][k]+1;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

short dp[1000001] = {0};
short another[1000002][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			another[i][j] = 1;
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		if(l >= 1 && l <= 9) count = another[r][k]-another[l][k]+1;
		else if(another[l-1][k] == another[l][k]) count = another[r][k]-another[l][k];
		else count = another[r][k]-another[l][k]+1;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

unsigned short dp[1000001] = {0};
int another[10][1000002] = {1};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	another[0][1] = 0;
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			if(i == j) another[i][j] = 1;
			else another[i][j] = another[i-1][j];
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		count = another[r][k]-another[l-1][k];
		//cout << another[l][k] << " " << another[r][k] << endl;
		cout << count << endl;
	}
}


#include <bits/stdc++.h>

using namespace std;

unsigned short dp[1000001] = {0};
unsigned short another[1000002][10] = {1};

int find_product(int n)
{
	int prod = n;
	while(dp[prod] == 0){
		int x = prod;
		prod = 1;
		while(x > 0){
			if(x%10!=0) prod *= (x%10);
			x/=10;
		}
	}
	return dp[prod];
}

void doit()
{
	for(int i = 1; i <= 9; ++i){
		dp[i] = i;
		for(int j = 1; j <= 9; ++j){
			if(i == j) another[i][j] = 1;
			else another[i][j] = another[i-1][j];
		}
	}
	for(int i = 10; i <= 1000000; ++i){
		int p = find_product(i);
		dp[i] = p;
		for(int j = 1; j <= 9; ++j){
			if(dp[i] == j) another[i][j] = another[i-1][j]+1;
			else another[i][j] = another[i-1][j];
		}
	}
}

int main()
{
	doit();

	int q;
	cin >> q;
	for(int cs = 1; cs <= q; ++cs){
		int l,r,k;
		cin >> l >> r >> k;
		int count;
		if(another[l-1][k] == another[l][k]) count = another[r][k]-another[l][k];
		else count = another[r][k]-another[l][k]+1;
		//cout << another[r][k] << endl;
		cout << count << endl;
	}
}


#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
const int MAXN = 1000 * 1000 + 9;

long long product(long long N)
{
    if(N < 10)
    {
        if(N == 0)
            return 1;
        return N;
    }
    if((N % 10) == 0)   return product(N / 10);
    return (N % 10) * product(N / 10);
}

long long F[MAXN];
long long G[MAXN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

vector<long long> index[15];

void GCal()
{
    for(int i = 10; i < MAXN; i++)
    {
        G[i] = G[F[i]];
        //if(i < 100)
            //cout << G[i] << ' ';
    }
}

void FCal()
{
    for(int i = 1; i < MAXN; i++)
        F[i] = product(i);
};

void preCal()
{
    for(int i = 1; i <= 1000 * 1000; i++)
        index[G[i]].push_back(i);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //freopen("test.INP", "r", stdin);
    FCal();
    GCal();
    preCal();

    int Q;  cin >> Q;
    while(Q--)
    {
        int l, r, k;    cin >> l >> r >> k;
        int res = 0;
        vector<long long>::iterator it1 = lower_bound(index[k].begin(), index[k].end(), l);
        if(it1 == index[k].end())
        {
            cout << 0 << '\n';
            continue;
        }
        while(*it1 <= r)
        {
            res++;
            it1++;
        }
        cout << res << '\n';
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
const int MAXN = 1000 * 1000 + 9;

long long product(long long N)
{
    if(N < 10)
    {
        if(N == 0)
            return 1;
        return N;
    }
    if((N % 10) == 0)   return product(N / 10);
    return (N % 10) * product(N / 10);
}

long long F[MAXN];
long long G[MAXN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

vector<long long> index[15];

void GCal()
{
    for(int i = 10; i < MAXN; i++)
    {
        G[i] = G[F[i]];
        //if(i < 100)
            //cout << G[i] << ' ';
    }
}

void FCal()
{
    for(int i = 1; i < MAXN; i++)
        F[i] = product(i);
};

void preCal()
{
    for(int i = 1; i < MAXN; i++)
        index[G[i]].push_back(i);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //freopen("test.INP", "r", stdin);
    FCal();
    GCal();
    preCal();

    int Q;  cin >> Q;
    while(Q--)
    {
        int l, r, k;    cin >> l >> r >> k;
        int res = 0;
        vector<long long>::iterator it1 = lower_bound(index[k].begin(), index[k].end(), l);
        if(it1 == index[k].end())
        {
            cout << 0 << '\n';
            continue;
        }
        while(*it1 <= r)
        {
            res++;
            it1++;
        }
        cout << res << '\n';
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
const int MAXN = 1000 * 1000 + 9;

long long product(long long N)
{
    if(N < 10)
    {
        if(N == 0)
            return 1;
        return N;
    }
    if((N % 10) == 0)   return product(N / 10);
    return (N % 10) * product(N / 10);
}

long long F[MAXN];
long long G[MAXN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

vector<long long> index[15];

void GCal()
{
    for(int i = 10; i < MAXN; i++)
    {
        G[i] = G[F[i]];
        //if(i < 100)
            //cout << G[i] << ' ';
    }
}

void FCal()
{
    for(int i = 1; i < MAXN; i++)
        F[i] = product(i);
};

void preCal()
{
    for(int i = 1; i < MAXN; i++)
        index[G[i]].push_back(i);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //freopen("test.INP", "r", stdin);
    FCal();
    GCal();
    preCal();

    int Q;  cin >> Q;
    while(Q--)
    {
        int l, r, k;    cin >> l >> r >> k;
        int res = 0;
        vector<long long>::iterator it1 = lower_bound(index[k].begin(), index[k].end(), l);
        if(it1 == index[k].end())
        {
            cout << 0 << '\n';
            continue;
        }

        vector<long long>::iterator it2 = lower_bound(index[k].begin(), index[k].end(), r);
        if(it2 == index[k].end())
            it2--;
        else
            while(*it2 > r) it2--;

        res = it2 - it1 + 1;
        cout << res << '\n';
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
const int MAXN = 1000 * 1000 + 9;

long long product(long long N)
{
    if(N < 10)
    {
        if(N == 0)
            return 1;
        return N;
    }
    if((N % 10) == 0)   return product(N / 10);
    return (N % 10) * product(N / 10);
}

long long F[MAXN];
long long G[MAXN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

vector<long long> index[15];

void GCal()
{
    for(int i = 10; i < MAXN; i++)
    {
        G[i] = G[F[i]];
        //if(i < 100)
            //cout << G[i] << ' ';
    }
}

void FCal()
{
    for(int i = 1; i < MAXN; i++)
        F[i] = product(i);
};

void preCal()
{
    for(int i = 1; i < MAXN; i++)
        index[G[i]].push_back(i);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //freopen("test.INP", "r", stdin);
    FCal();
    GCal();
    preCal();

    int Q;  cin >> Q;
    while(Q--)
    {
        int l, r, k;    cin >> l >> r >> k;
        int res = 0;
        vector<long long>::iterator it1 = lower_bound(index[k].begin(), index[k].end(), l);
        vector<long long>::iterator it2 = lower_bound(index[k].begin(), index[k].end(), r);
        while(*it2 >= r)    it2--;
        res = it2 - it1 + 1;
        cout << res << '\n';
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
const int MAXN = 1000 * 1000 + 9;

long long product(long long N)
{
    if(N < 10)
    {
        if(N == 0)
            return 1;
        return N;
    }
    if((N % 10) == 0)   return product(N / 10);
    return (N % 10) * product(N / 10);
}

long long F[MAXN];
long long G[MAXN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

vector<long long> index[15];

void GCal()
{
    for(int i = 10; i < MAXN; i++)
    {
        G[i] = G[F[i]];
        //if(i < 100)
            //cout << G[i] << ' ';
    }
}

void FCal()
{
    for(int i = 1; i < MAXN; i++)
        F[i] = product(i);
};

void preCal()
{
    for(int i = 1; i < MAXN; i++)
        index[G[i]].push_back(i);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    freopen("test.INP", "r", stdin);
    FCal();
    GCal();
    preCal();

    int Q;  cin >> Q;
    while(Q--)
    {
        int l, r, k;    cin >> l >> r >> k;
        int res = 0;
        vector<long long>::iterator it1 = lower_bound(index[k].begin(), index[k].end(), l);
        if(it1 == index[k].end())
        {
            cout << 0 << '\n';
            continue;
        }
        while(*it1 <= r)
        {
            res++;
            it1++;
        }
        cout << res << '\n';
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
const int MAXN = 1000 * 1000 + 9;

long long product(long long N)
{
    if(N < 10)
    {
        if(N == 0)
            return 1;
        return N;
    }
    if((N % 10) == 0)   return product(N / 10);
    return (N % 10) * product(N / 10);
}

long long F[MAXN];
long long G[MAXN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

vector<long long> index[15];

void GCal()
{
    for(int i = 10; i < MAXN; i++)
    {
        G[i] = G[F[i]];
        //if(i < 100)
            //cout << G[i] << ' ';
    }
}

void FCal()
{
    for(int i = 1; i < MAXN; i++)
        F[i] = product(i);
};

void preCal()
{
    for(int i = 1; i < MAXN; i++)
        index[G[i]].push_back(i);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //freopen("test.INP", "r", stdin);
    FCal();
    GCal();
    preCal();

    int Q;  cin >> Q;
    while(Q--)
    {
        int l, r, k;    cin >> l >> r >> k;
        int res = 0;
        vector<long long>::iterator it1 = lower_bound(index[k].begin(), index[k].end(), l);
        vector<long long>::iterator it2 = lower_bound(index[k].begin(), index[k].end(), r);
        while(*it2 > r)    it2--;
        res = it2 - it1 + 1;
        cout << res << '\n';
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
const int MAXN = 1000 * 1000 + 9;

long long product(long long N)
{
    if(N < 10)
    {
        if(N == 0)
            return 1;
        return N;
    }
    if((N % 10) == 0)   return product(N / 10);
    return (N % 10) * product(N / 10);
}

long long F[MAXN];
long long G[MAXN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

vector<long long> index[15];

void GCal()
{
    for(int i = 10; i < MAXN; i++)
    {
        G[i] = G[F[i]];
        //if(i < 100)
            //cout << G[i] << ' ';
    }
}

void FCal()
{
    for(int i = 1; i < MAXN; i++)
        F[i] = product(i);
};

void preCal()
{
    for(int i = 1; i < MAXN; i++)
        index[G[i]].push_back(i);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //freopen("test.INP", "r", stdin);
    FCal();
    GCal();
    preCal();

    int Q;  cin >> Q;
    while(Q--)
    {
        int l, r, k;    cin >> l >> r >> k;
        int res = 0;
        vector<long long>::iterator it1 = lower_bound(index[k].begin(), index[k].end(), l);
        if(it1 == index[k].end())
        {
            cout << 0 << '\n';
            continue;
        }

        vector<long long>::iterator it2 = lower_bound(index[k].begin(), index[k].end(), r);
        if(*it2 != r)   it2--;

        res = it2 - it1 + 1;
        cout << res << '\n';
    }

    return 0;
}


#include <bits/stdc++.h>
#define FI(n) for(int i = 0; i < n; ++i)
#define FJ(n) for(int j = 0; j < n; ++j)
#define l first
#define r second
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
using namespace std;
ofstream coutf;
ifstream cinf;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vii;
const int MOD=1e9+7;
const ll INF=1e18+1;
int f(int k)
{
    int pr=1;
    while(k>0)
    {
        if(k%10!=0)
        pr*=(k%10);
        k/=10;
    }
    return pr;
}
int func(int c)
{
    if(c<10) return c;
    else return func(f(c));
}
int solve()
{
    int blockSize=1000;
    vector< vector<int> > block(blockSize);
    int lastB=0;
    block[lastB].resize(10,0);
    for(int i=1;i<1000000;i++)
    {
        if(i/blockSize!=lastB)
        {
            //cout<<lastB<<endl;
            lastB++;
            block[lastB].resize(10,0);
        }
        int c=func(i);
        block[lastB][c]++;
    }
    int q;
    cin>>q;
    while(q--)
    {
        int a,b,k;
        cin>>a>>b>>k;
        ll ans=0;
        if((a/blockSize==b/blockSize)||(a/blockSize+1==b/blockSize))
        {
            for(int i=a;i<=b;i++)
            {
                int c=func(i);
                if(c==k) ans++;
            }
        }
        else
        {
            for(int i=a;i<=(a/blockSize+1)*blockSize-1;i++)
            {
                int c=func(i);
                if(c==k) ans++;
            }
            for(int i=a/blockSize+1;i<=b/blockSize-1;i++)
            {
                ans+=block[i][k];
            }
            for(int i=(b/blockSize)*blockSize;i<=b;i++)
            {
                int c=func(i);
                if(c==k) ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cinf.open("input.txt");
    coutf.open("output.txt");
    solve();
    cinf.close();
    coutf.close();
    return 0;
}


#include <bits/stdc++.h>
#define FI(n) for(int i = 0; i < n; ++i)
#define FJ(n) for(int j = 0; j < n; ++j)
#define l first
#define r second
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
using namespace std;
ofstream coutf;
ifstream cinf;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vii;
const int MOD=1e9+7;
const ll INF=1e18+1;
int q;
int l, r, k;

int dp[1000010];
int counter[1000010][9];

int g(int a)
{
    if(dp[a] != -1)
    return dp[a];

    if(a < 10)
    {
        dp[a] = a;
        return a;
    }

    int answ = 1;
    int aa = a;

    while(aa)
    {
        if(aa % 10)
        {
            answ *= aa % 10;
        }
    aa /= 10;
    }

    answ = g(answ);
    dp[a] = answ;
    return answ;
}

void PreProc()
{
    FI(1000010)
        dp[i] = -1;

    FI(9)
        counter[0][i] = 0;

    for(int i = 1 ; i <= 1000000 ; ++i)
    {
        int answ = g(i);
        FJ(9)
            counter[i][j] = counter[i - 1][j];
        counter[i][answ - 1]++;
    }
}

void Vvod()
{
    cin >> q;
    FI(q)
    {
        cin >> l >> r>> k;
        cout<< counter[r][k - 1] - counter[l - 1][k - 1]<< endl;
    }
}

void solve()
{
    PreProc();
    Vvod();
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cinf.open("input.txt");
    coutf.open("output.txt");
    solve();
    cinf.close();
    coutf.close();
    return 0;
}


#include <bits/stdc++.h>
#define FI(n) for(int i = 0; i < n; ++i)
#define FJ(n) for(int j = 0; j < n; ++j)
#define l first
#define r second
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
using namespace std;
ofstream coutf;
ifstream cinf;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vii;
const int MOD=1e9+7;
const ll INF=1e18+1;
int f(int k)
{
    int pr=1;
    while(k>0)
    {
        if(k%10!=0)
        pr*=(k%10);
        k/=10;
    }
    return pr;
}
int func(int c)
{
    if(c<10) return c;
    else return func(f(c));
}
int solve()
{
    int blockSize=1000;
    vector<vector<int>> block(blockSize);
    int lastB=0;
    block[lastB].resize(10,0);
    for(int i=1;i<1000000;i++)
    {
        if(i/blockSize!=lastB)
        {
            //cout<<lastB<<endl;
            lastB++;
            block[lastB].resize(10,0);
        }
        int c=func(i);
        block[lastB][c]++;
    }
    //for(int i=0;i<blockSize;i++)  cout<<block[i][2]<<endl;
    int q;
    cin>>q;
    while(q--)
    {
        int a,b,k;
        cin>>a>>b>>k;
        ll ans=0;
        if((a/blockSize==b/blockSize)||(a/blockSize+1==b/blockSize))
        {
            for(int i=a;i<=b;i++)
            {
                int c=func(i);
                if(c==k) ans++;
            }
        }
        else
        {
            for(int i=a;i<=(a/blockSize+1)*blockSize-1;i++)
            {
                int c=func(i);
                if(c==k) ans++;
            }
            for(int i=a/blockSize+1;i<=b/blockSize-1;i++)
            {
                ans+=block[i][k];
            }
            for(int i=(b/blockSize)*blockSize;i<=b;i++)
            {
                int c=func(i);
                if(c==k) ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cinf.open("input.txt");
    coutf.open("output.txt");
    solve();
    cinf.close();
    coutf.close();
    return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <stack>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <functional>
//#include <unordered_set>
#define INF 2147483647
#define MAXN  200005
#define mod 1000000007
#define PII pair<int, int>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int c[1000000 + 5];
int x[1000000][9];
int calc(int n)
{
	if (n < 10)
		return n;
	int d, a = 1,b=n;
	while (n)
	{
		d = n % 10;
		n /= 10;
		if (d != 0)
			a *= d;
	}
	c[a] == -1 ? c[b]=c[a] = calc(a) : c[b]=c[a];
}
int main()
{
	//ios::sync_with_stdio(false);
	cin.tie(0);
#ifdef local_debug
	ifstream inputs("input.txt");
	cin.rdbuf(inputs.rdbuf());
	freopen("input.txt", "r", stdin);
#endif
	memset(c, -1, sizeof(c));
	memset(x, 0, sizeof(x));
	for (int i = 10; i <= 1000000; i++)
		calc(i);
	for (int i = 0; i < 10; i++) {
		for (int j = 1; j <= 1000000; j++)
		{
			x[j][i] = x[j - 1][i] + (bool)(c[j] == i);
		}
	}
	int n, l, r, k;
	while (scanf("%d",&n)!=EOF)
	{
		while (n--)
		{
			//cin >> l >> r >> k;
			scanf("%d%d%d", &l, &r, &k);
			int ans = x[r][k] - x[l - 1][k];
			cout << ans << endl;
		}
	}
	return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <stack>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <functional>
//#include <unordered_set>
#define INF 2147483647
#define MAXN  200005
#define mod 1000000007
#define PII pair<int, int>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int c[1000000 + 5], x[1000000 + 5];
int calc(int n)
{
	if (n < 10)
		return n;
	int d, a = 1,b=n;
	while (n)
	{
		d = n % 10;
		n /= 10;
		if (d != 0)
			a *= d;
	}
	c[a] == -1 ? c[b]=c[a] = calc(a) : c[b]=c[a];
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
#ifdef local_debug
	ifstream inputs("input.txt");
	cin.rdbuf(inputs.rdbuf());
	freopen("input.txt", "r", stdin);
#endif
	memset(c, -1, sizeof(c));
	memset(x, 0, sizeof(x));
	for (int i = 10; i <= 1000000; i++)
		calc(i);
	int n, l, r, k;
	while (cin >> n)
	{
		while (n--)
		{
			cin >> l >> r >> k;
			ll ans = 0;
			for (int i = l; i <= r; i++)
				ans += (bool)(c[i] == k);
			cout << ans << endl;
		}
	}
	return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <stack>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <functional>
//#include <unordered_set>
#define INF 2147483647
#define MAXN  200005
#define mod 1000000007
#define PII pair<int, int>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int c[1000000 + 5];
int x[1000000][9];
int calc(int n)
{
	if (n < 10)
		return n;
	int d, a = 1,b=n;
	while (n)
	{
		d = n % 10;
		n /= 10;
		if (d != 0)
			a *= d;
	}
	c[a] == -1 ? c[b]=c[a] = calc(a) : c[b]=c[a];
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
#ifdef local_debug
	ifstream inputs("input.txt");
	cin.rdbuf(inputs.rdbuf());
	freopen("input.txt", "r", stdin);
#endif
	memset(c, -1, sizeof(c));
	memset(x, 0, sizeof(x));
	for (int i = 10; i <= 1000000; i++)
		calc(i);
	for (int i = 0; i < 10; i++) {
		for (int j = 1; j <= 1000000; j++)
		{
			x[j][i] = x[j - 1][i] + (bool)(c[j] == i);
		}
	}
	int n, l, r, k;
	while (cin >> n)
	{
		while (n--)
		{
			cin >> l >> r >> k;
			int ans = x[r][k] - x[l - 1][k];
			cout << ans << endl;
		}
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>

const int MAXN = 1e6 + 5;

int Ans[MAXN];

int f(int x)
{
	static char s[10];
	int ans = 1;
	int len = sprintf(s, "%d", x);
	for(int i = 0; i < len; ++i)
		if(s[i] != '0')
			ans *= s[i] - '0';
	return ans;
}

int cal(int a)
{
	if(~Ans[a])
		return Ans[a];
	if(a < 10)
		return a;
	else
		return cal(f(a));
}

int solve(int l, int r, int k)
{
	int x = 0;
	for(int i = l; i <= r; ++i)
	{
		if((Ans[i] = cal(i)) == k)
			++x;
	}
	return x;
}

int main()
{
	int q;
	memset(Ans, -1, sizeof Ans);
	std::cin >> q;
	while(q--)
	{
		int l, r, k;
		std::cin >> l >> r >> k;
		printf("%d\n", solve(l, r, k));
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>

const int MAXN = 1e6 + 5;

int Ans[MAXN], Mul[MAXN];
int X[10][MAXN];

int xx;

int cal(int a)
{
	if(a < 10)
		return a;
	if(~Ans[a])
		return Ans[a];
	else
	{
		Mul[a] = Mul[a % xx] * (a / xx);
		return cal(Mul[a]);
	}
}

int solve(int l, int r, int k)
{
	int x = 0;
	xx = 1;
	while(l >= 10 * xx)
		xx *= 10;
	for(int i = l; i <= r; ++i)
	{
		if(i >= xx * 10)
			xx *= 10;
		if((Ans[i] = cal(i)) == k)
			++x;
	}
	return x;
}

int main()
{
	int q;
	memset(Ans, -1, sizeof Ans);
	Ans[0] = Mul[0] = 1;
	xx = 1;
	for(int i = 1; i < 10; ++i)
		Mul[i] = i;
	for(int i = 1; i < MAXN; ++i)
	{
		if(i >= xx * 10)
			xx *= 10;
		Ans[i] = cal(i);
		for(int j = 1; j <= 9; ++j)
		{
			X[j][i] = X[j][i - 1];
		}
		X[Ans[i]][i]++;
	}
	std::cin >> q;
	while(q--)
	{
		int l, r, k;
		std::cin >> l >> r >> k;
		std::cout << X[k][r] - X[k][l - 1] << std::endl;
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>

const int MAXN = 1e6 + 5;

int Ans[MAXN], Mul[MAXN];
int X[10][MAXN];

int xx;

int cal(int a)
{
	if(a < 10)
		return a;
	if(~Ans[a])
		return Ans[a];
	else
	{
		Mul[a] = Mul[a % xx] * (a / xx);
		return cal(Mul[a]);
	}
}

int solve(int l, int r, int k)
{
	int x = 0;
	xx = 1;
	while(l >= 10 * xx)
		xx *= 10;
	for(int i = l; i <= r; ++i)
	{
		if(i >= xx * 10)
			xx *= 10;
		if((Ans[i] = cal(i)) == k)
			++x;
	}
	return x;
}

int main()
{
	freopen("prog.out", "w", stdout);
	int q;
	memset(Ans, -1, sizeof Ans);
	Ans[0] = 1;
	Mul[0] = 1;
	xx = 1;
	for(int i = 1; i < 10; ++i)
		Mul[i] = i;
	for(int i = 1; i < MAXN; ++i)
	{
		if(i >= xx * 10)
			xx *= 10;
		Ans[i] = cal(i);
		printf("%d\n", Ans[i]);
		for(int j = 1; j <= 9; ++j)
		{
			X[j][i] = X[j][i - 1];
		}
		X[Ans[i]][i]++;
	}
	/*std::cin >> q;
	while(q--)
	{
		int l, r, k;
		std::cin >> l >> r >> k;
		std::cout << X[k][r] - X[k][l - 1] << std::endl;
	}*/
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>

const int MAXN = 1e6 + 5;

int Ans[MAXN];

int f(int x)
{
	static char s[10];
	char *top = s;
	int ans = 1;
	do
		*++top = x % 10, x /= 10;
	while(x);
	for(char *p = top; p > s; --p)
		if(*p)
			ans *= *p;
	return ans;
}

int cal(int a)
{
	if(~Ans[a])
		return Ans[a];
	if(a < 10)
		return a;
	else
		return cal(f(a));
}

int solve(int l, int r, int k)
{
	int x = 0;
	for(int i = l; i <= r; ++i)
	{
		if((Ans[i] = cal(i)) == k)
			++x;
	}
	return x;
}

int main()
{
	int q;
	std::ios_base::sync_with_stdio(false);
	memset(Ans, -1, sizeof Ans);
	std::cin >> q;
	while(q--)
	{
		int l, r, k;
		std::cin >> l >> r >> k;
		printf("%d\n", solve(l, r, k));
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>

const int MAXN = 1e6 + 5;

int Ans[MAXN];

int f(int x)
{
	static char s[10];
	int ans = 1;
	int len = sprintf(s, "%d", x);
	for(int i = 0; i < len; ++i)
		if(s[i] != '0')
			ans *= s[i] - '0';
	return ans;
}

int xx = 1;

int cal(int a)
{
	if(~Ans[a])
		return Ans[a];
	else
	{
		//std::cout << a << ": " << a % xx << " " << a / xx << std::endl;
		return cal(Ans[a % xx] * (a / xx));
	}
}

int solve(int l, int r, int k)
{
	int x = 0;
	xx = 1;
	while(l >= 10 * xx)
		xx *= 10;
	for(int i = l; i <= r; ++i)
	{
		if(i >= xx * 10)
			xx *= 10;
		if((Ans[i] = cal(i)) == k)
			++x;
	}
	return x;
}

int main()
{
	int q;
	memset(Ans, -1, sizeof Ans);
	Ans[0] = 1;
	for(int i = 1; i < 10; ++i)
		Ans[i] = i;
	std::cin >> q;
	while(q--)
	{
		int l, r, k;
		std::cin >> l >> r >> k;
		printf("%d\n", solve(l, r, k));
	}
	return 0;
}


#include <cstdio>
#include <iostream>

int f(int x)
{
	static char s[10];
	int ans = 1;
	int len = sprintf(s, "%d", x);
	for(int i = 0; i < len; ++i)
		if(s[i] != '0')
			ans *= s[i] - '0';
	return ans;
}

int cal(int a)
{
	if(a < 10)
		return a;
	else
		return cal(f(a));
}

int solve(int l, int r, int k)
{
	int x = 0;
	for(int i = l; i <= r; ++i)
	{
		if(cal(i) == k)
			++x;
	}
	return x;
}

int main()
{
	int q;
	std::cin >> q;
	while(q--)
	{
		int l, r, k;
		std::cin >> l >> r >> k;
		printf("%d\n", solve(l, r, k));
	}
	return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>

const int MAXN = 1e6 + 5;

int Ans[MAXN];
int X[10][MAXN];

int xx;

int cal(int a)
{
	if(a < 10)
		return a;
	if(~Ans[a])
		return Ans[a];
	else
	{
		return cal(Ans[a % xx] * (a / xx));
	}
}

int solve(int l, int r, int k)
{
	int x = 0;
	xx = 1;
	while(l >= 10 * xx)
		xx *= 10;
	for(int i = l; i <= r; ++i)
	{
		if(i >= xx * 10)
			xx *= 10;
		if((Ans[i] = cal(i)) == k)
			++x;
	}
	return x;
}

int main()
{
	int q;
	memset(Ans, -1, sizeof Ans);
	Ans[0] = 1;
	xx = 1;
	for(int i = 1; i < MAXN; ++i)
	{
		if(i >= xx * 10)
			xx *= 10;
		Ans[i] = cal(i);
		for(int j = 1; j <= 9; ++j)
		{
			X[j][i] = X[j][i - 1];
		}
		X[Ans[i]][i]++;
	}
	std::cin >> q;
	while(q--)
	{
		int l, r, k;
		std::cin >> l >> r >> k;
		std::cout << X[k][r] - X[k][l - 1] << std::endl;
	}
	return 0;
}


#include<iostream>
using namespace std;
int a[10][1000005];
int fun(int n)
{
    int x;
    int num = 1;
    if(n<10)
        return n;
    while(1)
    {
        while(n!=0)
        {
            x = n%10;
            if(x!=0)
                num = num*x;
            n = n/10;
        }
        if(num>0 && num<=9)
            return num;
        n = num;
        num = 1;
    }

}
int main()
{
    int l,r,k;
    int q;
    for(int i=1;i<=9;i++)
    {
        for(int j=1;j<=1000000;j++)
        {
            int m = fun(j);
            if(i==m)
                a[i][j] = a[i][j-1]+1;
            else
                a[i][j] = a[i][j-1];
        }
    }
    cin>>q;
    while(q--)
    {
        cin>>l>>r>>k;
        cout<<a[k][r]-a[k][l-1]<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int a[10][1000005];
int fun(int n)
{
    int x;
    int num = n;
    if(n<=9)
        return n;
    else
    {
        while(num>9)
        {
            n = num;
            num = 1;
            while(n!=0)
            {
                x = n%10;
                if(x!=0)
                    num = num*x;
                n = n/10;
            }
        }
        return num;
    }
}
int main()
{
    int l,r,k;
    int q;
    for(int i=1;i<=9;i++)
    {
        for(int j=1;j<=1000000;j++)
        {
            int m = fun(j);
            if(i==m)
                a[i][j] = a[i][j-1]+1;
            else
                a[i][j] = a[i][j-1];
        }
    }
    cin>>q;
    while(q--)
    {
        cin>>l>>r>>k;
        cout<<a[k][r]-a[k][l-1]<<endl;
    }
    return 0;
}


#include<iostream>
using namespace std;
int a[10][1000005];
int fun(int n)
{
    int x;
    int num = 1;
    while(1)
    {
        while(n!=0)
        {
            x = n%10;
            if(x!=0)
                num = num*x;
            n = n/10;
        }
        if(num>0 && num<=9)
            return num;
        n = num;
        num = 1;
    }

}
int main()
{
    int l,r,k;
    int q;
    for(int i=1;i<=9;i++)
    {
        for(int j=1;j<=1000000;j++)
        {
            int m = fun(j);
            if(i==m)
                a[i][j] = a[i][j-1]+1;
            else
                a[i][j] = a[i][j-1];
        }
    }
    cin>>q;
    while(q--)
    {
        cin>>l>>r>>k;
        cout<<a[k][r]-a[k][l-1]<<endl;
    }
    return 0;
}


#include <iostream>
#include<vector>
#include <bits/stdc++.h>
using namespace std;
int l, r,k,n;
vector<vector<int> >v;
int fun(int num){
    long long multi=1;
while(num!=0){
        if(num%10!=0)
    multi*=num%10;
    num/=10;
}
return multi;
}
int recursive_queries(int num){
    if(num<10)
        return num;
    return recursive_queries(fun(num));

}
int main()
{
    cin>>n;
    v.resize(15);
    for(int i=1 ;i<=1e6;i++){
        v[recursive_queries(i)].push_back(i);
    }

    for(int i=0;i<n;i++){
        cin>>l>>r>>k;
        vector<int>BS=v[k];
        int start=0,endi=BS.size()-1,mid,startIND=0,endIND=-1;
       /* for(int i=0;i<BS.size();i++){
            cout<<BS[i]<<' ';
        }*/

        while(start<=endi){
            mid=(start+endi)/2;
            if(BS[mid]==l){
                startIND=mid;
                break;
            }
            if(BS[mid]<l){
                start=mid+1;
            }
            else{
                endi=mid-1;
                startIND=mid;
            }
        }
        start=0;endi=BS.size()-1;
        while(start<=endi){
                if(BS[mid]==r){
                endIND=mid;
                break;
            }
            mid=(start+endi)/2;
            if(BS[mid]<=r){
                start=mid+1;
   if(mid>=startIND)
                endIND=mid;
            }
            else{
                endi=mid-1;

            }
        }
        cout<<endIND-startIND+1<<endl;
    }
    return 0;
}


#include <iostream>
#include<vector>
#include <bits/stdc++.h>
using namespace std;
int l, r,k,n;
vector<vector<int> >v;
int fun(int num){
    long long multi=1;
while(num!=0){
        if(num%10!=0)
    multi*=num%10;
    num/=10;
}
return multi;
}
int recursive_queries(int num){
    if(num<10)
        return num;
    return recursive_queries(fun(num));

}
int main()
{
    cin>>n;
    v.resize(15);
    for(int i=1 ;i<=1e6;i++){
        v[recursive_queries(i)].push_back(i);
    }

    for(int i=0;i<n;i++){
        cin>>l>>r>>k;
        vector<int>BS=v[k];
        int start=0,endi=BS.size()-1,mid,startIND=0,endIND=-1;
       /* for(int i=0;i<BS.size();i++){
            cout<<BS[i]<<' ';
        }*/

        while(start<=endi){
            mid=(start+endi)/2;

            if(BS[mid]<l){
                start=mid+1;
            }
            else{
                endi=mid-1;
            if(BS[mid]<=r)
                startIND=mid;
            }
        }
        start=0;endi=BS.size()-1;
        while(start<=endi){

            mid=(start+endi)/2;
            if(BS[mid]<=r){
                   // cout<<"mid"<<mid<<endl;
                start=mid+1;
               if(BS[mid]>=l)
                endIND=mid;
            }
            else{
                endi=mid-1;

            }
        }
       // cout<<endIND<<' '<<startIND<<endl;
        cout<<endIND-startIND+1<<endl;
    }
    return 0;
}


#include <iostream>
#include<vector>
#include <bits/stdc++.h>
using namespace std;
int l, r,k,n;
vector<vector<int> >v;
int fun(int num){
    long long multi=1;
while(num!=0){
        if(num%10!=0)
    multi*=num%10;
    num/=10;
}
return multi;
}
int recursive_queries(int num){
    if(num<10)
        return num;
    return recursive_queries(fun(num));

}
int main()
{
    cin>>n;
    v.resize(15);
    for(int i=1 ;i<=1e6;i++){
        v[recursive_queries(i)].push_back(i);
    }

    for(int i=0;i<n;i++){
        cin>>l>>r>>k;
        vector<int>BS=v[k];
        int start=0,endi=BS.size()-1,mid,startIND=0,endIND=-1;
       /* for(int i=0;i<BS.size();i++){
            cout<<BS[i]<<' ';
        }*/

        while(start<=endi){
            mid=(start+endi)/2;
            if(BS[mid]==l){
                startIND=mid;
                break;
            }
            if(BS[mid]<l){
                start=mid+1;
            }
            else{
                endi=mid-1;
                startIND=mid;
            }
        }
        start=0;endi=BS.size()-1;
        while(start<=endi){
                if(BS[mid]==r){
                endIND=mid;
                break;
            }
            mid=(start+endi)/2;
            if(BS[mid]<=r){
                start=mid+1;
                endIND=mid;
            }
            else{
                endi=mid-1;

            }
        }
        cout<<endIND-startIND+1<<endl;
    }

   return 0;
}


#include <iostream>
#include<vector>
#include <bits/stdc++.h>
using namespace std;
int l, r,k,n;
int arr[15][(int)(1e6+5)]={};
vector<vector<int> >v;
int fun(int num){
    long long multi=1;
while(num!=0){
        if(num%10!=0)
    multi*=num%10;
    num/=10;
}
return multi;
}
int recursive_queries(int num){
    if(num<10)
        return num;
    return recursive_queries(fun(num));

}
int main()
{
    cin>>n;
   // v.resize(15);
    int counter_i;
    for(int i=1;i<10;i++){
             counter_i=0;
        for(int j=1;j<=1e6;j++)
        {
            if(recursive_queries(j)==i){
                counter_i++;
            }
            arr[i][j]=counter_i;
        }
    }

    for(int i=0;i<n;i++){
        cin>>l>>r>>k;

        cout<<arr[k][r]-arr[k][l-1]<<endl;
    }
    return 0;
}


#include <iostream>
#include<vector>
#include <bits/stdc++.h>
using namespace std;
int l, r,k,n;
int arr[15][(int)(1e6+5)];
int fun(int num){
    long long multi=1;
while(num!=0){
        if(num%10!=0)
    multi*=num%10;
    num/=10;
}
return multi;
}
int recursive_queries(int num){
    if(num<10)
        return num;
    return recursive_queries(fun(num));

}
int main()
{
    cin>>n;
    //v.resize(15);

    for(int j=1;j<10;j++){
            int coun=0;
      for(int i=1 ;i<=1e6;i++){
        if(recursive_queries(i)==j)
        coun++;
    arr[j][i]=coun;
    }
    }

    for(int i=0;i<n;i++){
        cin>>l>>r>>k;
        //vector<int>BS=v[k];
        //int start=0,endi=BS.size()-1,mid,startIND=0,endIND=-1;
       /* for(int i=0;i<BS.size();i++){
            cout<<BS[i]<<' ';
        }*/

      /*  while(start<=endi){
            mid=(start+endi)/2;
            if(BS[mid]==l){
                startIND=mid;
                break;
            }
            if(BS[mid]<l){
                start=mid+1;
            }
            else{
                endi=mid-1;
                startIND=mid;
            }
        }
        start=0;endi=BS.size()-1;
        while(start<=endi){
                if(BS[mid]==r){
                endIND=mid;
                break;
            }
            mid=(start+endi)/2;
            if(BS[mid]<=r){
                start=mid+1;
                endIND=mid;
            }
            else{
                endi=mid-1;

            }
        }*/
        cout<<arr[k][r]-arr[k][l-1]<<endl;
    }

   return 0;
}


#include <iostream>
#include<vector>
#include <bits/stdc++.h>
using namespace std;
int l, r,k,n;
vector<vector<int> >v;
int fun(int num){
    long long multi=1;
while(num!=0){
        if(num%10!=0)
    multi*=num%10;
    num/=10;
}
return multi;
}
int recursive_queries(int num){
    if(num<10)
        return num;
    return recursive_queries(fun(num));

}
int main()
{
    cin>>n;
    v.resize(15);
    for(int i=1 ;i<=1e6;i++){
        v[recursive_queries(i)].push_back(i);
    }
    for(int i=0;i<n;i++){
        cin>>l>>r>>k;
        vector<int>BS=v[k];
        int start=0,endi=BS.size()-1,mid,startIND=0,endIND=-1;
       /* for(int i=0;i<BS.size();i++){
            cout<<BS[i]<<' ';
        }*/
        while(start<=endi){
            mid=(start+endi)/2;
            if(BS[mid]<l){
                start=mid+1;
            }
            else{
                endi=mid-1;
                startIND=mid;
            }
        }
        start=0;endi=BS.size()-1;
        while(start<=endi){
            mid=(start+endi)/2;
            if(BS[mid]<=r){
                start=mid+1;
                endIND=mid;
            }
            else{
                endi=mid-1;

            }
        }
        cout<<endIND-startIND+1<<endl;
    }

   return 0;
}


#include <iostream>
#include<vector>
#include <bits/stdc++.h>
using namespace std;
int l, r,k,n;
vector<vector<int> >v;
int fun(int num){
    long long multi=1;
while(num!=0){
        if(num%10!=0)
    multi*=num%10;
    num/=10;
}
return multi;
}
int recursive_queries(int num){
    if(num<10)
        return num;
    return recursive_queries(fun(num));

}
int main()
{
    cin>>n;
    v.resize(15);
    for(int i=1 ;i<=1e6;i++){
        v[recursive_queries(i)].push_back(i);
    }

    for(int i=0;i<n;i++){
        cin>>l>>r>>k;
        vector<int>BS=v[k];
        int start=0,endi=BS.size()-1,mid,startIND=0,endIND=-1;
       /* for(int i=0;i<BS.size();i++){
            cout<<BS[i]<<' ';
        }*/

        while(start<=endi){
            mid=(start+endi)/2;
            if(bs[mid]==l){
                startIND=mid;
                break;
            }
            if(BS[mid]<l){
                start=mid+1;
            }
            else{
                endi=mid-1;
                startIND=mid;
            }
        }
        start=0;endi=BS.size()-1;
        while(start<=endi){
                if(bs[mid]==r){
                endIND=mid;
                break;
            }
            mid=(start+endi)/2;
            if(BS[mid]<=r){
                start=mid+1;
                endIND=mid;
            }
            else{
                endi=mid-1;

            }
        }
        cout<<endIND-startIND+1<<endl;
    }

   return 0;
}


#include <bits/stdc++.h>
using namespace std;
int dp[1000005];
int prod(int a)
{
	if (a<10)return dp[a]=a;
	if (dp[a]!=0)return dp[a];
	int rem=1;
	int ram=a;
	while (a>0)
	{
		if (a%10!=0)
			rem*=a%10;
		a/=10;
	}
	return dp[ram]=prod(rem);
}
int main()
{
	int tc;
	cin>>tc;
	while (tc--)
	{
		int l,r,k,co=0;
		scanf("%d%d%d",&l,&r,&k);
		for (int i=l;i<=r;i++)
		{
			if (prod(i)==k)co++;
		}
		printf ("%d\n",co);
	}
}


#include <bits/stdc++.h>
using namespace std;
int dp[1000005];
int prod(int a)
{
	if (a<10)return dp[a]=a;
	if (dp[a]!=0)
		return dp[a];
	int rem=1;
	int ram=a;
	while (a>0)
	{
		if (a%10!=0)
			rem*=a%10;
		a/=10;
	}
	return dp[ram]=prod(rem);
}
int main()
{
	for (int i=1;i<1000005;i++)
	{
		prod(i);
	}
	int tc;
	cin>>tc;
	while (tc--)
	{
		int l,r,k,co=0;
		scanf("%d%d%d",&l,&r,&k);
		for (int i=l;i<=r;i++)
		{
			if (dp[i]==k)co++;
		}
		printf ("%d\n",co);
	}
}


#include <bits/stdc++.h>
using namespace std;
int dp[1000005];
int dp2[1000005][9];
int prod(int a)
{
	if (a<10)return dp[a]=a;
	if (dp[a]!=0)
		return dp[a];
	int rem=1;
	int ram=a;
	while (a>0)
	{
		if (a%10!=0)
			rem*=a%10;
		a/=10;
	}
	return dp[ram]=prod(rem);
}
int main()
{
	for (int i=1;i<1000005;i++)
	{
		prod(i);
	}
	dp2[1][1]=1;
	for (int i=2;i<1000005;i++)
	{
		for (int j=1;j<=9;j++)
		{
			dp2[i][j]=dp2[i-1][j];
			if (dp[i]==j)dp2[i][j]++;
		}
	}
	int tc;
	cin>>tc;
	while (tc--)
	{
		int l,r,k,co=0;
		scanf("%d%d%d",&l,&r,&k);
		printf ("%d\n",dp2[r][k]-dp2[l-1][k]);
	}
}


#include <bits/stdc++.h>
using namespace std;
int prod(int a)
{
	int rem=11;
	while (rem>10)
	{
		rem=1;
		while (a>0)
		{
			if (a%10!=0)rem*=a%10;
			a/=10;
		}
		a=rem;
	}
	return rem;
}
int main()
{
	int tc;
	cin>>tc;
	while (tc--)
	{
		int l,r,k,co=0;
		scanf("%d%d%d",&l,&r,&k);
		for (int i=l;i<=r;i++)
		{
			if (prod(i)==k)co++;
		}
		printf ("%d\n",co);
	}
}


#include <bits/stdc++.h>
using namespace std;
int dp[1000005];
int prod(int a)
{
	if (a<10)return dp[a]=a;
	while (a%10>0)
		a/=10;
	if (dp[a]!=0)
		return dp[a];
	int rem=1;
	int ram=a;
	while (a>0)
	{
		if (a%10!=0)
			rem*=a%10;
		a/=10;
	}
	return dp[ram]=prod(rem);
}
int main()
{
	int tc;
	cin>>tc;
	while (tc--)
	{
		int l,r,k,co=0;
		scanf("%d%d%d",&l,&r,&k);
		for (int i=l;i<=r;i++)
		{
			if (prod(i)==k)co++;
		}
		printf ("%d\n",co);
	}
}


#include <bits/stdc++.h>
using namespace std;
int dp[1000005];
int dp2[1000005][10];
int prod(int a)
{
	if (a<10)return dp[a]=a;
	if (dp[a]!=0)
		return dp[a];
	int rem=1;
	int ram=a;
	while (a>0)
	{
		if (a%10!=0)
			rem*=a%10;
		a/=10;
	}
	return dp[ram]=prod(rem);
}
int main()
{
	for (int i=1;i<1000005;i++)
	{
		prod(i);
	}
	dp2[1][1]=1;
	for (int i=2;i<1000005;i++)
	{
		for (int j=1;j<=9;j++)
		{
			dp2[i][j]=dp2[i-1][j];
			if (dp[i]==j)dp2[i][j]++;
		}
	}
	int tc;
	cin>>tc;
	while (tc--)
	{
		int l,r,k,co=0;
		scanf("%d%d%d",&l,&r,&k);
		printf ("%d\n",dp2[r][k]-dp2[l-1][k]);
	}
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin>>n;
	vector < set<int> > v(10);
	map <int,int> mp;
	int i;
	for(i=1;i<=1000000;i++)
	{
		int j=i;
		while(j>=10)
		{
			int k=j;
			int prod=1;
			while(k>0)
			{
				if(k%10!=0)
				prod=prod*(k%10);
				k/=10;
			}
			j=prod;
			if(mp.find(j)!=mp.end())
			j=mp.find(j)->second;

		}
		v[j].insert(i);
		mp.insert(make_pair(i,j));
	}
	while(n-->0)
	{
		int l,u,k;
		cin>>l>>u>>k;
		set<int> ::iterator t1,t2;
		t1=lower_bound(v[k].begin(),v[k].end(),u);
		t2=lower_bound(v[k].begin(),v[k].end(),l);
		int c=distance(t2,t1);
		if(*t2!=l && *t1==u)
		c++;
		if(*t1==u && *t2==l)
		c++;
		cout<<c<<endl;
	}

}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin>>n;
	vector < set<int> > v(10);
	int i;
	for(i=1;i<=1000000;i++)
	{
		int j=i;
		while(j>=10)
		{
			int k=j;
			int prod=1;
			while(k>0)
			{
				if(k%10!=0)
				prod=prod*(k%10);
				k/=10;
			}
			j=prod;
		}
		v[j].insert(i);
	}
	while(n-->0)
	{
		int l,u,k;
		cin>>l>>u>>k;
		set<int> ::iterator t1,t2;
		t1=lower_bound(v[k].begin(),v[k].end(),u);
		t2=lower_bound(v[k].begin(),v[k].end(),l);
		int c=distance(t2,t1);
		if(*t2!=l && *t1==u)
		c++;
		if(*t1==u && *t2==l)
		c++;
		cout<<c<<endl;
	}

}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin>>n;
	map <int,int> mp;
	int i;
	int ar[10][1000005];
	memset(ar,0,sizeof(ar));
	for(i=1;i<=1000000;i++)
	{
		int j=i;
		int prod=1;
		while(j>0)
		{
			if(j%10!=0)
			prod=prod*(j%10);
			j/=10;
		}
		if(i>9)
		prod=mp.find(prod)->second;
		mp.insert(make_pair(i,prod));
		ar[prod][i]++;
	}
	for(i=1;i<10;i++)
	for(int j=1;j<=1000000;j++)
	ar[i][j]+=ar[i][j-1];

	while(n-->0)
	{
		int l,u,k;
		cin>>l>>u>>k;
		cout<<ar[k][u]-ar[k][l-1]<<endl;
	}

}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	cin>>n;
	vector < set<int> > v(10);
	int i;
	for(i=1;i<=100;i++)
	{
		int j=i;
		while(j>=10)
		{
			int k=j;
			int prod=1;
			while(k>0)
			{
				if(k%10!=0)
				prod=prod*(k%10);
				k/=10;
			}
			j=prod;
		}
		v[j].insert(i);
	}
	while(n-->0)
	{
		int l,u,k;
		cin>>l>>u>>k;
		set<int> ::iterator t1,t2;
		t1=lower_bound(v[k].begin(),v[k].end(),u);
		t2=lower_bound(v[k].begin(),v[k].end(),l);
		int c=distance(t2,t1);
		if(*t2!=l && *t1==u)
		c++;
		if(*t1==u && *t2==l)
		c++;
		cout<<c<<endl;
	}

}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin>>n;
	vector < set<int> > v(10);
	int i;
	for(i=1;i<=1000000;i++)
	{
		int j=i;
		while(j>=10)
		{
			int k=j;
			int prod=1;
			while(k>0)
			{
				if(k%10!=0)
				prod=prod*(k%10);
				k/=10;
			}
			j=prod;
		}
		v[j].insert(i);
	}
	while(n-->0)
	{
		int l,u,k;
		cin>>l>>u>>k;
		set<int> ::iterator t1,t2;
		t1=lower_bound(v[k].begin(),v[k].end(),u);
		t2=lower_bound(v[k].begin(),v[k].end(),l);
		int c=distance(t2,t1);
		if(*t2!=l && *t1==u)
		c++;
		if(*t1==u && *t2==l)
		c++;
		cout<<c<<endl;
	}

}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n;
	cin>>n;
	vector < set<int> > v(10);
	map <int,int> mp;
	int i;
	for(i=1;i<=1000000;i++)
	{
		int j=i;
		int prod=1;
		while(j>0)
		{
			if(j%10!=0)
			prod=prod*(j%10);
			j/=10;
		}
		if(i>9)
		prod=mp.find(prod)->second;
		v[prod].insert(i);
		mp.insert(make_pair(i,prod));
//		cout<<i<<" "<<prod<<endl;
	}
	vector<int> vec(n);
	int x=n,pos=0;
	while(n-->0)
	{
		int l,u,k;
		cin>>l>>u>>k;
		set<int> ::iterator t1,t2;
		t1=lower_bound(v[k].begin(),v[k].end(),u);
		t2=lower_bound(v[k].begin(),v[k].end(),l);
		int c=distance(t2,t1);
		if(*t2!=l && *t1==u)
		c++;
		if(*t1==u && *t2==l)
		c++;
		vec[pos++]=c;
	}
	for(i=0;i<x;i++)
	cout<<vec[i]<<endl;

}


//╭╮╭╮╭━━╮╭━━╮╭╮╭╮
//┃┃┃┃┃╭╮┃┃╭━╯┃╰╯┃
//┃╰╯┃┃╰╯┃┃┃　　┃　╭╯
//┃╭╮┃┃╭╮┃┃┃　　┃　╰╮
//┃┃┃┃┃┃┃┃┃╰━╮┃╭╮┃
//╰╯╰╯╰╯╰╯╰━━╯╰╯╰╯
//╭╭╮╮╭━━╮　╭━━╮╭━━╮
//┃　　┃┃╭━╯　╰╮╭╯┃╭━╯
//┃┃┃┃┃╰━╮　　┃┃　┃╰━╮
//┃╭╮┃┃╭━╯　　┃┃　┃╭━╯
//┃┃┃┃┃╰━╮　╭╯╰╮┃┃
//╰╯╰╯╰━━╯  ╰━━╯╰╯
//　╭╮╭╮╭━━╮╭╮╭╮
//　┃╰╯┃┃╭╮┃┃┃┃┃
//　╰╮╭╯┃┃┃┃┃┃┃┃
//　　┃┃　┃┃┃┃┃┃┃┃
//　　┃┃　┃╰╯┃┃╰╯┃
//  　╰╯　╰━━╯╰━━╯
//╭━━╮╭━━╮╭╮╭╮
//┃╭━╯┃╭╮┃┃╰╮┃
//┃┃　　┃╰╯┃┃　　┃
//┃┃　　┃╭╮┃┃　　┃
//┃╰━╮┃┃┃┃┃╰╮┃
//╰━━╯╰╯╰╯╰╯╰╯
#include <bits/stdc++.h>
using namespace std;
set <int> b;
int t;
long long int g(int n)
{
    if(b.find(n)!=b.end())
        return t;
    if(n<10)
        return n;
    else
    {
        int p=1,k;
        while(n!=0)
        {
            k=n%10;
            n/=10;
            if(k!=0)
                p*=k;
        }
        return g(p);
    }
}
int main()
{
    int q,l,s,r,t;
    int *a=new int [1000000];
    //for(int i=1;i<=1000000;i++)
    //    a[i-1]=g(i);
    cin >> q;
    for(int z=0;z<q;z++)
    {
        s=0;
        b.clear();
        cin >> l >> r >> t;
        for(int i=r;i>=l;i--)
            if(b.find(i)!=b.end() || g(i)==t)
            {
                b.insert(i);
                s++;
            }
        cout << s << endl;
    }
}


//╭╮╭╮╭━━╮╭━━╮╭╮╭╮
//┃┃┃┃┃╭╮┃┃╭━╯┃╰╯┃
//┃╰╯┃┃╰╯┃┃┃　　┃　╭╯
//┃╭╮┃┃╭╮┃┃┃　　┃　╰╮
//┃┃┃┃┃┃┃┃┃╰━╮┃╭╮┃
//╰╯╰╯╰╯╰╯╰━━╯╰╯╰╯
//╭╭╮╮╭━━╮　╭━━╮╭━━╮
//┃　　┃┃╭━╯　╰╮╭╯┃╭━╯
//┃┃┃┃┃╰━╮　　┃┃　┃╰━╮
//┃╭╮┃┃╭━╯　　┃┃　┃╭━╯
//┃┃┃┃┃╰━╮　╭╯╰╮┃┃
//╰╯╰╯╰━━╯  ╰━━╯╰╯
//　╭╮╭╮╭━━╮╭╮╭╮
//　┃╰╯┃┃╭╮┃┃┃┃┃
//　╰╮╭╯┃┃┃┃┃┃┃┃
//　　┃┃　┃┃┃┃┃┃┃┃
//　　┃┃　┃╰╯┃┃╰╯┃
//  　╰╯　╰━━╯╰━━╯
//╭━━╮╭━━╮╭╮╭╮
//┃╭━╯┃╭╮┃┃╰╮┃
//┃┃　　┃╰╯┃┃　　┃
//┃┃　　┃╭╮┃┃　　┃
//┃╰━╮┃┃┃┃┃╰╮┃
//╰━━╯╰╯╰╯╰╯╰╯
#include <bits/stdc++.h>
using namespace std;
long long int g(int n)
{
    if(n<10)
        return n;
    else
    {
        int p=1,k;
        while(n!=0)
        {
            k=n%10;
            n/=10;
            if(k!=0)
                p*=k;
        }
        return g(p);
    }
}
int main()
{
    int q,l,s,r,k;
    int *a=new int [1000000];
    for(int i=1;i<=1000000;i++)
        a[i-1]=g(i);
    cin >> q;
    for(int z=0;z<q;z++)
    {
        s=0;
        cin >> l >> r >> k;
        for(int i=l-1;i<r;i++)
            if(a[i]==k)
                s++;
        cout << s << endl;
    }
}


//╭╮╭╮╭━━╮╭━━╮╭╮╭╮
//┃┃┃┃┃╭╮┃┃╭━╯┃╰╯┃
//┃╰╯┃┃╰╯┃┃┃　　┃　╭╯
//┃╭╮┃┃╭╮┃┃┃　　┃　╰╮
//┃┃┃┃┃┃┃┃┃╰━╮┃╭╮┃
//╰╯╰╯╰╯╰╯╰━━╯╰╯╰╯
//╭╭╮╮╭━━╮　╭━━╮╭━━╮
//┃　　┃┃╭━╯　╰╮╭╯┃╭━╯
//┃┃┃┃┃╰━╮　　┃┃　┃╰━╮
//┃╭╮┃┃╭━╯　　┃┃　┃╭━╯
//┃┃┃┃┃╰━╮　╭╯╰╮┃┃
//╰╯╰╯╰━━╯  ╰━━╯╰╯
//　╭╮╭╮╭━━╮╭╮╭╮
//　┃╰╯┃┃╭╮┃┃┃┃┃
//　╰╮╭╯┃┃┃┃┃┃┃┃
//　　┃┃　┃┃┃┃┃┃┃┃
//　　┃┃　┃╰╯┃┃╰╯┃
//  　╰╯　╰━━╯╰━━╯
//╭━━╮╭━━╮╭╮╭╮
//┃╭━╯┃╭╮┃┃╰╮┃
//┃┃　　┃╰╯┃┃　　┃
//┃┃　　┃╭╮┃┃　　┃
//┃╰━╮┃┃┃┃┃╰╮┃
//╰━━╯╰╯╰╯╰╯╰╯
#include <bits/stdc++.h>
using namespace std;
long long int g(int n)
{
    if(n<10)
        return n;
    else
    {
        int p=1,k;
        while(n!=0)
        {
            k=n%10;
            n/=10;
            if(k!=0)
                p*=k;
        }
        return g(p);
    }
}
int main()
{
    int q,l,s,r,k;
    cin >> q;
    for(int z=0;z<q;z++)
    {
        s=0;
        cin >> l >> r >> k;
        for(int i=l;i<=r;i++)
            if(g(i)==k)
                s++;
        cout << s << endl;
    }
}


//╭╮╭╮╭━━╮╭━━╮╭╮╭╮
//┃┃┃┃┃╭╮┃┃╭━╯┃╰╯┃
//┃╰╯┃┃╰╯┃┃┃　　┃　╭╯
//┃╭╮┃┃╭╮┃┃┃　　┃　╰╮
//┃┃┃┃┃┃┃┃┃╰━╮┃╭╮┃
//╰╯╰╯╰╯╰╯╰━━╯╰╯╰╯
//╭╭╮╮╭━━╮　╭━━╮╭━━╮
//┃　　┃┃╭━╯　╰╮╭╯┃╭━╯
//┃┃┃┃┃╰━╮　　┃┃　┃╰━╮
//┃╭╮┃┃╭━╯　　┃┃　┃╭━╯
//┃┃┃┃┃╰━╮　╭╯╰╮┃┃
//╰╯╰╯╰━━╯  ╰━━╯╰╯
//　╭╮╭╮╭━━╮╭╮╭╮
//　┃╰╯┃┃╭╮┃┃┃┃┃
//　╰╮╭╯┃┃┃┃┃┃┃┃
//　　┃┃　┃┃┃┃┃┃┃┃
//　　┃┃　┃╰╯┃┃╰╯┃
//  　╰╯　╰━━╯╰━━╯
//╭━━╮╭━━╮╭╮╭╮
//┃╭━╯┃╭╮┃┃╰╮┃
//┃┃　　┃╰╯┃┃　　┃
//┃┃　　┃╭╮┃┃　　┃
//┃╰━╮┃┃┃┃┃╰╮┃
//╰━━╯╰╯╰╯╰╯╰╯
#include <bits/stdc++.h>
using namespace std;
set <int> b;
int t;
long long int g(int n)
{
    if(b.find(n)!=b.end())
        return t;
    if(n<10)
        return n;
    else
    {
        int p=1,k;
        while(n!=0)
        {
            k=n%10;
            n/=10;
            if(k!=0)
                p*=k;
        }
        return g(p);
    }
}
int main()
{
    int q,l,s,r,t;
    int *a=new int [1000000];
    for(int i=1;i<=1000000;i++)
        a[i-1]=g(i);
    cin >> q;
    for(int z=0;z<q;z++)
    {
        s=0;
        b.clear();
        cin >> l >> r >> t;
        for(int i=r;i>=l;i--)
            if(b.find(i)!=b.end() || a[i-1]==t)
            {
                b.insert(i);
                s++;
            }
        cout << s << endl;
    }
}


//╭╮╭╮╭━━╮╭━━╮╭╮╭╮
//┃┃┃┃┃╭╮┃┃╭━╯┃╰╯┃
//┃╰╯┃┃╰╯┃┃┃　　┃　╭╯
//┃╭╮┃┃╭╮┃┃┃　　┃　╰╮
//┃┃┃┃┃┃┃┃┃╰━╮┃╭╮┃
//╰╯╰╯╰╯╰╯╰━━╯╰╯╰╯
//╭╭╮╮╭━━╮　╭━━╮╭━━╮
//┃　　┃┃╭━╯　╰╮╭╯┃╭━╯
//┃┃┃┃┃╰━╮　　┃┃　┃╰━╮
//┃╭╮┃┃╭━╯　　┃┃　┃╭━╯
//┃┃┃┃┃╰━╮　╭╯╰╮┃┃
//╰╯╰╯╰━━╯  ╰━━╯╰╯
//　╭╮╭╮╭━━╮╭╮╭╮
//　┃╰╯┃┃╭╮┃┃┃┃┃
//　╰╮╭╯┃┃┃┃┃┃┃┃
//　　┃┃　┃┃┃┃┃┃┃┃
//　　┃┃　┃╰╯┃┃╰╯┃
//  　╰╯　╰━━╯╰━━╯
//╭━━╮╭━━╮╭╮╭╮
//┃╭━╯┃╭╮┃┃╰╮┃
//┃┃　　┃╰╯┃┃　　┃
//┃┃　　┃╭╮┃┃　　┃
//┃╰━╮┃┃┃┃┃╰╮┃
//╰━━╯╰╯╰╯╰╯╰╯

#include <bits/stdc++.h>
using namespace std;

int  s[1000005][10], t[1000005],q,l,r,k,p;

int g(int n)
{
    int pr=1;
    while(n>0)
    {
        if(n%10>0)
            pr*=(n%10);
        n/=10;
    }
    return pr;
}
int main()
{
    for(int i=0;i<10;i++) t[i] = i;
    for(int i=10;i<=1000000;i++) t[i]=t[g(i)];
    for(int i=1;i<=1000000;i++)
    {
        for(int j=1;j<10;j++)
            s[i][j]= s[i-1][j];
        s[i][t[i]]++;
    }
    cin >> q;
    for(int i=0;i<q;i++)
    {
        cin >> l >> r >> k;
        cout << s[r][k]-s[l-1][k] << endl;
    }
}


//╭╮╭╮╭━━╮╭━━╮╭╮╭╮
//┃┃┃┃┃╭╮┃┃╭━╯┃╰╯┃
//┃╰╯┃┃╰╯┃┃┃　　┃　╭╯
//┃╭╮┃┃╭╮┃┃┃　　┃　╰╮
//┃┃┃┃┃┃┃┃┃╰━╮┃╭╮┃
//╰╯╰╯╰╯╰╯╰━━╯╰╯╰╯
//╭╭╮╮╭━━╮　╭━━╮╭━━╮
//┃　　┃┃╭━╯　╰╮╭╯┃╭━╯
//┃┃┃┃┃╰━╮　　┃┃　┃╰━╮
//┃╭╮┃┃╭━╯　　┃┃　┃╭━╯
//┃┃┃┃┃╰━╮　╭╯╰╮┃┃
//╰╯╰╯╰━━╯  ╰━━╯╰╯
//　╭╮╭╮╭━━╮╭╮╭╮
//　┃╰╯┃┃╭╮┃┃┃┃┃
//　╰╮╭╯┃┃┃┃┃┃┃┃
//　　┃┃　┃┃┃┃┃┃┃┃
//　　┃┃　┃╰╯┃┃╰╯┃
//  　╰╯　╰━━╯╰━━╯
//╭━━╮╭━━╮╭╮╭╮
//┃╭━╯┃╭╮┃┃╰╮┃
//┃┃　　┃╰╯┃┃　　┃
//┃┃　　┃╭╮┃┃　　┃
//┃╰━╮┃┃┃┃┃╰╮┃
//╰━━╯╰╯╰╯╰╯╰╯
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int q,l,s,r,t,n,p,temp;
    int *a=new int [1000000];
    //for(int i=1;i<=1000000;i++)
    //    a[i-1]=g(i);
    cin >> q;
    for(int z=0;z<q;z++)
    {
        s=0;
        cin >> l >> r >> t;
        for(int i=r;i>=l;i--)
        {
            n=i; p=1;
            while(n>0)
            {
                temp=n%10;
                if(temp!=0)
                    p*=temp;
                n/=10;
                if(n==0)
                {
                    if(p>9)
                    {
                        n=p;
                        p=1;
                    }
                    else
                        break;
                }
            }
            if(p==t)
            {
                s++;
            }
        }
        cout << s << endl;
    }
}


//╭╮╭╮╭━━╮╭━━╮╭╮╭╮
//┃┃┃┃┃╭╮┃┃╭━╯┃╰╯┃
//┃╰╯┃┃╰╯┃┃┃　　┃　╭╯
//┃╭╮┃┃╭╮┃┃┃　　┃　╰╮
//┃┃┃┃┃┃┃┃┃╰━╮┃╭╮┃
//╰╯╰╯╰╯╰╯╰━━╯╰╯╰╯
//╭╭╮╮╭━━╮　╭━━╮╭━━╮
//┃　　┃┃╭━╯　╰╮╭╯┃╭━╯
//┃┃┃┃┃╰━╮　　┃┃　┃╰━╮
//┃╭╮┃┃╭━╯　　┃┃　┃╭━╯
//┃┃┃┃┃╰━╮　╭╯╰╮┃┃
//╰╯╰╯╰━━╯  ╰━━╯╰╯
//　╭╮╭╮╭━━╮╭╮╭╮
//　┃╰╯┃┃╭╮┃┃┃┃┃
//　╰╮╭╯┃┃┃┃┃┃┃┃
//　　┃┃　┃┃┃┃┃┃┃┃
//　　┃┃　┃╰╯┃┃╰╯┃
//  　╰╯　╰━━╯╰━━╯
//╭━━╮╭━━╮╭╮╭╮
//┃╭━╯┃╭╮┃┃╰╮┃
//┃┃　　┃╰╯┃┃　　┃
//┃┃　　┃╭╮┃┃　　┃
//┃╰━╮┃┃┃┃┃╰╮┃
//╰━━╯╰╯╰╯╰╯╰╯
#include <bits/stdc++.h>
using namespace std;
set <int> b;
long long int g(int n)
{
    if(n<10)
        return n;
    else
    {
        int p=1,k;
        while(n!=0)
        {
            k=n%10;
            n/=10;
            if(k!=0)
                p*=k;
        }
        b.insert(p);
        return g(p);
    }
}
int main()
{
    int q,l,s,r,k;
    int *a=new int [1000000];
    for(int i=1;i<=1000000;i++)
        a[i-1]=g(i);
    cin >> q;
    for(int z=0;z<q;z++)
    {
        s=0;
        b.clear();
        cin >> l >> r >> k;
        for(int i=l;i<=r;i++)
            if(b.find(i)!=b.end() || a[i-1]==k)
            {
                s++;
            }
        cout << s << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=1e6;

ll f(ll x){
  ll prod=1;
  while(x>0){
    if(x%10!=0){prod*=x%10;}
    x/=10;
  }
  return prod;
}
ll g(ll x){
  if(x<=9)return x;
  return g(f(x));
}
 ll i,j,k,n,q,count1[10][N],p,l,r;
int main(){
 ios_base::sync_with_stdio(false);
  for(i=1;i<=N;i++){
    for(j=1;j<=9;j++){count1[j][i]=count1[j][i-1];}
    count1[g(i)][i]=1+count1[g(i)][i-1];
  }
  cin>>q;
  while(q--){
    cin>>l>>r>>k;
    if(n==87738&&q==29265){cout<<l<<r<<k<<"\n";}
    cout<<count1[k][r]-count1[k][l]+(k==g(l))<<"\n";
  }
  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=1e6;

ll f(ll x){
  ll prod=1;
  while(x>0){if(x%10!=0){prod*=x%10;}x/=10;}
  return prod;
}
int g(ll x){
  if(x<=9)return x;
  return g(f(x));
}
 ll i,j,k,n,q,count1[10][N],p,l,r;
int main(){

  for(i=1;i<=N;i++){
    for(j=1;j<=9;j++){count1[j][i]=count1[j][i-1];}
    count1[g(i)][i]=1+count1[g(i)][i-1];
  }
  //for(i=1;i<=101;i++)cout<<i<<" "<<count1[1][i]<<"\n";
  cin>>q;
  while(q--){
    cin>>l>>r>>k;
    bool p=(k==g(l));
    cout<<count1[k][r]-count1[k][l]+p<<"\n";
  }
  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=1e6;

ll f(ll x){
  ll prod=1;
  while(x>0){if(x%10!=0){prod*=x%10;}x/=10;}
  return prod;
}
int g(ll x){
  if(x<=9)return x;
  return g(f(x));
}
 ll i,j,k,n,q,count1[10][N],p,l,r;
int main(){
 ios_base::sync_with_stdio(false);
  for(i=1;i<=N;i++){
    for(j=1;j<=9;j++){count1[j][i]=count1[j][i-1];}
    count1[g(i)][i]=1+count1[g(i)][i-1];
  }
  //for(i=1;i<=101;i++)cout<<i<<" "<<count1[9][i]<<"\n";
  cin>>q;
  while(q--){
    cin>>l>>r>>k;
    bool b=(k==g(l));
    if(l>r)swap(l,r);
    cout<<count1[k][r]-count1[k][l]+b<<"\n";
  }
  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=1e6;

ll f(ll x){
  ll prod=1;
  while(x>0){
    if(x%10!=0){prod*=x%10;}
    x/=10;
  }
  return prod;
}
ll g(ll x){
  if(x<=9)return x;
  return g(f(x));
}
 ll i,j,k,n,q,count1[10][N],p,l,r;
int main(){
 ios_base::sync_with_stdio(false);
  for(i=1;i<=N;i++){
    for(j=1;j<=9;j++){count1[j][i]=count1[j][i-1];}
    count1[g(i)][i]=1+count1[g(i)][i-1];
  }
  cin>>q;
  while(q--){
    cin>>l>>r>>k;
    if(count1[k][r]-count1[k][l]+(k==g(l))==10096){cout<<l<<"p"<<r<<"p"<<k<<"\n";}
    cout<<count1[k][r]-count1[k][l]+(k==g(l))<<"\n";
  }
  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=1e6;

ll f(ll x){
  ll prod=1;
  while(x>0){
    if(x%10!=0){prod*=x%10;}
    x/=10;
  }
  return prod;
}
ll g(ll x){
  if(x<=9)return x;
  return g(f(x));
}
 ll i,j,k,n,q,count1[10][N],p,l,r;
int main(){
 ios_base::sync_with_stdio(false);
  for(i=1;i<=N;i++){
    for(j=1;j<=9;j++){count1[j][i]=count1[j][i-1];}
    count1[g(i)][i]=1+count1[g(i)][i-1];
  }
  cin>>q;
  while(q--){
    cin>>l>>r>>k;
    if(n==87738&&q==29265){cout<<l<<"p"<<r<<"p"<<k<<"\n";}
    cout<<count1[k][r]-count1[k][l]+(k==g(l))<<"\n";
  }
  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=1e6;

ll f(ll x){
  ll prod=1;
  while(x>0){
    if(x%10!=0){prod*=x%10;}
    x/=10;
  }
  return prod;
}
ll g(ll x){
  if(x<=9)return x;
  return g(f(x));
}
 ll i,j,k,n,q,count1[10][N],p,l,r;
int main(){
 ios_base::sync_with_stdio(false);
  for(i=1;i<=N;i++){
    for(j=1;j<=9;j++){count1[j][i]=count1[j][i-1];}
    count1[g(i)][i]=1+count1[g(i)][i-1];
  }
  cin>>q;
  n=q;
  while(q--){
    cin>>l>>r>>k;
    if(q==29624&&n==87738){cout<<l<<"p"<<r<<"p"<<k<<"\n";}
    cout<<count1[k][r]-count1[k][l]+(k==g(l))<<"\n";
  }
  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=1e6;

ll f(ll x){
  ll prod=1;
  while(x>0){
    if(x%10!=0){prod*=x%10;}
    x/=10;
  }
  return prod;
}
ll g(ll x){
  if(x<=9)return x;
  return g(f(x));
}
 ll i,j,k,n,q,count1[10][N],p,l,r;
int main(){
 ios_base::sync_with_stdio(false);
  for(i=1;i<=N;i++){
    for(j=1;j<=9;j++){count1[j][i]=count1[j][i-1];}
    count1[g(i)][i]=1+count1[g(i)][i-1];
  }
  cin>>q;
  n=q;
  while(q--){
    cin>>l>>r>>k;
    if(q==29625&&n==87738){cout<<l<<"p"<<r<<"p"<<k<<"\n";}
    cout<<count1[k][r]-count1[k][l]+(k==g(l))<<"\n";
  }
  return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N=1e6+5;

ll f(ll x){
  ll prod=1;
  while(x>0){
    if(x%10!=0){prod*=x%10;}
    x/=10;
  }
  return prod;
}
ll g(ll x){
  if(x<=9)return x;
  return g(f(x));
}
 ll i,j,k,n,q,count1[10][N],p,l,r;
int main(){
 ios_base::sync_with_stdio(false);
  for(i=1;i<=N;i++){
    for(j=1;j<=9;j++){count1[j][i]=count1[j][i-1];}
    count1[g(i)][i]=1+count1[g(i)][i-1];
  }
  cin>>q;
  n=q;
  while(q--){
    cin>>l>>r>>k;
    cout<<count1[k][r]-count1[k][l]+(k==g(l))<<"\n";
  }
  return 0;

}


#include<iostream>
#include<string>
#include<sstream>
#include<map>
using namespace std;
int q , l , r, k;
map<pair<int ,int > , int>vis;
int calc(int x){
	int ok=x;
	while(true){
		string s;
		ostringstream z;
		z << x;
		s=z.str();
		int pro=1;
		for(int i=0 ; i<s.size() ; i++)pro*=((s[i]!='0')? s[i]-'0' : 1);
		if(s.size()==1){
			vis[make_pair(k , ok)]=(pro==k)+1;
			return pro==k;
		}
		x=pro;
	}
}
int main (){

	scanf("%d" , &q);
	for(int i=0 ; i<q ; i++){
		scanf("%d%d%d" , &l , &r , &k);
		int cnt=0;
		for(int i2=l ; i2<=r ; i2++){
			if(!vis[make_pair(k  , i2)])cnt+=calc(i2);
			else cnt+=vis[make_pair(k ,i2)]-1;
		}
		printf("%d\n" , cnt);
	}
	return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
const int N=1e6+1;
int q , l , r, k, dp[10][N] , ans[N];
int calc(int u){
	if(u<10)return u;
	int &cnt=ans[u];
	if(~cnt)return cnt;
	int pro=1;
	while(u){
		int y=u%10;
		if(y)pro*=y;
		u/=10;
	}
	return cnt=calc(pro);
}
int main (){

	memset(ans , -1 , sizeof ans);
	for(int i=1 ;i<=N; i++)dp[calc(i)][i]++;
	for(int i=1 ;i<=9 ; i++)
	     for(int i2=1 ; i2<=N ; i2++)dp[i][i2]+=dp[i][i2-1];
	scanf("%d" , &q);
	for(int i=0 ; i<q ; i++){
		scanf("%d%d%d" , &l , &r , &k);
		printf("%d\n", dp[k][r]-dp[k][l-1]);
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
const int N=2e5+1;
int q , l[N]  , r[N] , k[N];
vector<int>rs[10], lol[10];
map<pair<int ,int > , bool>vis;
bool vis2[10];
void build(int x , int y){
	for(int i=21 ; i<=x ; i++){
		string s;
		ostringstream z;
		z << i;
		s=z.str();
		int sum=1;
		for(int i2=0; i2<s.size() ; i2++)
		     if(s[i2]!='0')sum*=(s[i2]-'0');
		if(sum==y||vis[make_pair(y, sum)]){lol[y].push_back(i);vis[make_pair(y , i)]=1;}
	}
}
int main (){

	scanf("%d" , &q);
	for(int i=11 ; i<=19 ; i++){lol[i-10].push_back(i);vis[make_pair((i-10) , i)]=1;vis[make_pair((i-10),(i-10))]=1;}
	for(int i=0; i<q ; i++){
		scanf("%d%d%d" , &l[i] , &r[i] , &k[i]);
		rs[k[i]].push_back(r[i]);vis2[k[i]]=1;
	}
	for(int i=1 ; i<=9 ; i++){
		if(vis2[i]){
			sort(rs[i].begin() , rs[i].end());
			build(rs[i][rs[i].size()-1], i);
		}
	}
	for(int i=0 ;i<q ; i++){
		int cnt=0;
		for(int i2=l[i] ; i2<=r[i] ; i2++)cnt+=((vis[make_pair(k[i], i2)]==1) ? 1 : 0);
		printf("%d\n", cnt);
	}
	return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
const int N=1e6+1;
int dp[10][N] , ans[N];
int calc(int u){
	if(u<10)return u;
	int &cnt=ans[u];
	if(cnt)return u;
	int pro=1;
	while(u){
		int y=u%10;
		if(u)pro*=y;
		u/=10;
	}
	return cnt=calc(pro);

}
int main (){

	int q , l , r , k;
	for(int i=1 ; i<=N+1 ; i++){dp[calc(i)][i]++;}
	for(int i=1 ; i<=9 ;i++)
	    for(int i2=2 ; i2<=N ; i2++)dp[i][i2]+=dp[i][i2-1];
	scanf("%d" , &q);
	for(int i=1 ; i<=q ; i++){
		scanf("%d%d%d" , &l , &r , &k);
		printf("%d\n", dp[k][r]-dp[k][l-1]);
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
const int N=2e5+1;
int q , l[N]  , r[N] , k[N];
vector<int>rs[10], lol[10];
map<pair<int ,int > , bool>vis;
bool vis2[10];
void build(int x , int y){
	for(int i=20 ; i<=x ; i++){
		string s;
		ostringstream z;
		z << i;
		s=z.str();
		int sum=1;
		for(int i2=0; i2<s.size() ; i2++)
		     if(s[i2]!='0')sum*=(s[i2]-'0');
		if(sum==y||vis[make_pair(y, sum)]){lol[y].push_back(i);vis[make_pair(y , i)]=1;}
	}
}
int main (){

	scanf("%d" , &q);
	vis[make_pair(1 , 10)]=1;
	for(int i=11 ; i<=19 ; i++){lol[i-10].push_back(i);vis[make_pair((i-10) , i)]=1;vis[make_pair((i-10),(i-10))]=1;}
	for(int i=0; i<q ; i++){
		scanf("%d%d%d" , &l[i] , &r[i] , &k[i]);
		rs[k[i]].push_back(r[i]);vis2[k[i]]=1;
	}
	for(int i=1 ; i<=9 ; i++){
		if(vis2[i]){
			sort(rs[i].begin() , rs[i].end());
			build(rs[i][rs[i].size()-1], i);
		}
	}
	for(int i=0 ;i<q ; i++){
		int cnt=0;
		for(int i2=l[i] ; i2<=r[i] ; i2++)cnt+=((vis[make_pair(k[i], i2)]==1) ? 1 : 0);
		printf("%d\n", cnt);
	}
	return 0;
}


#include<iostream>
#include<string>
#include<sstream>
#include<map>
#include<vector>
using namespace std;
int q , l , r, k;
map<pair<int ,int > , int>vis;
int calc(int x){
	int ok=x;
	vector<int>lol;
	while(true){
		string s;
		ostringstream z;
		z << x;
		s=z.str();
		int pro=1;
		for(int i=0 ; i<s.size() ; i++)pro*=((s[i]!='0')? s[i]-'0' : 1);
		if(s.size()==1){
			vis[make_pair(k , ok)]=(pro==k)+1;
			if((pro==k)){
				for(int i=0; i<lol.size() ; i++)vis[make_pair(k ,lol[i])]=2;
			}
			return pro==k;
		}
		x=pro;
		lol.push_back(pro);
	}
}
int main (){

	scanf("%d" , &q);
	for(int i=0 ; i<q ; i++){
		scanf("%d%d%d" , &l , &r , &k);
		int cnt=0;
		for(int i2=l ; i2<=r ; i2++){
			if(!vis[make_pair(k  , i2)])cnt+=calc(i2);
			else cnt+=vis[make_pair(k ,i2)]-1;
		}
		printf("%d\n" , cnt);
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
const int N=2e5+1;
int q , l[N]  , r[N] , k[N];
vector<int>rs[10], lol[10];
map<pair<int ,int > , bool>vis;
bool vis2[10];
void build(int x , int y){
	for(int i=21 ; i<=x ; i++){
		string s;
		ostringstream z;
		z << i;
		s=z.str();
		int sum=1;
		for(int i2=0; i2<s.size() ; i2++)
		     if(s[i2]!='0')sum*=(s[i2]-'0');
		if(sum==y||vis[make_pair(y, sum)]){lol[y].push_back(i);vis[make_pair(y , i)]=1;}
	}
}
int main (){

	scanf("%d" , &q);
	vis[make_pair(1 , 10)]=1;
	for(int i=11 ; i<=19 ; i++){lol[i-10].push_back(i);vis[make_pair((i-10) , i)]=1;vis[make_pair((i-10),(i-10))]=1;}
	for(int i=0; i<q ; i++){
		scanf("%d%d%d" , &l[i] , &r[i] , &k[i]);
		rs[k[i]].push_back(r[i]);vis2[k[i]]=1;
	}
	for(int i=1 ; i<=9 ; i++){
		if(vis2[i]){
			sort(rs[i].begin() , rs[i].end());
			build(rs[i][rs[i].size()-1], i);
		}
	}
	for(int i=0 ;i<q ; i++){
		int cnt=0;
		for(int i2=l[i] ; i2<=r[i] ; i2++)cnt+=((vis[make_pair(k[i], i2)]==1) ? 1 : 0);
		printf("%d\n", cnt);
	}
	return 0;
}


#include<iostream>
#include<string>
#include<sstream>
#include<map>
using namespace std;
int q , l , r, k;
int calc(int x){
	while(true){
		string s;
		ostringstream z;
		z << x;
		s=z.str();
		int pro=1;
		for(int i=0 ; i<s.size() ; i++)pro*=((s[i]!='0')? s[i]-'0' : 1);
		if(s.size()==1)return pro==k;
		x=pro;
	}
}
int main (){

	scanf("%d" , &q);
	for(int i=0 ; i<q ; i++){
		scanf("%d%d%d" , &l , &r , &k);
		int cnt=0;
		for(int i2=l ; i2<=r ; i2++)cnt+=calc(i2);
		printf("%d\n" , cnt);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <functional>
#include <algorithm>
#include <utility>
#include <unordered_map>
#include <iterator>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <stack>

#define ll long long
#define ui unsigned ll
#define MP make_pair
#define timer fghge
#define y1 yjyjyju

#define all(X) (X).begin(), (X).end()
using namespace std;
#define _CRT_SECURE_NO_WARNINGS

using namespace std;

const int INF = 1e9;

vector<int> dp(1000000, INF);

int f(int n)
{
	if (dp[n] != INF)
		return dp[n];
	else
	{
		int d = 10, i = n;
		dp[i] = 1;
		while (n != 0)
		{
			if (n % d != 0)
				dp[i] *= n % d;
			n /= d;
			d *= 10;
		}
		return dp[i];
	}
}

int g(int n)
{
	if (n < 10)
		return n;
	else
		return g(f(n));
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t;
	vector<int> a(10);
	vector<vector<int>> pf(10, vector<int>(1000000));

	for (int i = 1; i < 1000000; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (g(i) == j)
				pf[j][i] = ++a[j];
			else
				pf[j][i] = a[j];
		}
	}

	while (t--)
	{
		int l, r, k, ans = 0; cin >> l >> r >> k;
		cout << pf[k][r] - pf[k][l-1] << endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <functional>
#include <algorithm>
#include <utility>
#include <unordered_map>
#include <iterator>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <stack>

#define ll long long
#define ui unsigned ll
#define MP make_pair
#define timer fghge
#define y1 yjyjyju

#define all(X) (X).begin(), (X).end()
using namespace std;
#define _CRT_SECURE_NO_WARNINGS

using namespace std;

const int INF = 1e9;

vector<int> dp(1000000, INF);

int f(int n)
{
	int d = 10, i = n;
	dp[i] = 1;
	while (n != 0)
	{
		if (n % d != 0)
			dp[i] *= n % d;
		n /= d;
		d *= 10;
	}
	return dp[i];
}

int g(int n)
{
	if (n < 10)
		return n;
	else
	{
		dp[n] = dp[f(n)];
		return dp[n];
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t;
	vector<int> a(10);
	vector<vector<int>> pf(10, vector<int>(1000000));

	for (int i = 1; i < 1000000; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (g(i) == j)
				pf[j][i] = ++a[j];
			else
				pf[j][i] = a[j];
		}
	}

	while (t--)
	{
		int l, r, k, ans = 0; cin >> l >> r >> k;
		cout << pf[k][r] - pf[k][l-1] << endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <functional>
#include <algorithm>
#include <utility>
#include <unordered_map>
#include <iterator>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <stack>

#define ll long long
#define ui unsigned ll
#define MP make_pair
#define timer fghge
#define y1 yjyjyju

#define all(X) (X).begin(), (X).end()
using namespace std;
#define _CRT_SECURE_NO_WARNINGS

using namespace std;

const int INF = 1e9;

vector<int> dp(1000001, INF);

int f(int n)
{
	if (n < 10)
		return n;
	else
	{
		int d = 10, i = n;
		int ans = 1;
		while (n != 0)
		{
			if (n % d != 0)
				ans *= n % d;
			n /= d;
		}
		return ans;
	}
}

int g(int n)
{
	if (n < 10)
		return n;
	else
	{
		dp[n] = dp[f(n)];
		return dp[n];
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t;
	vector<int> a(10);
	vector<vector<int>> pf(10, vector<int>(1000001));

	for (int i = 1; i < 10; i++)
		dp[i] = i;

	for (int i = 1; i < 1000001; i++)
	{
		int d = g(i);
		for (int j = 0; j < 10; j++)
		{
			if (d == j)
				pf[j][i] = ++a[j];
			else
				pf[j][i] = a[j];
		}
	}

	while (t--)
	{
		int l, r, k, ans = 0; cin >> l >> r >> k;
		cout << pf[k][r] - pf[k][l-1] << endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <functional>
#include <algorithm>
#include <utility>
#include <unordered_map>
#include <iterator>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <stack>

#define ll long long
#define ui unsigned ll
#define MP make_pair
#define timer fghge
#define y1 yjyjyju

#define all(X) (X).begin(), (X).end()
using namespace std;
#define _CRT_SECURE_NO_WARNINGS

using namespace std;

const int INF = 1e9;

vector<int> dp(1000000, INF);

int f(int n)
{
	int d = 10, i = n;
	int ans = 1;
	while (n != 0)
	{
		if (n % d != 0)
			ans *= n % d;
		n /= d;
		d *= 10;
	}
	return ans;
}

int g(int n)
{
	if (n < 10)
		return n;
	else
	{
		dp[n] = dp[f(n)];
		return dp[n];
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t;
	vector<int> a(10);
	vector<vector<int>> pf(10, vector<int>(1000000));

	for (int i = 1; i < 1000000; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (g(i) == j)
				pf[j][i] = ++a[j];
			else
				pf[j][i] = a[j];
		}
	}

	while (t--)
	{
		int l, r, k, ans = 0; cin >> l >> r >> k;
		cout << pf[k][r] - pf[k][l-1] << endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <functional>
#include <algorithm>
#include <utility>
#include <unordered_map>
#include <iterator>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <stack>

#define ll long long
#define ui unsigned ll
#define MP make_pair
#define timer fghge
#define y1 yjyjyju

#define all(X) (X).begin(), (X).end()
using namespace std;
#define _CRT_SECURE_NO_WARNINGS

using namespace std;

const int INF = 1e9;

vector<int> dp(1000000, INF);

int f(int n)
{
	if (n < 10)
		return n;
	else
	{
		int d = 10, i = n;
		int ans = 1;
		while (n != 0)
		{
			if (n % d != 0)
				ans *= n % d;
			n /= d;
			d *= 10;
		}
		return ans;
	}
}

int g(int n)
{
	if (n < 10)
		return n;
	else
	{
		dp[n] = dp[f(n)];
		return dp[n];
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t;
	vector<int> a(10);
	vector<vector<int>> pf(10, vector<int>(1000000));
	for (int i = 1; i < 10; i++)
		dp[i] = i;

	for (int i = 10; i < 1000000; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (g(i) == j)
				pf[j][i] = ++a[j];
			else
				pf[j][i] = a[j];
		}
	}

	while (t--)
	{
		int l, r, k, ans = 0; cin >> l >> r >> k;
		cout << pf[k][r] - pf[k][l-1] << endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <functional>
#include <algorithm>
#include <utility>
#include <unordered_map>
#include <iterator>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <stack>

#define ll long long
#define ui unsigned ll
#define MP make_pair
#define timer fghge
#define y1 yjyjyju

#define all(X) (X).begin(), (X).end()
using namespace std;
#define _CRT_SECURE_NO_WARNINGS

using namespace std;

const int INF = 1e9;

vector<int> dp(1000000, INF);

int f(int n)
{
	if (dp[n] != INF)
		return dp[n];
	else
	{
		int d = 10, i = n;
		dp[i] = 1;
		while (n != 0)
		{
			if (n % d != 0)
				dp[i] *= n % d;
			n /= d;
			d *= 10;
		}
		return dp[i];
	}
}

int g(int n)
{
	if (n < 10)
		return n;
	else
		return g(f(n));
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t;
	vector<int> a(10);
	vector<vector<int>> pf(10, vector<int>(1000000));

	for (int i = 1; i < 100000; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (g(i) == j)
				pf[j][i] = ++a[j];
			else
				pf[j][i] = a[j];
		}
	}

	while (t--)
	{
		int l, r, k, ans = 0; cin >> l >> r >> k;
		cout << pf[k][r] - pf[k][l-1] << endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <functional>
#include <algorithm>
#include <utility>
#include <unordered_map>
#include <iterator>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <stack>

#define ll long long
#define ui unsigned ll
#define MP make_pair
#define timer fghge
#define y1 yjyjyju

#define all(X) (X).begin(), (X).end()
using namespace std;
#define _CRT_SECURE_NO_WARNINGS

using namespace std;

const int INF = 1e9;

vector<int> dp(1000000, INF);

int f(int n)
{
	if (n < 10)
		return n;
	else
	{
		int d = 10, i = n;
		int ans = 1;
		while (n != 0)
		{
			if (n % d != 0)
				ans *= n % d;
			n /= d;
		}
		return ans;
	}
}

int g(int n)
{
	if (n < 10)
		return n;
	else
	{
		dp[n] = dp[f(n)];
		return dp[n];
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t;
	vector<int> a(10);
	vector<vector<int>> pf(10, vector<int>(1000000));

	for (int i = 1; i < 10; i++)
		dp[i] = i;

	for (int i = 1; i < 1000000; i++)
	{
		int d = g(i);
		for (int j = 0; j < 10; j++)
		{
			if (d == j)
				pf[j][i] = ++a[j];
			else
				pf[j][i] = a[j];
		}
	}

	while (t--)
	{
		int l, r, k, ans = 0; cin >> l >> r >> k;
		cout << pf[k][r] - pf[k][l-1] << endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <functional>
#include <algorithm>
#include <utility>
#include <unordered_map>
#include <iterator>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <stack>

#define ll long long
#define ui unsigned ll
#define MP make_pair
#define timer fghge
#define y1 yjyjyju

#define all(X) (X).begin(), (X).end()
using namespace std;
#define _CRT_SECURE_NO_WARNINGS

using namespace std;

const int INF = 1e9;

int f(int n)
{
	string s = to_string(n);
	int ans = 1;
	for (auto i : s)
	{
		if (i != '0')
			ans *= i - '0';
	}
	return ans;
}

int g(int n)
{
	if (n < 10)
		return n;
	else
		return g(f(n));
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	//freopen("input.txt", "r", stdin);
	int t; cin >> t;
	while (t--)
	{
		int l, r, k, ans = 0; cin >> l >> r >> k;
		for (int i = l; i <= r; i++)
		{
			if (g(i) == k)
				ans++;
		}
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long int multiple_dp(long long int n)
{
	long long int x=1;

	while(n>9)
	{
		if(n%10==0)
		{
			n/=10;
			continue;
		}
		else
		{
			x=x*(n%10);
			n/=10;
		}
	}
	return n*x;
}

int main(){
	long long int prod[1000001];
	long long int dp[10][1000001];

	prod[0]=0;
	prod[1]=1;
	prod[2]=2;
	prod[3]=3;
	prod[4]=4;
	prod[5]=5;
	prod[6]=6;
	prod[7]=7;
	prod[8]=8;
	prod[9]=9;

	for(long long int i=10;i<=1000000;i++)
	{
		prod[i]=prod[multiple_dp(i)];
	}

 /*  for(long long int i=1;i<10;i++)
	{
		dp[i][i]=1;
	}

	for(long long int i=10;i<=1000009;i++)
	{
		long long int d=prod[i];
		dp[d][i]=1;
	}*/

for(long long int j=1;j<10;j++)
{
	for(long long int i=1;i<=1000000;i++)
	{
		if(prod[i]==j)
		{
			dp[j][i]=1;
		}
		dp[j][i]+=dp[j][i-1];
	}
}

long long int q;
cin>>q;
while(q--)
{
	long long int l,r,k;
	cin>>l>>r>>k;

	cout<<dp[k][r]-dp[k][l-1]<<endl;
}

return 0;
}


#include <bits/stdc++.h>
using namespace std;

int dp[10][1000001];

int modonmora(int p){
	if(p<10)
	return p;
	int product=1;
	while(p){
		if(p%10)
		    product*=(p%10);
		p/=10;
	}
	return modonmora(product);
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	for(int i=1;i<=1000000;i++){
		dp[modonmora(i)][i]++;
	}
	for(int i=1;i<10;i++){
		for(int j=1;j<=1000000;j++){
			dp[i][j]+=dp[i][j-1];
		}
	}
	int t;
	cin>>t;
	while(t--){
		int l,r,k;
		cin>>l>>r>>k;
		cout<<dp[k][r]-dp[k][l-1]<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int modonmora(int p){
	if(p<10)
	return p;
	int product=1;
	while(p){
		if(p%10)
		    product*=(p%10);
		p/=10;
	}
	return modonmora(product);
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int dp[10][1000001];

	for(int i=1;i<=1000000;i++){
		dp[modonmora(i)][i]++;
	}
	for(int i=1;i<10;i++){
		for(int j=1;j<=1000000;j++){
			dp[i][j]+=dp[i][j-1];
		}
	}
	int t;
	cin>>t;
	while(t--){
		int l,r,k;
		cin>>l>>r>>k;
		cout<<dp[k][r]-dp[k][l-1]<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long int multiple_dp(long long int n)
{
	long long int x=1;

	while(n>9)
	{
		if(n%10==0)
		{
			n/=10;
			continue;
		}
		else
		{
			x=x*(n%10);
			n/=10;
		}
	}
	return n*x;
}

int main(){
	long long int prod[10000010];
	long long int dp[10][10000010];

	prod[0]=0;
	prod[1]=1;
	prod[2]=2;
	prod[3]=3;
	prod[4]=4;
	prod[5]=5;
	prod[6]=6;
	prod[7]=7;
	prod[8]=8;
	prod[9]=9;

	for(long long int i=10;i<=1000009;i++)
	{
		prod[i]=prod[multiple_dp(i)];
	}

 /*  for(long long int i=1;i<10;i++)
	{
		dp[i][i]=1;
	}

	for(long long int i=10;i<=1000009;i++)
	{
		long long int d=prod[i];
		dp[d][i]=1;
	}*/

for(long long int j=1;j<10;j++)
{
	for(long long int i=1;i<=1000009;i++)
	{
		if(prod[i]==j)
		{
			dp[j][i]=1;
		}
		dp[j][i]+=dp[j][i-1];
	}
}

long long int q;
cin>>q;
while(q--)
{
	long long int l,r,k;
	cin>>l>>r>>k;

	cout<<dp[k][r]-dp[k][l-1]<<endl;
}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long int prod[1000002];
long long int dp[10][1000002];

long long int multiple_dp(long long int n)
{
	long long int x=1;

	while(n>9)
	{
		if(n%10==0)
		{
			n/=10;
			continue;
		}
		else
		{
			x=x*(n%10);
			n/=10;
		}
	}
	return n*x;
}

int main(){

	prod[0]=0;
	prod[1]=1;
	prod[2]=2;
	prod[3]=3;
	prod[4]=4;
	prod[5]=5;
	prod[6]=6;
	prod[7]=7;
	prod[8]=8;
	prod[9]=9;

	for(long long int i=10;i<=1000001;i++)
	{
		prod[i]=prod[multiple_dp(i)];
	}

	for(long long int i=1;i<10;i++)
	{
		dp[i][i]=1;
	}

	for(long long int i=10;i<=1000001;i++)
	{
		long long int d=prod[i];
		dp[d][i]=1;
	}

for(long long int j=1;j<=9;j++)
{
	for(long long int i=1;i<=1000001;i++)
	{
		dp[j][i]=dp[j][i]+dp[j][i-1];
	}
}

long long int q;
cin>>q;
while(q--)
{
	long long int l,r,k;
	cin>>l>>r>>k;

	cout<<dp[k][r]-dp[k][l-1]<<endl;
}

return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long int multiple_dp(long long int n)
{
	long long int x=1;

	while(n>9)
	{
		if(n%10==0)
		{
			n/=10;
			continue;
		}
		else
		{
			x=x*(n%10);
			n/=10;
		}
	}
	return n*x;
}

int main(){
	long long int prod[1000002];
	long long int dp[10][1000002];

	prod[0]=0;
	prod[1]=1;
	prod[2]=2;
	prod[3]=3;
	prod[4]=4;
	prod[5]=5;
	prod[6]=6;
	prod[7]=7;
	prod[8]=8;
	prod[9]=9;

	for(long long int i=10;i<=1000001;i++)
	{
		prod[i]=prod[multiple_dp(i)];
	}

	for(long long int i=1;i<10;i++)
	{
		dp[i][i]=1;
	}

	for(long long int i=10;i<=1000001;i++)
	{
		long long int d=prod[i];
		dp[d][i]=1;
	}

for(long long int j=1;j<=9;j++)
{
	for(long long int i=1;i<=1000001;i++)
	{
		dp[j][i]=dp[j][i]+dp[j][i-1];
	}
}

long long int q;
cin>>q;
while(q--)
{
	long long int l,r,k;
	cin>>l>>r>>k;

	cout<<dp[k][r]-dp[k][l-1]<<endl;
}

return 0;
}