#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef pair pii; typedef long long ull; typedef long long ll; typedef vector vi; #define xx first #define yy second #define rep(i, a, n) for (int i = a; i < n; i++) #define sa(n) scanf("%d", &(n)) #define vep(c) for(decltype((c).begin()) it = (c).begin(); it != (c).end(); it++) const int mod = int(1e9) + 7, INF = 0x3fffffff, maxn = 1e5 + 12; char str[20]; char ans[20]; int len; bool ok, key; void dfs(int pos, bool limit, int four, int seven) { if (ok) return; if (four > len / 2) return; if (seven > len / 2) return; if (!limit) { rep (i, 0, len / 2 - four) ans[pos++] = '4'; rep (i, 0, len / 2 - seven) ans[pos++] = '7'; ok = true; } if (pos == len) { ok = true; return; } if (str[pos] < '4') { ans[pos] = '4'; dfs(pos + 1, false, four + 1, seven); } if (ok) return; if (str[pos] == '4') { ans[pos] = '4'; dfs(pos + 1, true, four + 1, seven); } if (ok) return; if (str[pos] < '7') { ans[pos] = '7'; dfs(pos + 1, false, four, seven + 1); } if (ok) return; if (str[pos] == '7') { ans[pos] = '7'; dfs(pos + 1, true, four, seven + 1); } if (ok) return; } int main(void) { #ifdef LOCAL freopen(“in.txt”, “r”, stdin); //freopen(“out.txt”, “w”, stdout); #endif ll n; int T; sa(T); while (T--) { scanf("%s", str); len = strlen(str); ok = false; key = false; if (len % 2) { len++; rep (i, 0, len / 2) printf("4"); rep (i, 0, len / 2) printf("7"); puts(""); } else { if (str[0] <= '3') { rep (i, 0, len / 2) printf("4"); rep (i, 0, len / 2) printf("7"); puts(""); } else if (str[0] > '4' && str[0] <= '6') { printf("7"); rep (i, 0, len / 2) printf("4"); rep (i, 0, len / 2 - 1) printf("7"); puts(""); } else if (str[0] > '7') { len += 2; rep (i, 0, len / 2) printf("4"); rep (i, 0, len / 2) printf("7"); puts(""); } else { dfs(0, true, 0, 0); if (key || !ok) { len += 2; rep (i, 0, len / 2) printf("4"); rep (i, 0, len / 2) printf("7"); puts(""); } else { ans[len] = '\0'; printf("%s\n", ans); } } } } return 0; }