#include #include #define MAX 200010 #define MAXN 21 #define INF 777777777444444444 char ans[50] = { "44444444447777777777" }; #define init(x,y) memset(x,y,sizeof(x)) using namespace std; int n, m, i, j, k, t, sum = 0; __int64 num[MAX]; int map[2] = { 4,7 }; int tre[MAXN]; __int64 temp; int s[2] = { 0 }; int dfs(int x) { temp = 0; if (s[0] == s[1]) { for (int i = 0; i < x; i++) { temp *= 10; temp += tre[i]; } num[sum++] = temp; } if (x == 18) { return 0; } for (int j = 0; j < 2; j++) { tre[x] = map[j]; s[j]++; dfs(x + 1); s[j]--; tre[x] = 0; } return 0; } int main() { dfs(0); sort(num, num + sum); scanf("%d", &t); while (t--) { __int64 a = 0; int b = 0; scanf("%I64d", &a); b = lower_bound(num + 1, num + sum, a) - num; __int64 c = 0; c = num[b]; if (a > INF){ printf("%s\n", ans); } else { printf("%I64d\n", c); } } return 0; }