#include using namespace std; struct UnionFind { vector< int > data; UnionFind(int sz) { data.assign(sz, -1); } bool unite(int x, int y) { x = find(x), y = find(y); if(x == y) return (false); if(data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return (true); } int find(int k) { if(data[k] < 0) return (k); return (data[k] = find(data[k])); } int size(int k) { return (-data[find(k)]); } }; typedef long long int64; void solve() { int T; cin >> T; cin.ignore(); while(T--) { string S; getline(cin, S); UnionFind tree(S.size()); for(int i = 1; i < S.size(); i++) { if(isdigit(S[i - 1]) && isdigit(S[i])) tree.unite(i - 1, i); } int length = 0; for(int i = 0; i < S.size(); i++) { if(isdigit(S[i])) length = max(length, tree.size(i)); } if(length == 0) { cout << S << endl; } else { string X, Y, Z; int root; for(int i = S.size() - 1; i >= 0; i--) { if(isdigit(S[i]) && tree.size(i) == length) { root = tree.find(i); for(int j = i; j >= 0; j--) { if(tree.find(j) == root) Z += S[j]; } break; } X += S[i]; } reverse(begin(X), end(X)); reverse(begin(Z), end(Z)); for(int i = 0; i < S.size(); i++) { if(tree.find(i) == root) break; Y += S[i]; } bool inc = false; for(int i = Z.size() - 1; i >= 0; i--) { if(Z[i] == '9') { inc = true; Z[i] = '0'; } else { inc = false; Z[i]++; break; } } if(inc) Z = "1" + Z; cout << Y << Z << X << endl; } } } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); }