/*#include #include #include #include #include #include #include #include #include #include #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int MN=1000000+10; const int ME=100000+10; const int mod=1000000007; typedef long long LL; typedef unsigned long long ULL; const double pi=acos(-1.0); const int Intmax=(~(1<<31)); const int Intmin=(1<<31); const LL LLmax=(~(1LL<<63)); const LL LLmin=(1LL<<63); const ULL ULLmax=(~(0ULL)); int c[123]; char s[123]; int dp[123]; int main(){ #ifndef ONLINE_JUDGE freopen("1.cpp","r",stdin); #endif // ONLINE_JUDGE int t,k,l; scanf("%d",&t); while(t--){ memset(c,0,sizeof(c)); scanf("%d%d%d",&n,&k,&l); for(int i=0;i=0&&j+l=0&&j+1+l=l){ printf("True\n"); } else { bool can=0; memset(dp,0,sizeof(dp)); int i,j,k; for(i=1;i<=100;i++){ for(j=1;j<=c[i];j*=2){ int v=i*j; for(k=l;k>=v;k--){ } } } if(can) printf("True\n"); else printf("False\n"); } } return 0; } */ #include #include #include #include #include #include #include #include #include #include #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int MN=1000000+10; const int ME=100000+10; const int mod=1000000007; typedef long long LL; typedef unsigned long long ULL; const double pi=acos(-1.0); const int Intmax=(~(1<<31)); const int Intmin=(1<<31); const LL LLmax=(~(1LL<<63)); const LL LLmin=(1LL<<63); const ULL ULLmax=(~(0ULL)); int l; char a[25]; char ans[25]; char tmp[25]; void dfs(int i,int x,bool flag){ if(i==l){ if(x!=l/2) return ; ans[i++]='\0'; int c=strcmp(ans,a); int p=strcmp(ans,tmp); if(c>=0){ if(p<0){ strcpy(tmp,ans); } } return ; } if(flag){ if(xa[i]) tmp=true; dfs(i+1,x+1,tmp); }else if(a[i]=='4'){ ans[i]='4'; dfs(i+1,x+1,false); ans[i]='7'; dfs(i+1,x,true); }else{ ans[i]='7'; bool tmp=false; if(ans[i]>a[i]) tmp=true; dfs(i+1,x,tmp); } } } int main(){ #ifndef ONLINE_JUDGE freopen("1.cpp","r",stdin); #endif // ONLINE_JUDGE int t; scanf("%d",&t); while(t--){ scanf("%s",a); l=strlen(a); memset(tmp,'9',sizeof(tmp)); tmp[24]='\0'; if(l&1){ l++; int x=l/2,y=l/2; while(x--){ printf("4"); } while(y--){ printf("7"); } printf("\n"); }else{ int i; LL A=0; LL B=0; for(i=0;iB){ l+=2; int x=l/2,y=l/2; while(x--) printf("4"); while(y--) printf("7"); printf("\n"); } else{ bool flag=false; dfs(0,0,0); printf("%s\n",tmp); } } } return 0; }