http://acm.hdu.edu.cn/showproblem.php?pid=3943
被虐哈哈哈
该练递推了
//#define _TEST _TEST
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
/************************************************
Code By willinglive Blog:http://willinglive.cf
************************************************/
#define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++)
#define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--)
#define MS(arr,x) memset(arr,x,sizeof(arr))
#define LL long long
#define INE(i,u,e) for(int i=head[u];~i;i=e[i].next)
inline const LL read()
{LL r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1;
for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;}
/////////////////////////////////////////////////
LL x,y,l,r;
LL dp[25][25][25];
int bit[25];
/////////////////////////////////////////////////
void predoing()
{
MS(dp,0);
dp[0][0][0]=1;
rep(i,0,19)
{
rep(j,0,i) rep(k,0,i-j)
{
//枚举第i+1位有十种情况
dp[i+1][j][k]+=dp[i][j][k]*8;
dp[i+1][j+1][k]+=dp[i][j][k];
dp[i+1][j][k+1]+=dp[i][j][k];
}
}
}
LL get_count(LL n)
{
n++;
for(*bit=0;n;n/=10) bit[++*bit]=n%10;
LL ans=0;
int cx=x,cy=y;
per(i,*bit,1)
{
rep(j,0,bit[i]-1)
{
if(j==4)
{
if(cx) ans+=dp[i-1][cx-1][cy];
}
else if(j==7)
{
if(cy) ans+=dp[i-1][cx][cy-1];
}
else ans+=dp[i-1][cx][cy];
}
if(bit[i]==4) cx--;
else if(bit[i]==7) cy--;
if(cx<0||cy<0) break;
}
return ans;
}
LL cal(LL n)
{
int len=1;
for(;;len++) if(dp[len][x][y]>=n) break;
LL ans=0;
int cx=x,cy=y;
per(i,len,1)
{
rep(j,0,9)
{
LL sub;
if(j==4)
{
if(cx) sub=dp[i-1][cx-1][cy];
else continue;
}
else if(j==7)
{
if(cy) sub=dp[i-1][cx][cy-1];
else continue;
}
else sub=dp[i-1][cx][cy];
if(n-sub<=0)
{
if(j==4) cx--;
if(j==7) cy--;
ans=ans*10+j;
break;
}
n-=sub;
}
}
return ans;
}
/////////////////////////////////////////////////
void solve()
{
predoing();
rep(cases,1,read())
{
printf("Case #%d:\n",cases);
l=read(); r=read(); x=read(); y=read(); l++;
LL rr=get_count(r),ll=get_count(l-1);
rep(n,1,read())
{
LL k=read();
if(k>rr-ll){puts("Nya!");continue;}
printf("%I64d\n",cal(k+ll));
}
}
}
/////////////////////////////////////////////////
int main()
{
#ifndef _TEST
freopen("std.in","r",stdin); freopen("std.out","w",stdout);
#endif
solve();
return 0;
}