#include #include #define ll long long using namespace std; ll num[10000000]; int T; ll n; int cnt; void dfs(int len,ll number,int x,int y) { if (len > 18) return; if (x + 1 == y) num[++cnt] = number * 10 + 4;dfs(len + 1,number * 10 + 4,x + 1,y); if (x == y + 1) num[++cnt] = number * 10 + 7;dfs(len + 1,number * 10 + 7,x,y + 1); } int main() { scanf("%d",&T); dfs(1,0,0,0);sort(num + 1,num + 1 + cnt); while (T--) { scanf("%I64d",&n); int ans = lower_bound(num + 1,num + 1 + cnt,n) - num; if (n > num[cnt]) { for (int i = 1;i <= 10;i++) printf("4"); for (int i = 1;i <= 10;i++) printf("7"); printf("\n"); } else printf("%I64d\n",num[ans]); } }