#include <bits/stdc++.h>
using namespace std;

#define endl "\n"
#define int long long
#define uint unsigned long long
#define deb(x) cout << #x << " => " << x << "\n"
#define deba(x) cout << #x << "\n"; for (auto a : x) cout << a << " "; cout << "\n";
#define debm(x) cout << #x << "\n"; for (auto a : x) {for(auto b : a) cout << b << " "; cout << "\n";}
#define getMat(x, n, m, val) vector<vector<int>> x(n, vector<int> (m, val))
#define fastio ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pout cout << fixed << setprecision(10)
#define MOD (int) (1e9 + 7)

int fastpow(int a, int b, int m) {
    a %= m;
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

void modify(vector<int> &arr, int i, int j, int k) {
    int index = -1;
    for (int c = j; c < i; c++) {
        if (c != i and arr[c] > arr[i]) {
            index = c;
            break;
        }
    }
    if (index == -1) {
        for (int c = k; c > i; c--) {
            if (c != i and arr[c] < arr[i]) {
                index = c;
                break;
            }
        }
    }
    if (index != -1) {
        if (index < i) {
            int temp = arr[index];
            arr[index] = arr[i];
            for (int c = index + 1; c <= i; c++) {
                int t = arr[c];
                arr[c] = temp;
                temp = t;
            }
        } else {
            int temp = arr[index];
            arr[index] = arr[i];
            for (int c = index - 1; c >= i; c--) {
                int t = arr[c];
                arr[c] = temp;
                temp = t;
            }
        }
    }
}

void modify2(vector<int> &arr, int i, int j, int k) {
    int index = -1;
    for (int c = j; c < i; c++) {
        if (c != i and arr[c] > arr[i]) {
            index = c;
            break;
        }
    }
    if (index == -1) {
        for (int c = k; c > i; c--) {
            if (c != i and arr[c] < arr[i]) {
                index = c;
                break;
            }
        }
    }
    if (index != -1) {
        if (index < i) {
            int temp = arr[index];
            arr[index] = arr[i];
            for (int c = index + 1; c <= i; c++) {
                int t = arr[c];
                arr[c] = temp;
                temp = t;
            }
        } else {
            int temp = arr[index];
            arr[index] = arr[i];
            for (int c = index - 1; c >= i; c--) {
                int t = arr[c];
                arr[c] = temp;
                temp = t;
            }
        }
    }
}

int32_t main() {
    fastio;
    int t; cin >> t;
    while (t--) {
        string s; cin >> s;
        int n = (int) s.size();
        vector<int> arr(n);
        for (int i = 0; i < n; i++) {
            arr[i] = s[i] - '0';
        }
        for (int i = 0; i < n; i++) {
            if (arr[i] % 2 == 1) {
                int j = i - 1;
                while (j > 0 and arr[j] % 2 == 0) {
                    j--;
                }
                j++;
                int k = i + 1;
                while (k < n and arr[k] % 2 == 0) {
                    k++;
                }
                k--;
                modify(arr, i, j, k);
                i = k;
            }
        }
        for (int i = 0; i < n; i++) {
            if (arr[i] % 2 == 0) {
                int j = i - 1;
                while (j > 0 and arr[j] % 2 == 1) {
                    j--;
                }
                j++;
                int k = i + 1;
                while (k < n and arr[k] % 2 == 1) {
                    k++;
                }
                k--;
                modify2(arr, i, j, k);
                i = k;
            }
        }
        for (int i = 0; i < n; i++) {
            cout << arr[i];
        } cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define endl "\n"
#define int long long
#define uint unsigned long long
#define deb(x) cout << #x << " => " << x << "\n"
#define deba(x) cout << #x << "\n"; for (auto a : x) cout << a << " "; cout << "\n";
#define debm(x) cout << #x << "\n"; for (auto a : x) {for(auto b : a) cout << b << " "; cout << "\n";}
#define getMat(x, n, m, val) vector<vector<int>> x(n, vector<int> (m, val))
#define fastio ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pout cout << fixed << setprecision(10)
#define MOD (int) (1e9 + 7)

int fastpow(int a, int b, int m) {
    a %= m;
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

void modify(vector<int> &arr, int i, int j, int k) {

    // int index = i - 1;
    // bool done = false;
    // while (index >= j and arr[index] > arr[index + 1]) {
    //     swap(arr[index], arr[index + 1]);
    //     index--;
    //     done = true;
    // }

    int index = -1;
    for (int c = j; c < i; c++) {
        if (c != i and arr[c] > arr[i]) {
            index = c;
            break;
        }
    }

    if (index == -1) {
        int index = i + 1;
        while (index <= k and arr[index] < arr[index - 1]) {
            swap(arr[index], arr[index - 1]);
            index++;
        }
    } else {
        int temp = arr[index];
        arr[index] = arr[i];
        for (int c = index + 1; c <= i; c++) {
            int t = arr[c];
            arr[c] = temp;
            temp = t;
        }
    }

    // int index = -1;
    // for (int c = j; c < i; c++) {
    //     if (c != i and arr[c] > arr[i]) {
    //         index = c;
    //         break;
    //     }
    // }
    // if (index == -1) {
    //     for (int c = k; c > i; c--) {
    //         if (c != i and arr[c] < arr[i]) {
    //             index = c;
    //             break;
    //         }
    //     }
    // }
    // if (index != -1) {
    //     if (index < i) {
    //         int temp = arr[index];
    //         arr[index] = arr[i];
    //         for (int c = index + 1; c <= i; c++) {
    //             int t = arr[c];
    //             arr[c] = temp;
    //             temp = t;
    //         }
    //     } else {
    //         int temp = arr[index];
    //         arr[index] = arr[i];
    //         for (int c = index - 1; c >= i; c--) {
    //             int t = arr[c];
    //             arr[c] = temp;
    //             temp = t;
    //         }
    //     }
    // }
}

void modify2(vector<int> &arr, int i, int j, int k) {

    // int index = i - 1;
    // bool done = false;
    // while (index >= j and arr[index] > arr[index + 1]) {
    //     swap(arr[index], arr[index + 1]);
    //     index--;
    //     done = true;
    // }

    int index = -1;
    for (int c = j; c < i; c++) {
        if (c != i and arr[c] > arr[i]) {
            index = c;
            break;
        }
    }

    if (index == -1) {
        int index = i + 1;
        while (index <= k and arr[index] < arr[index - 1]) {
            swap(arr[index], arr[index - 1]);
            index++;
        }
    } else {
        int temp = arr[index];
        arr[index] = arr[i];
        for (int c = index + 1; c <= i; c++) {
            int t = arr[c];
            arr[c] = temp;
            temp = t;
        }
    }

    // int index = -1;
    // for (int c = j; c < i; c++) {
    //     if (c != i and arr[c] > arr[i]) {
    //         index = c;
    //         break;
    //     }
    // }
    // if (index == -1) {
    //     for (int c = k; c > i; c--) {
    //         if (c != i and arr[c] < arr[i]) {
    //             index = c;
    //             break;
    //         }
    //     }
    // }
    // if (index != -1) {
    //     if (index < i) {
    //         int temp = arr[index];
    //         arr[index] = arr[i];
    //         for (int c = index + 1; c <= i; c++) {
    //             int t = arr[c];
    //             arr[c] = temp;
    //             temp = t;
    //         }
    //     } else {
    //         int temp = arr[index];
    //         arr[index] = arr[i];
    //         for (int c = index - 1; c >= i; c--) {
    //             int t = arr[c];
    //             arr[c] = temp;
    //             temp = t;
    //         }
    //     }
    // }
}

int32_t main() {
    fastio;
    int t; cin >> t;
    while (t--) {
        string s; cin >> s;
        int n = (int) s.size();
        vector<int> arr(n);
        for (int i = 0; i < n; i++) {
            arr[i] = s[i] - '0';
        }
        for (int i = 0; i < n; i++) {
            if (arr[i] % 2 == 1) {
                int j = i - 1;
                while (j > 0 and arr[j] % 2 == 0) {
                    j--;
                }
                j++;
                int k = i + 1;
                while (k < n and arr[k] % 2 == 0) {
                    k++;
                }
                k--;
                modify(arr, i, j, k);
                i = k;
            }
        }
        for (int i = 0; i < n; i++) {
            if (arr[i] % 2 == 0) {
                int j = i - 1;
                while (j > 0 and arr[j] % 2 == 1) {
                    j--;
                }
                j++;
                int k = i + 1;
                while (k < n and arr[k] % 2 == 1) {
                    k++;
                }
                k--;
                modify2(arr, i, j, k);
                i = k;
            }
        }
        for (int i = 0; i < n; i++) {
            cout << arr[i];
        } cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define endl "\n"
#define int long long
#define uint unsigned long long
#define deb(x) cout << #x << " => " << x << "\n"
#define deba(x) cout << #x << "\n"; for (auto a : x) cout << a << " "; cout << "\n";
#define debm(x) cout << #x << "\n"; for (auto a : x) {for(auto b : a) cout << b << " "; cout << "\n";}
#define getMat(x, n, m, val) vector<vector<int>> x(n, vector<int> (m, val))
#define fastio ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pout cout << fixed << setprecision(10)
#define MOD (int) (1e9 + 7)

int now() {
    std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()
    .time_since_epoch()).count();
}

int fastpow(int a, int b, int m) {
    a %= m;
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

int modify(vector<int> &arr, int i, int j, int k) {
    int index = -1;
    for (int c = j; c < i; c++) {
        if (c != i and arr[c] > arr[i]) {
            index = c;
            break;
        }
    }

    if (index == -1) {
        int index = i + 1;
        while (index <= k and arr[index] < arr[index - 1]) {
            swap(arr[index], arr[index - 1]);
            index++;
        }
        return index;
    } else {
        int temp = arr[index];
        arr[index] = arr[i];
        for (int c = index + 1; c <= i; c++) {
            int t = arr[c];
            arr[c] = temp;
            temp = t;
        }
        return index + 1;
    }
}

int modify2(vector<int> &arr, int i, int j, int k) {
    int index = -1;
    for (int c = j; c < i; c++) {
        if (c != i and arr[c] > arr[i]) {
            index = c;
            break;
        }
    }

    if (index == -1) {
        int index = i + 1;
        while (index <= k and arr[index] < arr[index - 1]) {
            swap(arr[index], arr[index - 1]);
            index++;
        }
        return index;
    } else {
        int temp = arr[index];
        arr[index] = arr[i];
        for (int c = index + 1; c <= i; c++) {
            int t = arr[c];
            arr[c] = temp;
            temp = t;
        }
        return index + 1;
    }
}

int32_t main() {
    fastio;
    time_t start = now();
    int t; cin >> t;
    while (t--) {
        string s; cin >> s;
        int n = (int) s.size();
        vector<int> arr(n);
        for (int i = 0; i < n; i++) {
            arr[i] = s[i] - '0';
        }
        int j = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] % 2 == 1) {
                // int j = i - 1;
                // while (j >= 0 and arr[j] % 2 == 0) {
                //     j--;
                // }
                // j++;
                int k = i + 1;
                while (k < n and arr[k] % 2 == 0) {
                    k++;
                }
                k--;
                j = modify(arr, i, j, k);
                i = k;
            }
        }
        j = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] % 2 == 0) {
                // int j = i - 1;
                // while (j >= 0 and (arr[j] % 2 == 1)) {
                //     j--;
                // }
                // j++;
                int k = i + 1;
                while (k < n and (arr[k] % 2 == 1)) {
                    k++;
                }
                k--;
                j = modify2(arr, i, j, k);
                i = k;
            }
        }
        for (int i = 0; i < n; i++) {
            cout << arr[i];
        } cout << endl;
    }
    time_t end = now();
    // cout << "TIME: " << end - start << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define endl "\n"
#define int long long
#define uint unsigned long long
#define deb(x) cout << #x << " => " << x << "\n"
#define deba(x) cout << #x << "\n"; for (auto a : x) cout << a << " "; cout << "\n";
#define debm(x) cout << #x << "\n"; for (auto a : x) {for(auto b : a) cout << b << " "; cout << "\n";}
#define getMat(x, n, m, val) vector<vector<int>> x(n, vector<int> (m, val))
#define fastio ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pout cout << fixed << setprecision(10)
#define MOD (int) (1e9 + 7)

int fastpow(int a, int b, int m) {
    a %= m;
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

void modify(vector<int> &arr, int i, int j, int k) {

    // int index = i - 1;
    // bool done = false;
    // while (index >= j and arr[index] > arr[index + 1]) {
    //     swap(arr[index], arr[index + 1]);
    //     index--;
    //     done = true;
    // }

    int index = -1;
    for (int c = j; c < i; c++) {
        if (c != i and arr[c] > arr[i]) {
            index = c;
            break;
        }
    }

    if (index == -1) {
        int index = i + 1;
        while (index <= k and arr[index] < arr[index - 1]) {
            swap(arr[index], arr[index - 1]);
            index++;
        }
    } else {
        int temp = arr[index];
        arr[index] = arr[i];
        for (int c = index + 1; c <= i; c++) {
            int t = arr[c];
            arr[c] = temp;
            temp = t;
        }
    }

    // int index = -1;
    // for (int c = j; c < i; c++) {
    //     if (c != i and arr[c] > arr[i]) {
    //         index = c;
    //         break;
    //     }
    // }
    // if (index == -1) {
    //     for (int c = k; c > i; c--) {
    //         if (c != i and arr[c] < arr[i]) {
    //             index = c;
    //             break;
    //         }
    //     }
    // }
    // if (index != -1) {
    //     if (index < i) {
    //         int temp = arr[index];
    //         arr[index] = arr[i];
    //         for (int c = index + 1; c <= i; c++) {
    //             int t = arr[c];
    //             arr[c] = temp;
    //             temp = t;
    //         }
    //     } else {
    //         int temp = arr[index];
    //         arr[index] = arr[i];
    //         for (int c = index - 1; c >= i; c--) {
    //             int t = arr[c];
    //             arr[c] = temp;
    //             temp = t;
    //         }
    //     }
    // }
}

void modify2(vector<int> &arr, int i, int j, int k) {

    // int index = i - 1;
    // bool done = false;
    // while (index >= j and arr[index] > arr[index + 1]) {
    //     swap(arr[index], arr[index + 1]);
    //     index--;
    //     done = true;
    // }

    int index = -1;
    for (int c = j; c < i; c++) {
        if (arr[c] > arr[i]) {
            index = c;
            break;
        }
    }

    if (index == -1) {
        int index = i + 1;
        while (index <= k and arr[index] < arr[index - 1]) {
            swap(arr[index], arr[index - 1]);
            index++;
        }
    } else {
        int temp = arr[index];
        arr[index] = arr[i];
        for (int c = index + 1; c <= i; c++) {
            int t = arr[c];
            arr[c] = temp;
            temp = t;
        }
    }

    // int index = -1;
    // for (int c = j; c < i; c++) {
    //     if (c != i and arr[c] > arr[i]) {
    //         index = c;
    //         break;
    //     }
    // }
    // if (index == -1) {
    //     for (int c = k; c > i; c--) {
    //         if (c != i and arr[c] < arr[i]) {
    //             index = c;
    //             break;
    //         }
    //     }
    // }
    // if (index != -1) {
    //     if (index < i) {
    //         int temp = arr[index];
    //         arr[index] = arr[i];
    //         for (int c = index + 1; c <= i; c++) {
    //             int t = arr[c];
    //             arr[c] = temp;
    //             temp = t;
    //         }
    //     } else {
    //         int temp = arr[index];
    //         arr[index] = arr[i];
    //         for (int c = index - 1; c >= i; c--) {
    //             int t = arr[c];
    //             arr[c] = temp;
    //             temp = t;
    //         }
    //     }
    // }
}

int32_t main() {
    fastio;
    int t; cin >> t;
    while (t--) {
        string s; cin >> s;
        int n = (int) s.size();
        vector<int> arr(n);
        for (int i = 0; i < n; i++) {
            arr[i] = s[i] - '0';
        }
        for (int i = 0; i < n; i++) {
            if (arr[i] % 2 == 1) {
                int j = i - 1;
                while (j >= 0 and arr[j] % 2 == 0) {
                    j--;
                }
                j++;
                int k = i + 1;
                while (k < n and arr[k] % 2 == 0) {
                    k++;
                }
                k--;
                modify(arr, i, j, k);
                i = k;
            }
        }
        for (int i = 0; i < n; i++) {
            if (arr[i] % 2 == 0) {
                int j = i - 1;
                while (j >= 0 and arr[j] % 2 == 1) {
                    j--;
                }
                j++;
                int k = i + 1;
                while (k < n and arr[k] % 2 == 1) {
                    k++;
                }
                k--;
                modify2(arr, i, j, k);
                i = k;
            }
        }
        for (int i = 0; i < n; i++) {
            cout << arr[i];
        } cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define endl "\n"
#define int long long
#define uint unsigned long long
#define deb(x) cout << #x << " => " << x << "\n"
#define deba(x) cout << #x << "\n"; for (auto a : x) cout << a << " "; cout << "\n";
#define debm(x) cout << #x << "\n"; for (auto a : x) {for(auto b : a) cout << b << " "; cout << "\n";}
#define getMat(x, n, m, val) vector<vector<int>> x(n, vector<int> (m, val))
#define fastio ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pout cout << fixed << setprecision(10)
#define MOD (int) (1e9 + 7)

int fastpow(int a, int b, int m) {
    a %= m;
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

int32_t main() {
    fastio;
    int t; cin >> t;
    while (t--) {
        string s; cin >> s;
        string c1, c2;
        for (auto x : s) {
            if (x & 1) {
                c1.push_back(x);
            } else {
                c2.push_back(x);
            }
        }

        int i = 0, j = 0;
        while (i < c1.length() and j < c2.length()) {
            if (c1[i] < c2[j]) {
                cout << c1[i]; i++;
            } else {
                cout << c2[j]; j++;
            }
        }
        while (i < c1.length()) {
            cout << c1[i]; i++;
        }
        while (j < c2.length()) {
            cout << c2[j]; j++;
        }
        cout << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define endl "\n"
#define int long long
#define uint unsigned long long
#define deb(x) cout << #x << " => " << x << "\n"
#define deba(x) cout << #x << "\n"; for (auto a : x) cout << a << " "; cout << "\n";
#define debm(x) cout << #x << "\n"; for (auto a : x) {for(auto b : a) cout << b << " "; cout << "\n";}
#define getMat(x, n, m, val) vector<vector<int>> x(n, vector<int> (m, val))
#define fastio ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pout cout << fixed << setprecision(10)
#define MOD (int) (1e9 + 7)

int fastpow(int a, int b, int m) {
    a %= m;
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

void modify(vector<int> &arr, int i, int j, int k) {
    int index = -1;
    for (int c = j; c < i; c++) {
        if (c != i and arr[c] > arr[i]) {
            index = c;
            break;
        }
    }
    if (index == -1) {
        for (int c = k; c > i; c--) {
            if (c != i and arr[c] < arr[i]) {
                index = c;
                break;
            }
        }
    }
    if (index != -1) {
        if (index < i) {
            int temp = arr[index];
            arr[index] = arr[i];
            for (int c = index + 1; c <= i; c++) {
                int t = arr[c];
                arr[c] = temp;
                temp = t;
            }
        } else {
            int temp = arr[index];
            arr[index] = arr[i];
            for (int c = index - 1; c >= i; c--) {
                int t = arr[c];
                arr[c] = temp;
                temp = t;
            }
        }
    }
}

int32_t main() {
    fastio;
    int t; cin >> t;
    while (t--) {
        string s; cin >> s;
        int n = (int) s.size();
        vector<int> arr(n);
        for (int i = 0; i < n; i++) {
            arr[i] = s[i] - '0';
        }
        for (int i = 0; i < n; i++) {
            if (arr[i] % 2 == 1) {
                int j = i - 1;
                while (j > 0 and arr[j] % 2 == 0) {
                    j--;
                }
                j++;
                int k = i + 1;
                while (k < n and arr[k] % 2 == 0) {
                    k++;
                }
                k--;
                modify(arr, i, j, k);
                i = k;
            }
        }
        for (int i = 0; i < n; i++) {
            cout << arr[i];
        } cout << endl;
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef double db;
typedef long long ll;
const int maxn=1000;
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt","r",stdin);
    #endif
    int n;
    char str[100];
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        scanf("%s",str);
        int len=strlen(str);
        list<char> v,v1;
        for(int i=0;i<len;i++)
        {
            if((str[i]-'0')&1)
                v.push_back(str[i]);//奇数
            else
                v1.push_back(str[i]);//偶数
        }
        int num1=v1.size(),num2=v.size();
        if(num1>num2)
        {
            while((!v.empty())||(!v1.empty()))
            {
                if(v.empty())
                {
                      //cout<<"A"<<endl;
                      printf("%c",v1.front());v1.pop_front();
                }
                else if(v1.empty())
                {
                    //cout<<"B"<<endl;
                     printf("%c",v.front());v.pop_front();
                }
                else  if(v1.front()<v.front())
                {
                   //cout<<"C"<<endl;
                    printf("%c",v1.front());v1.pop_front();
                }
                else
                {
                    //cout<<"D"<<endl;
                    printf("%c",v.front());v.pop_front();
                }

            }
            printf("\n");
        }
        else{
        //奇数大于等于偶数个数
             while((!v.empty())||(!v1.empty()))
            {
                if(v.empty())
                {
                    //cout<<"E"<<endl;
                      printf("%c",v1.front());v1.pop_front();
                }
                else if(v1.empty())
                {
                    //cout<<"F"<<endl;
                     printf("%c",v.front());v.pop_front();
                }
                else if(v1.front()<v.front())
                {
                   // cout<<"G"<<endl;
                    printf("%c",v1.front());v1.pop_front();
                }
                else
                {
                   // cout<<"H"<<endl;
                    printf("%c",v.front());v.pop_front();
                }
            }
            printf("\n");
        }
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef double db;
typedef long long ll;
const int maxn=1000;
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt","r",stdin);
    #endif
    int n;
    scanf("%d",&n);
    while(n--)
    {
        char str[10];
        scanf("%s",str);
        int len=strlen(str);
        list<char> v,v1;
        for(int i=0;i<len;i++)
        {
            if((str[i]-'0')&1)
                v.push_back(str[i]);//奇数
            else
                v1.push_back(str[i]);//偶数
        }
        int num1=v1.size(),num2=v.size();
        if(num1>num2)
        {
            for(int i=0;i<=max(v1.size(),v.size());i++)
            {
                if(v.empty())
                {
                      //cout<<"A"<<endl;
                      printf("%c",v1.front());v1.pop_front();
                }
                else if(v1.empty())
                {
                    //cout<<"B"<<endl;
                     printf("%c",v.front());v.pop_front();
                }
                else  if(v1.front()<v.front())
                {
                   //cout<<"C"<<endl;
                    printf("%c",v1.front());v1.pop_front();
                }
                else
                {
                    //cout<<"D"<<endl;
                    printf("%c",v.front());v.pop_front();
                }
                i=0;
            }
            printf("\n");
        }
        else{
        //奇数大于等于偶数个数
            for(int i=0;i<=max(v.size(),v1.size());i++)
            {
                if(v.empty())
                {
                    //cout<<"E"<<endl;
                      printf("%c",v1.front());v1.pop_front();
                }
                else if(v1.empty())
                {
                    //cout<<"F"<<endl;
                     printf("%c",v.front());v.pop_front();
                }
                else if(v1.front()<v.front())
                {
                   // cout<<"G"<<endl;
                    printf("%c",v1.front());v1.pop_front();
                }
                else
                {
                   // cout<<"H"<<endl;
                    printf("%c",v.front());v.pop_front();
                }
                i=0;
            }
            printf("\n");
        }
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef double db;
typedef long long ll;
const int maxn=1000;
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt","r",stdin);
    #endif
    int n;
    char str[10];
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        scanf("%s",str);
        int len=strlen(str);
        list<char> v,v1;
        for(int i=0;i<len;i++)
        {
            if((str[i]-'0')&1)
                v.push_back(str[i]);//奇数
            else
                v1.push_back(str[i]);//偶数
        }
        int num1=v1.size(),num2=v.size();
        if(num1>num2)
        {
            while((!v.empty())||(!v1.empty()))
            {
                if(v.empty())
                {
                      printf("%c",v1.front());v1.pop_front();
                }
                else if(v1.empty())
                {
                     printf("%c",v.front());v.pop_front();
                }
                else  if(v1.front()<v.front())
                {
                    printf("%c",v1.front());v1.pop_front();
                }
                else
                {
                    printf("%c",v.front());v.pop_front();
                }

            }
            printf("\n");
            continue;
        }
             while((!v.empty())||(!v1.empty()))
            {
                if(v.empty())
                {
                      printf("%c",v1.front());v1.pop_front();
                }
                else if(v1.empty())
                {
                     printf("%c",v.front());v.pop_front();
                }
                else if(v1.front()<v.front())
                {
                    printf("%c",v1.front());v1.pop_front();
                }
                else
                {
                    printf("%c",v.front());v.pop_front();
                }
            }
            printf("\n");
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef double db;
typedef long long ll;
const int maxn=3e5+10;
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt","r",stdin);
    #endif
    int n;
    char str[maxn];
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        scanf("%s",str);
        int len=strlen(str);
        list<char> v,v1;
        for(int i=0;i<len;i++)
        {
            if((str[i]-'0')&1)
                v.push_back(str[i]);//奇数
            else
                v1.push_back(str[i]);//偶数
        }
        int num1=v1.size(),num2=v.size();
        if(num1>num2)
        {
            while((!v.empty())||(!v1.empty()))
            {
                if(v.empty())
                {
                      printf("%c",v1.front());v1.pop_front();
                }
                else if(v1.empty())
                {
                     printf("%c",v.front());v.pop_front();
                }
                else  if(v1.front()<v.front())
                {
                    printf("%c",v1.front());v1.pop_front();
                }
                else
                {
                    printf("%c",v.front());v.pop_front();
                }

            }
            printf("\n");
            continue;
        }
             while((!v.empty())||(!v1.empty()))
            {
                if(v.empty())
                {
                      printf("%c",v1.front());v1.pop_front();
                }
                else if(v1.empty())
                {
                     printf("%c",v.front());v.pop_front();
                }
                else if(v1.front()<v.front())
                {
                    printf("%c",v1.front());v1.pop_front();
                }
                else
                {
                    printf("%c",v.front());v.pop_front();
                }
            }
            printf("\n");
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef double db;
typedef long long ll;
const int maxn=1000;
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt","r",stdin);
    #endif
    int n;
    scanf("%d",&n);
    while(n--)
    {
        char str[10];
        scanf("%s",str);
        int len=strlen(str);
        list<char> v,v1;
        for(int i=0;i<len;i++)
        {
            if((str[i]-'0')&1)
                v.push_back(str[i]);//奇数
            else
                v1.push_back(str[i]);//偶数
        }
        int num1=v1.size(),num2=v.size();
        if(num1>num2)
        {
            while((!v.empty())||(!v1.empty()))
            {
                if(v.empty())
                {
                      //cout<<"A"<<endl;
                      printf("%c",v1.front());v1.pop_front();
                }
                else if(v1.empty())
                {
                    //cout<<"B"<<endl;
                     printf("%c",v.front());v.pop_front();
                }
                else  if(v1.front()<v.front())
                {
                   //cout<<"C"<<endl;
                    printf("%c",v1.front());v1.pop_front();
                }
                else
                {
                    //cout<<"D"<<endl;
                    printf("%c",v.front());v.pop_front();
                }

            }
            printf("\n");
        }
        else{
        //奇数大于等于偶数个数
             while((!v.empty())||(!v1.empty()))
            {
                if(v.empty())
                {
                    //cout<<"E"<<endl;
                      printf("%c",v1.front());v1.pop_front();
                }
                else if(v1.empty())
                {
                    //cout<<"F"<<endl;
                     printf("%c",v.front());v.pop_front();
                }
                else if(v1.front()<v.front())
                {
                   // cout<<"G"<<endl;
                    printf("%c",v1.front());v1.pop_front();
                }
                else
                {
                   // cout<<"H"<<endl;
                    printf("%c",v.front());v.pop_front();
                }
            }
            printf("\n");
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<ll, ll> pll;
typedef vector<pll> vll;
typedef pair<int, ii> tri;
typedef vector<tri> viii;

#define fi(i,a,b) for(auto i=a;i<b;i++)
#define trav(i,v) for(auto &i: v)
#define fd(i,a,b) for(auto i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mt make_tuple
#define ss second
#define ff first
#define sz(a) a.size()
#define sc(x) scanf("%d", &x)
#define sc2(x, y) scanf("%d %d", &x, &y)
#define sc3(x) scanf("%s", x)
#define sc4(x) scanf("%lld", &x)
#define sc5(x, y) scanf("%lld %lld", &x, &y)
#define sc6(x) scanf("%lf", &x)
#define sc7(x, y) scanf("%lf %lf", &x, &y)
#define mse(a, x) memset(a, x, sizeof(a))
#define mod 1000000007

#define TRACE
#ifdef TRACE
	#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1){
		cerr << name << ": " << arg1 << std::endl;
	}

	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args){
		const char* comma = strchr(names + 1, ',');
		cerr.write(names, comma - names) << ": " << arg1<<" |";
		__f(comma+1, args...);
	}
#else
	#define trace(...) ;
#endif

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

// int main() {
// 	int T;
// 	sc(T);
// 	char s[300300], t[300300], x[300300];
// 	while(T--) {
// 		sc3(s);
// 		mse(x, 0);
// 		int n = strlen(s);
// 		int i = 0, j = 0, k = 0, flag;
// 		if(s[0]%2 == 0) {
// 			flag = 0;
// 		} else {
// 			flag = 1;
// 		}
// 		while(i < n && j < n) {
// 			// trace(i, j, k, s[i], s[j]);
// 			if(s[i]%2 != s[j]%2) {
// 				if(s[i] > s[j]) {
// 					if(x[j] == 1) {
// 						j++;
// 					}
// 					x[j] = 1;
// 					t[k] = s[j];
// 					k++;
// 					j++;
// 				} else {
// 					if(x[i] == 1) {
// 						i++;
// 					}
// 					x[i] = 1;
// 					t[k] = s[i];
// 					k++;
// 					i++;
// 				}
// 			} else {
// 				// while(i < j && s[i]%2 == s[j]%2) {
// 				// 	if(x[i] == 1) {
// 				// 		i++;
// 				// 	}
// 				// 	x[i] = 1;
// 				// 	t[k] = s[i];
// 				// 	i++;
// 				// 	k++;
// 				// }
// 				// i = j;
// 				j++;
// 				// while(j < n && s[i]%2 == s[j]%2) {
// 				// 	j++;
// 				// }
// 			}
// 			// if(flag == 0) {
// 			// 	if(s[i]%2 == 0) {
// 			// 		j++;
// 			// 	} else {

// 			// 	}
// 			// } else {
// 			// 	if(s[i]%2 == 0) {

// 			// 	} else {
// 			// 		j++;
// 			// 	}
// 			// }
// 		}
// 		while(k < n) {
// 			if(x[i] == 1) {
// 				i++;
// 			}
// 			x[i] = 1;
// 			t[k] = s[i];
// 			i++;
// 			k++;
// 		}
// 		t[n] = '\0';
// 		printf("%s\n", t);
// 	}
// 	return 0;
// }

int main() {
	int T;
	sc(T);
	char s[300300], res[300300];
	while(T--) {
		sc3(s);
		int n = strlen(s);
		vii x;
		int prev = 0;
		mse(res, 0);
		fi(i, 0, n) {
			if(s[i]%2 == s[prev]%2) {
				continue;
			} else {
				x.pb({prev, i-1});
				prev = i;
			}
		}
		x.pb({prev, n-1});
		int a = 0, b = 0, k = 0;
		while(k < n) {
			trace(k, a, b, x[a].ff, x[a].ss, x[b].ff, x[b].ss, s[x[a].ff], s[x[b].ff]);
			if(b == x.size()) {
				if(x[a].ff > x[a].ss) {
					a++;
					continue;
				}
				fi(i, x[a].ff, x[a].ss+1) {
					res[k] = s[i];
					k++;
				}
				a += 2;
				continue;
			}
			if(x[a].ff > x[a].ss) {
				a++;
				continue;
			}
			if(x[b].ff > x[b].ss) {
				b++;
				continue;
			}
			if(a%2 == b%2) {
				b++;
			} else {
				if(s[x[b].ff] < s[x[a].ff]) {
					res[k++] = s[x[b].ff];
					x[b].ff++;
				} else {
					res[k++] = s[x[a].ff];
					x[a].ff++;
				}
			}
		}
		printf("%s\n", res);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<ll, ll> pll;
typedef vector<pll> vll;
typedef pair<int, ii> tri;
typedef vector<tri> viii;

#define fi(i,a,b) for(auto i=a;i<b;i++)
#define trav(i,v) for(auto &i: v)
#define fd(i,a,b) for(auto i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mt make_tuple
#define ss second
#define ff first
#define sz(a) a.size()
#define sc(x) scanf("%d", &x)
#define sc2(x, y) scanf("%d %d", &x, &y)
#define sc3(x) scanf("%s", x)
#define sc4(x) scanf("%lld", &x)
#define sc5(x, y) scanf("%lld %lld", &x, &y)
#define sc6(x) scanf("%lf", &x)
#define sc7(x, y) scanf("%lf %lf", &x, &y)
#define mse(a, x) memset(a, x, sizeof(a))
#define mod 1000000007

#define TRACE
#ifdef TRACE
	#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1){
		cerr << name << ": " << arg1 << std::endl;
	}

	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args){
		const char* comma = strchr(names + 1, ',');
		cerr.write(names, comma - names) << ": " << arg1<<" |";
		__f(comma+1, args...);
	}
#else
	#define trace(...) ;
#endif

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

int main() {
	int T;
	sc(T);
	char s[300300], t[300300], x[300300];
	while(T--) {
		sc3(s);
		mse(x, 0);
		int n = strlen(s);
		int i = 0, j = 0, k = 0, flag;
		if(s[0]%2 == 0) {
			flag = 0;
		} else {
			flag = 1;
		}
		while(i < n && j < n) {
			// trace(i, j, k, s[i], s[j]);
			if(s[i]%2 != s[j]%2) {
				if(s[i] > s[j]) {
					if(x[j] == 1) {
						j++;
					}
					x[j] = 1;
					t[k] = s[j];
					k++;
					j++;
				} else {
					if(x[i] == 1) {
						i++;
					}
					x[i] = 1;
					t[k] = s[i];
					k++;
					i++;
				}
			} else {
				// while(i < j && s[i]%2 == s[j]%2) {
				// 	if(x[i] == 1) {
				// 		i++;
				// 	}
				// 	x[i] = 1;
				// 	t[k] = s[i];
				// 	i++;
				// 	k++;
				// }
				// i = j;
				j++;
				// while(j < n && s[i]%2 == s[j]%2) {
				// 	j++;
				// }
			}
			// if(flag == 0) {
			// 	if(s[i]%2 == 0) {
			// 		j++;
			// 	} else {

			// 	}
			// } else {
			// 	if(s[i]%2 == 0) {

			// 	} else {
			// 		j++;
			// 	}
			// }
		}
		while(k < n) {
			if(x[i] == 1) {
				i++;
			}
			x[i] = 1;
			t[k] = s[i];
			i++;
			k++;
		}
		t[n] = '\0';
		printf("%s\n", t);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<ll, ll> pll;
typedef vector<pll> vll;
typedef pair<int, ii> tri;
typedef vector<tri> viii;

#define fi(i,a,b) for(auto i=a;i<b;i++)
#define trav(i,v) for(auto &i: v)
#define fd(i,a,b) for(auto i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mt make_tuple
#define ss second
#define ff first
#define sz(a) a.size()
#define sc(x) scanf("%d", &x)
#define sc2(x, y) scanf("%d %d", &x, &y)
#define sc3(x) scanf("%s", x)
#define sc4(x) scanf("%lld", &x)
#define sc5(x, y) scanf("%lld %lld", &x, &y)
#define sc6(x) scanf("%lf", &x)
#define sc7(x, y) scanf("%lf %lf", &x, &y)
#define mse(a, x) memset(a, x, sizeof(a))
#define mod 1000000007

#define TRACE
#ifdef TRACE
	#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1){
		cerr << name << ": " << arg1 << std::endl;
	}

	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args){
		const char* comma = strchr(names + 1, ',');
		cerr.write(names, comma - names) << ": " << arg1<<" |";
		__f(comma+1, args...);
	}
#else
	#define trace(...) ;
#endif

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

// int main() {
// 	int T;
// 	sc(T);
// 	char s[300300], t[300300], x[300300];
// 	while(T--) {
// 		sc3(s);
// 		mse(x, 0);
// 		int n = strlen(s);
// 		int i = 0, j = 0, k = 0, flag;
// 		if(s[0]%2 == 0) {
// 			flag = 0;
// 		} else {
// 			flag = 1;
// 		}
// 		while(i < n && j < n) {
// 			// trace(i, j, k, s[i], s[j]);
// 			if(s[i]%2 != s[j]%2) {
// 				if(s[i] > s[j]) {
// 					if(x[j] == 1) {
// 						j++;
// 					}
// 					x[j] = 1;
// 					t[k] = s[j];
// 					k++;
// 					j++;
// 				} else {
// 					if(x[i] == 1) {
// 						i++;
// 					}
// 					x[i] = 1;
// 					t[k] = s[i];
// 					k++;
// 					i++;
// 				}
// 			} else {
// 				// while(i < j && s[i]%2 == s[j]%2) {
// 				// 	if(x[i] == 1) {
// 				// 		i++;
// 				// 	}
// 				// 	x[i] = 1;
// 				// 	t[k] = s[i];
// 				// 	i++;
// 				// 	k++;
// 				// }
// 				// i = j;
// 				j++;
// 				// while(j < n && s[i]%2 == s[j]%2) {
// 				// 	j++;
// 				// }
// 			}
// 			// if(flag == 0) {
// 			// 	if(s[i]%2 == 0) {
// 			// 		j++;
// 			// 	} else {

// 			// 	}
// 			// } else {
// 			// 	if(s[i]%2 == 0) {

// 			// 	} else {
// 			// 		j++;
// 			// 	}
// 			// }
// 		}
// 		while(k < n) {
// 			if(x[i] == 1) {
// 				i++;
// 			}
// 			x[i] = 1;
// 			t[k] = s[i];
// 			i++;
// 			k++;
// 		}
// 		t[n] = '\0';
// 		printf("%s\n", t);
// 	}
// 	return 0;
// }

int main() {
	int T;
	sc(T);
	char s[300300], res[300300];
	while(T--) {
		sc3(s);
		int n = strlen(s);
		vii x;
		int prev = 0;
		mse(res, 0);
		fi(i, 0, n) {
			if(s[i]%2 == s[prev]%2) {
				continue;
			} else {
				x.pb({prev, i-1});
				prev = i;
			}
		}
		x.pb({prev, n-1});
		int a = 0, b = 0, k = 0;
		while(k < n) {
			// trace(k, a, b, x[a].ff, x[a].ss, x[b].ff, x[b].ss, s[x[a].ff], s[x[b].ff]);
			if(b == x.size()) {
				if(x[a].ff > x[a].ss) {
					a++;
					continue;
				}
				fi(i, x[a].ff, x[a].ss+1) {
					res[k] = s[i];
					k++;
				}
				a += 2;
				continue;
			}
			if(x[a].ff > x[a].ss) {
				a++;
				continue;
			}
			if(x[b].ff > x[b].ss) {
				b++;
				continue;
			}
			if(a%2 == b%2) {
				b++;
			} else {
				if(s[x[b].ff] < s[x[a].ff]) {
					res[k++] = s[x[b].ff];
					x[b].ff++;
				} else {
					res[k++] = s[x[a].ff];
					x[a].ff++;
				}
			}
		}
		printf("%s\n", res);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<ll, ll> pll;
typedef vector<pll> vll;
typedef pair<int, ii> tri;
typedef vector<tri> viii;

#define fi(i,a,b) for(auto i=a;i<b;i++)
#define trav(i,v) for(auto &i: v)
#define fd(i,a,b) for(auto i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mt make_tuple
#define ss second
#define ff first
#define sz(a) a.size()
#define sc(x) scanf("%d", &x)
#define sc2(x, y) scanf("%d %d", &x, &y)
#define sc3(x) scanf("%s", x)
#define sc4(x) scanf("%lld", &x)
#define sc5(x, y) scanf("%lld %lld", &x, &y)
#define sc6(x) scanf("%lf", &x)
#define sc7(x, y) scanf("%lf %lf", &x, &y)
#define mse(a, x) memset(a, x, sizeof(a))
#define mod 1000000007

#define TRACE
#ifdef TRACE
	#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1){
		cerr << name << ": " << arg1 << std::endl;
	}

	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args){
		const char* comma = strchr(names + 1, ',');
		cerr.write(names, comma - names) << ": " << arg1<<" |";
		__f(comma+1, args...);
	}
#else
	#define trace(...) ;
#endif

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

int main() {
	int T;
	sc(T);
	char s[300300], t[300300];
	while(T--) {
		sc3(s);
		int n = strlen(s);
		int i = 0, j = 0, k = 0, flag;
		if(s[0]%2 == 0) {
			flag = 0;
		} else {
			flag = 1;
		}
		while(i < n && j < n) {
			// trace(i, j, k, s[i], s[j]);
			if(s[i]%2 != s[j]%2) {
				if(s[i] > s[j]) {
					t[k] = s[j];
					k++;
					j++;
				} else {
					t[k] = s[i];
					k++;
					i++;
				}
			} else {
				while(i < j && s[i]%2 == s[j]%2) {
					t[k] = s[i];
					i++;
					k++;
				}
				i = j;
				while(j < n && s[i]%2 == s[j]%2) {
					j++;
				}
			}
			// if(flag == 0) {
			// 	if(s[i]%2 == 0) {
			// 		j++;
			// 	} else {

			// 	}
			// } else {
			// 	if(s[i]%2 == 0) {

			// 	} else {
			// 		j++;
			// 	}
			// }
		}
		while(k < n) {
			t[k] = s[i];
			i++;
			k++;
		}
		t[n] = '\0';
		printf("%s\n", t);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<ll, ll> pll;
typedef vector<pll> vll;
typedef pair<int, ii> tri;
typedef vector<tri> viii;

#define fi(i,a,b) for(auto i=a;i<b;i++)
#define trav(i,v) for(auto &i: v)
#define fd(i,a,b) for(auto i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mt make_tuple
#define ss second
#define ff first
#define sz(a) a.size()
#define sc(x) scanf("%d", &x)
#define sc2(x, y) scanf("%d %d", &x, &y)
#define sc3(x) scanf("%s", x)
#define sc4(x) scanf("%lld", &x)
#define sc5(x, y) scanf("%lld %lld", &x, &y)
#define sc6(x) scanf("%lf", &x)
#define sc7(x, y) scanf("%lf %lf", &x, &y)
#define mse(a, x) memset(a, x, sizeof(a))
#define mod 1000000007

#define TRACE
#ifdef TRACE
	#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1){
		cerr << name << ": " << arg1 << std::endl;
	}

	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args){
		const char* comma = strchr(names + 1, ',');
		cerr.write(names, comma - names) << ": " << arg1<<" |";
		__f(comma+1, args...);
	}
#else
	#define trace(...) ;
#endif

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

// int main() {
// 	int T;
// 	sc(T);
// 	char s[300300], t[300300], x[300300];
// 	while(T--) {
// 		sc3(s);
// 		mse(x, 0);
// 		int n = strlen(s);
// 		int i = 0, j = 0, k = 0, flag;
// 		if(s[0]%2 == 0) {
// 			flag = 0;
// 		} else {
// 			flag = 1;
// 		}
// 		while(i < n && j < n) {
// 			// trace(i, j, k, s[i], s[j]);
// 			if(s[i]%2 != s[j]%2) {
// 				if(s[i] > s[j]) {
// 					if(x[j] == 1) {
// 						j++;
// 					}
// 					x[j] = 1;
// 					t[k] = s[j];
// 					k++;
// 					j++;
// 				} else {
// 					if(x[i] == 1) {
// 						i++;
// 					}
// 					x[i] = 1;
// 					t[k] = s[i];
// 					k++;
// 					i++;
// 				}
// 			} else {
// 				// while(i < j && s[i]%2 == s[j]%2) {
// 				// 	if(x[i] == 1) {
// 				// 		i++;
// 				// 	}
// 				// 	x[i] = 1;
// 				// 	t[k] = s[i];
// 				// 	i++;
// 				// 	k++;
// 				// }
// 				// i = j;
// 				j++;
// 				// while(j < n && s[i]%2 == s[j]%2) {
// 				// 	j++;
// 				// }
// 			}
// 			// if(flag == 0) {
// 			// 	if(s[i]%2 == 0) {
// 			// 		j++;
// 			// 	} else {

// 			// 	}
// 			// } else {
// 			// 	if(s[i]%2 == 0) {

// 			// 	} else {
// 			// 		j++;
// 			// 	}
// 			// }
// 		}
// 		while(k < n) {
// 			if(x[i] == 1) {
// 				i++;
// 			}
// 			x[i] = 1;
// 			t[k] = s[i];
// 			i++;
// 			k++;
// 		}
// 		t[n] = '\0';
// 		printf("%s\n", t);
// 	}
// 	return 0;
// }

int main() {
	int T;
	sc(T);
	char s[300300], res[300300];
	while(T--) {
		sc3(s);
		int n = strlen(s);
		vii x;
		int prev = 0;
		mse(res, 0);
		fi(i, 0, n) {
			if(s[i]%2 == s[prev]%2) {
				continue;
			} else {
				x.pb({prev, i-1});
				prev = i;
			}
		}
		x.pb({prev, n-1});
		int a = 0, b = 0, k = 0;
		while(k < n) {
			// trace(k, a, b, x[a].ff, x[a].ss, x[b].ff, x[b].ss, s[x[a].ff], s[x[b].ff]);
			if(x[b].ff > x[b].ss) {
				b++;
				continue;
			}
			if(x[a].ff > x[a].ss) {
				a++;
				continue;
			}
			if(b == x.size()) {
				fi(i, x[a].ff, x[a].ss+1) {
					res[k] = s[i];
					k++;
				}
				a += 2;
				continue;
			}
			if(a%2 == b%2) {
				b++;
			} else {
				if(s[x[b].ff] < s[x[a].ff]) {
					res[k++] = s[x[b].ff];
					x[b].ff++;
				} else {
					res[k++] = s[x[a].ff];
					x[a].ff++;
				}
			}
		}
		printf("%s\n", res);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef pair<ll, ll> pll;
typedef vector<pll> vll;
typedef pair<int, ii> tri;
typedef vector<tri> viii;

#define fi(i,a,b) for(auto i=a;i<b;i++)
#define trav(i,v) for(auto &i: v)
#define fd(i,a,b) for(auto i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mt make_tuple
#define ss second
#define ff first
#define sz(a) a.size()
#define sc(x) scanf("%d", &x)
#define sc2(x, y) scanf("%d %d", &x, &y)
#define sc3(x) scanf("%s", x)
#define sc4(x) scanf("%lld", &x)
#define sc5(x, y) scanf("%lld %lld", &x, &y)
#define sc6(x) scanf("%lf", &x)
#define sc7(x, y) scanf("%lf %lf", &x, &y)
#define mse(a, x) memset(a, x, sizeof(a))
#define mod 1000000007

#define TRACE
#ifdef TRACE
	#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)

	template <typename Arg1>
	void __f(const char* name, Arg1&& arg1){
		cerr << name << ": " << arg1 << std::endl;
	}

	template <typename Arg1, typename... Args>
	void __f(const char* names, Arg1&& arg1, Args&&... args){
		const char* comma = strchr(names + 1, ',');
		cerr.write(names, comma - names) << ": " << arg1<<" |";
		__f(comma+1, args...);
	}
#else
	#define trace(...) ;
#endif

#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

int main() {
	int T;
	sc(T);
	char s[300300], t[300300], x[300300];
	while(T--) {
		sc3(s);
		mse(x, 0);
		int n = strlen(s);
		int i = 0, j = 0, k = 0, flag;
		if(s[0]%2 == 0) {
			flag = 0;
		} else {
			flag = 1;
		}
		while(i < n && j < n) {
			// trace(i, j, k, s[i], s[j]);
			if(s[i]%2 != s[j]%2) {
				if(s[i] > s[j]) {
					if(x[j] == 1) {
						j++;
					}
					x[j] = 1;
					t[k] = s[j];
					k++;
					j++;
				} else {
					if(x[i] == 1) {
						i++;
					}
					x[i] = 1;
					t[k] = s[i];
					k++;
					i++;
				}
			} else {
				while(i < j && s[i]%2 == s[j]%2) {
					if(x[i] == 1) {
						i++;
					}
					x[i] = 1;
					t[k] = s[i];
					i++;
					k++;
				}
				i = j;
				while(j < n && s[i]%2 == s[j]%2) {
					j++;
				}
			}
			// if(flag == 0) {
			// 	if(s[i]%2 == 0) {
			// 		j++;
			// 	} else {

			// 	}
			// } else {
			// 	if(s[i]%2 == 0) {

			// 	} else {
			// 		j++;
			// 	}
			// }
		}
		while(k < n) {
			if(x[i] == 1) {
				i++;
			}
			x[i] = 1;
			t[k] = s[i];
			i++;
			k++;
		}
		t[n] = '\0';
		printf("%s\n", t);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define bp push_back
#define mp make_pair
const int N=3e5+10;
const double eps=1e-9;
const int inf=1e9;
vector<int>v[2];
char a[N];
main(){

    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",a);
        int n=strlen(a);
        v[0].clear();
        v[1].clear();
        for(int i=1;i<n;i++){
            int c=a[i]-'0',c1=a[i-1]-'0';
            if(c<c1 && (abs(c-c1)&1))swap(a[i],a[i-1]);
        }
        for(int i=0;i<n;i++){
            int c=a[i]-'0';
            v[c&1].bp(i);
        }
        for(int i=0;i<n;i++){
            int c=a[i]-'0',c1=a[i-1]-'0';
            if(c1>c && abs(c-c1)&1){
                if(c&1){
                    int lo=lower_bound(v[0].begin(),v[0].end(),i)-v[0].begin();
                    --lo;
                    while(lo>1 && v[0][lo-1]+1==v[0][lo])--lo;
                    ++lo;
                    swap(a[i],a[lo]);
                }
                else{
                    int lo=lower_bound(v[1].begin(),v[1].end(),i)-v[1].begin();
                    --lo;
                    while(lo>1 && v[1][lo-1]+1==v[1][lo])--lo;
                    ++lo;
                    swap(a[i],a[lo]);
                }
            }
        }
        printf("%s\n",a);
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define bp push_back
#define mp make_pair
const int N=3e5+10;
const double eps=1e-9;
const int inf=1e9;
char a[N];
main(){

    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",a);
        int n=strlen(a);
        vector<int>ans;
        bool ok=0;
        int j=0;
        for(int i=0;i<n;i++){
            ok=0;
            if(a[i]=='?')continue;
            int c=a[i]-'0';
            for(j;j<n;j++){
                if(a[j]=='?')continue;
                int c2=a[j]-'0';
                if(c%2 != c2%2 && c2>c)break;
                if(c%2 == c2%2)continue;
                if(c2<c){
                    ans.bp(c2);
                    a[j]='?';
                    --i;
                    ok=1;
                    break;
                }
            }
            if(!ok)ans.bp(a[i]-'0');
        }
        for(auto x:ans)printf("%d",x);
        printf("\n");
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define bp push_back
#define mp make_pair
const int N=3e5+10;
const double eps=1e-9;
const int inf=1e9;
char a[N];
main(){

    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",a);
        int n=strlen(a);
        vector<int>ans;
        bool ok=0;
        for(int i=0;i<n;i++){
            ok=0;
            if(a[i]=='?')continue;
            int c=a[i]-'0';
            for(int j=i+1;j<n;j++){
                if(a[j]=='?')continue;
                int c2=a[j]-'0';
                if(c%2 != c2%2 && c2>c)break;
                if(c%2 == c2%2)continue;

                if(c2<c){
                    ans.bp(c2);
                    a[j]='?';
                    --i;
                    ok=1;
                    break;
                }
            }
            if(!ok)ans.bp(a[i]-'0');
        }
        for(auto x:ans)printf("%d",x);
        printf("\n");
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define bp push_back
#define mp make_pair
const int N=3e5+10;
const double eps=1e-9;
const int inf=1e9;
vector<int>v[2];
char a[N];
main(){

    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",a);
        int n=strlen(a);
        v[0].clear();
        v[1].clear();

        for(int i=0;i<n;i++){
            int c=a[i]-'0';
            v[c%2].bp(i);
        }
        vector<pair<int,int> >vv;
        for(int i=1;i<n;i++){
            int c=a[i]-'0',c1=a[i-1]-'0';
            if(abs(c+c1)&1){
                if(c&1){
                    int lo=i-1;
                    int pos=-1;
                    while(lo>-1 && (a[lo]-'0')%2==0  ){
                        if(a[i]<a[lo])pos=lo;
                        --lo;
                    }
                    if(pos!=-1){
                        lo=i-1;
                        while(lo>=pos ){
                            swap(a[lo],a[lo+1]);
                            --lo;
                        }
                    }
                }
                else{
                    int lo=i-1,pos=-1;
                    while(lo>-1 && (a[lo]-'0')&1){
                        if(a[i]<a[lo])pos=lo;
                        --lo;
                    }

                    if(pos!=-1){
                        lo=i-1;
                        while(lo>=pos){
                            swap(a[lo],a[lo+1]);
                            --lo;
                        }
                    }
                }
            }
        }
        for(auto x:vv)cout<<x.f<<" "<<x.s<<endl;
        int pos=0;
        for(int i=0;i<n;i++){
            if(a[i]=='a')continue;
            if(pos<vv.size() && i==vv[pos].s){
                cout<<a[vv[pos].f];
                --i;
                a[vv[pos].f]='a';
                pos++;
            }
            else cout<<a[i];
        }
        cout<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define bp push_back
#define mp make_pair
const int N=3e5+10;
const double eps=1e-9;
const int inf=1e9;
vector<int>v[2];
char a[N];
main(){

    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",a);
        int n=strlen(a);
        v[0].clear();
        v[1].clear();

        for(int i=0;i<n;i++){
            int c=a[i]-'0';
            v[c%2].bp(i);
        }
        vector<pair<int,int> >vv;
        for(int i=1;i<n;i++){
            int c=a[i]-'0',c1=a[i-1]-'0';
            if(c1>c && abs(c+c1)&1){
                if(c&1){
                    int lo=i-1;
                    int tmp=lo;
                    while(lo>1 && v[0][lo-1]+1==v[0][lo] && a[i]<a[lo-1])--lo;
                    vv.bp({i,lo});
                }
                else{
                    int lo=i-1;
                    while(lo>1 && v[1][lo-1]+1==v[1][lo] && a[i]<a[lo-1])--lo;
                    vv.bp({i,lo});
                }
            }
        }
        int pos=0;
        for(int i=0;i<n;i++){
            if(a[i]=='a')continue;
            if(pos<vv.size() && i==vv[pos].s){
                cout<<a[vv[pos].f];
                --i;
                a[vv[pos].f]='a';
                pos++;
            }
            else cout<<a[i];
        }
        cout<<endl;
    }

}


#include <bits/stdc++.h>
using namespace std;

string ab(string s, string r, int i, int n) {
    if (i == n)
        return r;
    else if (i == 0)
        return ab(s, "" + s[0], 1, n);
    else if (i < n) {
        char d = s[i];
        if (d%2 == r[i-1]%2) {
            return ab(s, r + d, i + 1, n);
        }
        else {
            int j = i - 1;
            int pos = 1e6;
            while(r[j]%2 != d%2) {
                if (r[j] > d)
                    pos = j;
                j--;
            }
            if (pos != 1e6) {
                r = r.substr(0, pos) + d + r.substr(pos);
                return ab(s, r, i + 1, n);
            }
            else {
                return ab(s, r + d, i + 1, n);
            }

        }
    }

}

int main() {
    int q;
    cin >> q;
    while(q--) {
        string s;
        cin >> s;
        cout << ab(s, "", 0, s.size()) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

string ab(string s, string r, int i, int n) {
    if (i == n)
        return r;
    else if (i == 0)
        return ab(s, r + s[0], 1, n);
    else if (i < n) {
        char d = s[i];
        if (d%2 == r[i-1]%2) {
            return ab(s, r + d, i + 1, n);
        }
        else {
            int j = i - 1;
            int pos = 1e6;
            while(r[j]%2 != d%2) {
                if (r[j] > d)
                    pos = j;
                j--;
            }
            if (pos != 1e6) {
                r = r.substr(0, pos) + d + r.substr(pos);
                return ab(s, r, i + 1, n);
            }
            else {
                return ab(s, r + d, i + 1, n);
            }

        }
    }

}

int main() {
    int q;
    cin >> q;
    while(q--) {
        string s;
        cin >> s;
        cout << ab(s, "", 0, s.size()) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

string ss(string s) {
    int n  = s.size();
    int i, j;
    i = j = 0;
    for (i = 0; i < n; i=j+1) {
        int d = s[i] % 2;
        for (j = i + 1; j < n && s[j]%2 == d; j++) {}
        if (j == n)
            return s;
        else {
            for (int k = i; k < j; k++) {
                if (s[k] > s[j]) {
                    s = s.substr(0, k) + s[j] + s.substr(k, j - k) + s.substr(j+1);
                    break;
                }
            }
        }
    }
    return s;
}
int main() {
    string s;
    cin >> s;
    cout << ss(s) << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

string ab(string s, char r) {
    int n = s.size();
    s = s + r;
    if (n == 0)
        return s;
    if (r%2 != s[n - 1]%2) {
        int i = n - 1;
        int pos = -1;
        while(s[i]%2 != r%2) {
            if (s[i] > r)
                pos = i;
            i--;
        }

        if (pos != -1) {
            for (int j = n; j > pos && j > 0; --j) {
                swap(s[j], s[j - 1]);
            }
        }
    }
    return s;
}

void rs(string s) {
    string r = "";
    for (char d : s) {
        r = ab(r, d);
    }
    cout << r << endl;
}
int main() {
    int q;
    cin >> q;
    while(q--) {
        string s;
        cin >> s;
        rs(s);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

string ss(string s) {
    int n  = s.size();
    int i, j;
    i = j = 0;
    for (i = 0; i < n; i=j) {
        int d = s[i] % 2;
        for (j = i; j < n && s[j]%2 == d; j++) {}
        if (j == n)
            return s;
        else {
            for (int k = i; k < j; k++) {
                if (s[k] > s[j]) {
                    s = s.substr(0, k) + s[j] + s.substr(k, j - k) + s.substr(j+1);
                    break;
                }
            }
        }
    }
    return s;
}
int main() {
    int t;
    cin >> t;
    while(t--) {
    string s;
    cin >> s;
    cout << ss(s) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

string ab(string s, string r, int i, int n) {
    if (i == n)
        return r;
    else if (i == 0) {
        return ab(s, r + s[0], 1, n);
    }
    else if (i < n) {
        char d = s[i];
        if (d%2 == r[i-1]%2) {
            return ab(s, r + d, i + 1, n);
        }
        else {
            int j = i - 1;
            int pos = 1e6;
            while(r[j]%2 != d%2) {
                if (r[j] > d)
                    pos = j;
                j--;
            }
            if (pos != 1e6) {
                r = r.substr(0, pos) + d + r.substr(pos);
                return ab(s, r, i + 1, n);
            }
            else {
                return ab(s, r + d, i + 1, n);
            }

        }
    }

}

int main() {
    int q;
    cin >> q;
    while(q--) {
        string s;
        cin >> s;
        string r = "";
        cout << ab(s, r, 0, s.size()) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
void ab(string s) {
    int n = s.size();
    vector<int> l, t;
    int m = s[0];
    for (int i = 0; i < n - 1; i++) {
        if (s[i]%2 != s[i + 1]%2) {
            l.push_back(i + 1);
            t.push_back(m);
            m = s[i + 1];
        }
        else {
            m = max(m, int(s[i + 1]));
        }
    }

    n = l.size();
    int i;
    for (i = 0; i < n; i++) {
        if (s[l[i]] < t[i])
            break;
    }
    int j;
    int w = 0;
    if (n > 0)
        w = l[i];
    for (j = 0; j < w; j++) {
        if (s[j] > s[w]) {
            string r = s.substr(0, j) + s[w] + s.substr(j, w - j) + s.substr(w + 1);
            s = r;
            break;
        }
    }

    cout << s << endl;
}
int main() {
    int q;
    cin >> q;
    while(q--) {
        string s;
        cin >> s;
        ab(s);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

string ab(string s, string r, int i, int n) {
    if (i == n)
        return r;
    if (i == 0)
        return ab(s, r + s[0], 1, n);
    if (i < n) {
        char d = s[i];
        if (d%2 == r[i-1]%2) {
            return ab(s, r + d, i + 1, n);
        }
        else {
            int j = i - 1;
            int pos = 1e6;
            while(r[j]%2 != d%2) {
                if (r[j] > d)
                    pos = j;
                j--;
            }
            if (pos != 1e6) {
                r = r.substr(0, pos) + d + r.substr(pos);
                return ab(s, r, i + 1, n);
            }
            else {
                return ab(s, r + d, i + 1, n);
            }

        }
    }

}

int main() {
    int q;
    cin >> q;
    while(q--) {
        string s;
        cin >> s;
        cout << ab(s, "", 0, s.size()) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

string ab(string s, string r, int i, int n) {
    if (i == n)
        return r;
    if (i == 0)
        return ab(s, r + s[0], 1, n);
    if (i < n) {
        char d = s[i];
        if (d%2 == r[i-1]%2)
            return ab(s, r + d, i + 1, n);
        else {
            r = r + d;
            int j = i - 1;
            int pos = i;
            while(r[j]%2 != d%2) {
                if (r[j] > d)
                    pos = j;
                j--;
            }
            for (int k = i; k > pos && k > 0; --k)
                swap(r[k], r[k - 1]);

            return ab(s, r, i + 1, n);
        }
    }

}

int main() {
    int q;
    cin >> q;
    while(q--) {
        string s;
        cin >> s;
        cout << ab(s, "", 0, s.size()) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

string ss(string s) {
    int n  = s.size();
    int i, j;
    i = j = 0;
    for (i = 0; i < n; i=j) {
        int d = s[i] % 2;
        for (j = i + 1; j < n && s[j]%2 == d; j++) {}
        if (j == n)
            return s;
        else {
            for (int k = i; k < j; k++) {
                if (s[k] > s[j]) {
                    s = s.substr(0, k) + s[j] + s.substr(k, j - k) + s.substr(j+1);
                    break;
                }
            }
        }
    }
    return s;
}
int main() {
    int t;
    cin >> t;
    while(t--) {
    string s;
    cin >> s;
    cout << ss(s) << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
void ab(string s) {
    int n = s.size();
    vector<int> l, t;
    int m = s[0];
    for (int i = 0; i < n - 1; i++) {
        if (s[i]%2 != s[i + 1]%2) {
            l.push_back(i + 1);
            t.push_back(m);
            m = s[i + 1];
        }
        else {
            m = max(m, int(s[i + 1]));
        }
    }

    n = l.size();
    int i;
    for (i = 0; i < n; i++) {
        if (s[l[i]] < t[i])
            break;
    }
    int j;
    i = l[i];
    for (j = 0; n > 0 && j < i; j++) {
        if (s[j] > s[i]) {
            string r = s.substr(0, j) + s[i] + s.substr(j, i - j) + s.substr(i + 1);
            s = r;
            break;
        }
    }

    cout << s << endl;
}
int main() {
    int q;
    cin >> q;
    while(q--) {
        string s;
        cin >> s;
        ab(s);
    }
    return 0;
}


#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define F first
#define S second
#define vi vector
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define mod 1000000007
#define pb push_back
#define ll long long int
#define db double
#define all(v) v.begin(),v.end()

using namespace std;

/*

ll expo(ll n){
    ll t;
    if(n==0)return 1;
    else if(n==1) return 2;
    t=expo(n/2);
    if(n%2){
        return t*t*2;
    }
    else return t*t;
}

//ffrom geek for geek
ll exponentMod(ll A, ll B, ll C)
{
    if (A == 0)
        return 0;
    if (B == 0)
        return 1;
    ll y;
    if (B % 2 == 0) {
        y = exponentMod(A, B / 2, C);
        y = (y * y) % C;
    }
    else {
        y = A % C;
        y = (y * exponentMod(A, B - 1, C) % C) % C;
    }

    return (ll)((y + C) % C);
}

*/
/*
class dsu{
public:
vi<ll> v;

void init(ll n){
    v.assign(n+1,-1);
}

ll dsufind(ll x){
    if(v[x]<0){
        return x;
    }
    v[x]=dsufind(v[x]);
    return v[x];
}

void dsujoin(ll x,ll y){
    x=dsufind(x);
    y=dsufind(y);

    if(x==y)
    return;
    if(v[x]>v[y])swap(x,y);
    v[y]+=v[x];
    v[x]=y;
}

};
*/

int main(){
   IOS;
    ll t;
    t=1;
    //cin>>t;

    while(t--){
       ll n;
       cin>>n;
       for(int i=n*n;i>n*n-3;i--){
           ll p=i;
           for(int j=0;j<3;j++){
               cout<<p<<" ";
               p-=3;
           }
           cout<<"\n";
       }

    }
    return 0;
}


#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define F first
#define S second
#define vi vector
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define mod 1000000007
#define pb push_back
#define ll long long int
#define db double
#define all(v) v.begin(),v.end()

using namespace std;

/*

ll expo(ll n){
    ll t;
    if(n==0)return 1;
    else if(n==1) return 2;
    t=expo(n/2);
    if(n%2){
        return t*t*2;
    }
    else return t*t;
}

//ffrom geek for geek
ll exponentMod(ll A, ll B, ll C)
{
    if (A == 0)
        return 0;
    if (B == 0)
        return 1;
    ll y;
    if (B % 2 == 0) {
        y = exponentMod(A, B / 2, C);
        y = (y * y) % C;
    }
    else {
        y = A % C;
        y = (y * exponentMod(A, B - 1, C) % C) % C;
    }

    return (ll)((y + C) % C);
}

*/
/*
class dsu{
public:
vi<ll> v;

void init(ll n){
    v.assign(n+1,-1);
}

ll dsufind(ll x){
    if(v[x]<0){
        return x;
    }
    v[x]=dsufind(v[x]);
    return v[x];
}

void dsujoin(ll x,ll y){
    x=dsufind(x);
    y=dsufind(y);

    if(x==y)
    return;
    if(v[x]>v[y])swap(x,y);
    v[y]+=v[x];
    v[x]=y;
}

};
*/

int main(){
   IOS;
    ll t;
    t=1;
    //cin>>t;

    while(t--){
       ll n;
       cin>>n;
       ll a[n+1][n+1];
       ll k=n*n;
       for(int i=0;i<n;i++){
           for(int j=0;j<n;j++){
               a[i][j]=k;
               k--;
           }
       }

       for(int j=0;j<n;j++){
               for(int i=0;i<n/2;i++)
                   cout<<a[i][j]<<" ";
                for(int i=n/2;i<n;i++){
                    cout<<a[i][n-j-1]<<" ";
                }
           cout<<"\n";
       }

    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (j % 2 == 0) {
                cout << j * n + 1 + i << " ";
            } else {
                cout << j * n + n - i << " ";
            }
        }
        cout << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cout << j * n + 1 + (i + j) % n << " ";
        }
        cout << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<vector<int> > asdf;
for(int i=1; i<=n; i++){
    vector<int> vi;
    if(i%2==1){
        for(int j=1; j<=n; j++)
            vi.push_back(j+(i-1)*n);
    }
    else{
        for(int j=n; j>=1; j--)
            vi.push_back(j+(i-1)*n);
    }
    asdf.push_back(vi);
}
for(int i=0; i<asdf[0].size(); i++){
    for(int j=0; j<asdf.size(); j++){
        cout<<asdf[j][i]<<" ";
    }
    cout<<'\n';
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=1; i<=n; i++){
    for(int j=i; j<=n*n; j+=n)
        cout<<j<<" ";
    cout<<'\n';
}
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<vector<int> > asdf;
for(int i=1; i<=n; i++){
    vector<int> vi;
    for(int j=i+(n*(i-1)); j<=n*i; j++)
        vi.push_back(j);
    for(int j=n*(i-1)+1; j<i+(n*(i-1)); j++)
        vi.push_back(j);
    asdf.push_back(vi);
}
for(int i=0; i<asdf[0].size(); i++){
    for(int j=0; j<asdf.size(); j++)
        cout<<asdf[j][i]<<" ";
    cout<<'\n';
}
return 0;
}


// #pragma GCC optimize("O3")
// #pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

#define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define __FOR3(i, a, n, inc) for(int i = (a); (inc) > 0 ? i < (n) : i >= (n); i += (inc))
#define __FOR2(i, a, n) __FOR3(i, a, n, 1)
#define __FOR1(i, n) __FOR2(i, 0, n)
#define FOR(...) GET_MACRO(__VA_ARGS__, __FOR3, __FOR2, __FOR1)(__VA_ARGS__)

#define BOOST 			ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ELO             exit(0)
#define ALL(a) 			(a).begin(), (a).end()
#define SZ(a) 			(int)(a).size()
#define TRAV(a, b) 		for (auto& a : b)
#define REV(a)          reverse(ALL(a))
#define SORT(a)         sort(ALL(a))
#define SORTR(a)        SORT(a), REV(a)
#define PB              push_back
#define F               first
#define S               second
using ll = 				long long;
using ld = 				long double; // 'long double' is 2.2 times slower than double
using pii = 			pair<int, int>;
using pll = 			pair<ll, ll>;
using pld =             pair<ld, ld>;
using vi = 				vector<int>;
using vvi =             vector<vi>;
using vll = 			vector<ll>;
using vvll =            vector<vll>;
using vpii = 			vector<pii>;
using vpll = 			vector<pll>;
using vld =             vector<ld>;
using vpld =            vector<pld>;

constexpr int MOD = 1e9 + 7; // 998244353
constexpr int INF = MOD;
constexpr ll LLINF = 1e18;
const ld PI = acos(-1);

template <class T> inline bool setmin(T& a, T b) {if (a > b) return a = b, 1; return 0; }
template <class T> inline bool setmax(T& a, T b) {if (a < b) return a = b, 1; return 0; }

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <class key, class cmp = less<key>> using ordered_set = tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template <class key, class cmp = less_equal<key>> using ordered_multiset = tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
//* find_by_order(k) - returns an iterator to the k-th largest element (counting from zero)
//* order_of_key(k)  - the number of items in a set that are strictly smaller than k.
template <class key, class value, class cmp = less<key>> using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
using pref_trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>;
// * prefix_range(s) - return iterator range, prefix equal to s

namespace input {
    template <class T> istream &operator>>(istream &os, vector<T> & vec) { for (auto& u : vec) os >> u; return os; }
    template<class T1, class T2> istream& operator>>(istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; }
    template <class T, size_t N> istream& operator>>(istream& os, array<T, N>& a) { for (auto& u : a) os >> u; return os; }

    void re() {}
    template <typename T, typename... args> void re(T& x, args&... tail) { cin >> x; re(tail...); }
} // namespace input
using namespace input;

namespace output {
    template <class T> ostream &operator<<(ostream &os, const vector<T> & vec){for (const auto& u : vec) os << u << " "; return os; }
    template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { return os << p.first << " " << p.second; }
    template <class T> ostream &operator<<(ostream &os, set<T> & con) { for (const auto& u : con) os << u << " "; return os; }
    template <class T, size_t N> ostream& operator<<(ostream& os, array<T, N>& a) { for (const auto& u : a) os << u << " "; return os; }

    void pr() {}
    template <typename T> void pr(const T& x) { cout << x; }
    template <typename T, typename... args> void pr(const T& x, const args&... tail) { cout << x << " "; pr(tail...);}
    template <typename T> void prs(const T& x) { cout << x << " "; }
    template <typename T, typename... args> void prs(const T& x, const args&... tail) { cout << x << " "; prs(tail...);}
    template <typename... args> void prln(const args&... tail) { pr(tail...); cout << "\n";}
} // namespace output
using namespace output;

namespace pair_magic {
	#define PT1T2 pair<T1,T2>
	#define TT1T2 template<class T1, class T2>
	TT1T2 inline PT1T2 operator+(const PT1T2 &p1, const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }
	TT1T2 inline PT1T2& operator+=(PT1T2 &p1, const PT1T2 &p2) { p1.first += p2.first, p1.second += p2.second; return p1; }
	TT1T2 inline PT1T2 operator-(const PT1T2 &p1, const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }
	TT1T2 inline PT1T2& operator-=(PT1T2 &p1, const PT1T2 &p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; }
	#undef PT1T2
	#undef TT1T2
}
using namespace pair_magic;

namespace random_nsp {
	thread_local std::mt19937 gen{std::random_device{}()};
	template <typename T>T random(T min, T max){using dist = std::conditional_t<std::is_integral<T>::value, std::uniform_int_distribution<T>, std::uniform_real_distribution<T>>;return dist{min, max}(gen);}
	template <typename T> T randomINT(T min, T max) { return std::uniform_int_distribution<T>{min, max}(gen); }
}
using namespace random_nsp;

namespace triple {
	#define TT1T2T3 template<class T1, class T2, class T3>
	#define TRT1T2T3  triple<T1, T2, T3>
	TT1T2T3 struct triple { T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c) {} };
	TT1T2T3 bool operator<(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.a != t2.a)return t1.a < t2.a; else if (t1.b != t2.b)return t1.b < t2.b; else return t1.c < t2.c; }
	TT1T2T3 bool operator>(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.a != t2.a)return t1.a > t2.a; else if (t1.b != t2.b)return t1.b > t2.b; else return t1.c > t2.c; }
	TT1T2T3 bool operator==(const TRT1T2T3&t1, const TRT1T2T3&t2) { return (t1.a == t2.a && t1.b == t2.b && t1.c == t2.c); }
	TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; }
	TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t) { return os << t.a << " " << t.b << " " << t.c; }
	#undef TRT1T2T3
	#undef TT1T2T3
	using tri =  triple<int, int, int>;
	using trll = triple<ll, ll, ll>;
	using vtri = vector<tri>;
    using vtrll = vector<trll>;
}
using namespace triple;

template <class T> T invGeneral(T a, T b) { // 0 < a < b, gcd(a,b) = 1
    a %= b;
    if (a == 0) return b == 1 ? 0 : -1;
    T x = invGeneral(b, a);
    return x == -1 ? -1 : ((1-(ll)b * x) / a + b) % b;
}

template<class T, int MOD> struct modular {
	T val;
	explicit operator T() const { return val; }
	modular() { val = 0; }
	modular(const ll& v) {
		val = (-MOD <= v && v <= MOD) ? v : v % MOD;
		if (val < 0) val += MOD;
	}

	friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
	friend void pr(const modular& a) { pr(a.val); }
	friend void re(modular& a) { ll x; re(x); a = modular(x); }

	friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
	friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }
	friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; }

	modular operator-() const { return modular(-val); }
	modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
	modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
	modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }
	friend modular pow(modular a, ll p) {
		modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
		return ans;
	}
	friend modular inv(const modular& a) {
		auto i = invGeneral(a.val,MOD); assert(i != -1);
		return i;
	} // equivalent to return exp(b,MOD-2) if MOD is prime
	modular& operator/=(const modular& m) { return (*this) *= inv(m); }

	friend modular operator+(modular a, const modular& b) { return a += b; }
	friend modular operator-(modular a, const modular& b) { return a -= b; }
	friend modular operator*(modular a, const modular& b) { return a *= b; }
	friend modular operator/(modular a, const modular& b) { return a /= b; }
};

using mi = modular<int, MOD>;
using pmi = pair<mi,mi>;
using vmi = vector<mi>;
using vpmi = vector<pmi>;

namespace debug {
    template < typename _T > inline void _debug(const char *s, _T x) { cerr << s << " = " << x << "\n"; }
    template < typename _T, typename... args > void _debug(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _debug(s + 1, a...); }

    #if 1
        #define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__)
        #define cerr cout
    #else
        #define debug(...) 1999
        #define cerr if(0) cout
    #endif
} // namespace debug
using namespace debug;

//////////////_________CODE_________//////////////

int main() {
    BOOST;
    int n; re(n);

	vvi res(n);

	FOR(i, n) {
		FOR(j, n) {
			int id = i * n + j;
			int poz = (j + i) % n;
			res[poz].PB(id + 1);
		}
	}

	FOR(i, n) {
		prln(res[i]);
	}
}


// #pragma GCC optimize("O3")
// #pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

#define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define __FOR3(i, a, n, inc) for(int i = (a); (inc) > 0 ? i < (n) : i >= (n); i += (inc))
#define __FOR2(i, a, n) __FOR3(i, a, n, 1)
#define __FOR1(i, n) __FOR2(i, 0, n)
#define FOR(...) GET_MACRO(__VA_ARGS__, __FOR3, __FOR2, __FOR1)(__VA_ARGS__)

#define BOOST 			ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ELO             exit(0)
#define ALL(a) 			(a).begin(), (a).end()
#define SZ(a) 			(int)(a).size()
#define TRAV(a, b) 		for (auto& a : b)
#define REV(a)          reverse(ALL(a))
#define SORT(a)         sort(ALL(a))
#define SORTR(a)        SORT(a), REV(a)
#define PB              push_back
#define F               first
#define S               second
using ll = 				long long;
using ld = 				long double; // 'long double' is 2.2 times slower than double
using pii = 			pair<int, int>;
using pll = 			pair<ll, ll>;
using pld =             pair<ld, ld>;
using vi = 				vector<int>;
using vvi =             vector<vi>;
using vll = 			vector<ll>;
using vvll =            vector<vll>;
using vpii = 			vector<pii>;
using vpll = 			vector<pll>;
using vld =             vector<ld>;
using vpld =            vector<pld>;

constexpr int MOD = 1e9 + 7; // 998244353
constexpr int INF = MOD;
constexpr ll LLINF = 1e18;
const ld PI = acos(-1);

template <class T> inline bool setmin(T& a, T b) {if (a > b) return a = b, 1; return 0; }
template <class T> inline bool setmax(T& a, T b) {if (a < b) return a = b, 1; return 0; }

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template <class key, class cmp = less<key>> using ordered_set = tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template <class key, class cmp = less_equal<key>> using ordered_multiset = tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
//* find_by_order(k) - returns an iterator to the k-th largest element (counting from zero)
//* order_of_key(k)  - the number of items in a set that are strictly smaller than k.
template <class key, class value, class cmp = less<key>> using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
using pref_trie = trie<string, null_type, trie_string_access_traits<>, pat_trie_tag, trie_prefix_search_node_update>;
// * prefix_range(s) - return iterator range, prefix equal to s

namespace input {
    template <class T> istream &operator>>(istream &os, vector<T> & vec) { for (auto& u : vec) os >> u; return os; }
    template<class T1, class T2> istream& operator>>(istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; }
    template <class T, size_t N> istream& operator>>(istream& os, array<T, N>& a) { for (auto& u : a) os >> u; return os; }

    void re() {}
    template <typename T, typename... args> void re(T& x, args&... tail) { cin >> x; re(tail...); }
} // namespace input
using namespace input;

namespace output {
    template <class T> ostream &operator<<(ostream &os, const vector<T> & vec){for (const auto& u : vec) os << u << " "; return os; }
    template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1, T2>& p) { return os << p.first << " " << p.second; }
    template <class T> ostream &operator<<(ostream &os, set<T> & con) { for (const auto& u : con) os << u << " "; return os; }
    template <class T, size_t N> ostream& operator<<(ostream& os, array<T, N>& a) { for (const auto& u : a) os << u << " "; return os; }

    void pr() {}
    template <typename T> void pr(const T& x) { cout << x; }
    template <typename T, typename... args> void pr(const T& x, const args&... tail) { cout << x << " "; pr(tail...);}
    template <typename T> void prs(const T& x) { cout << x << " "; }
    template <typename T, typename... args> void prs(const T& x, const args&... tail) { cout << x << " "; prs(tail...);}
    template <typename... args> void prln(const args&... tail) { pr(tail...); cout << "\n";}
} // namespace output
using namespace output;

namespace pair_magic {
	#define PT1T2 pair<T1,T2>
	#define TT1T2 template<class T1, class T2>
	TT1T2 inline PT1T2 operator+(const PT1T2 &p1, const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }
	TT1T2 inline PT1T2& operator+=(PT1T2 &p1, const PT1T2 &p2) { p1.first += p2.first, p1.second += p2.second; return p1; }
	TT1T2 inline PT1T2 operator-(const PT1T2 &p1, const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }
	TT1T2 inline PT1T2& operator-=(PT1T2 &p1, const PT1T2 &p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; }
	#undef PT1T2
	#undef TT1T2
}
using namespace pair_magic;

namespace random_nsp {
	thread_local std::mt19937 gen{std::random_device{}()};
	template <typename T>T random(T min, T max){using dist = std::conditional_t<std::is_integral<T>::value, std::uniform_int_distribution<T>, std::uniform_real_distribution<T>>;return dist{min, max}(gen);}
	template <typename T> T randomINT(T min, T max) { return std::uniform_int_distribution<T>{min, max}(gen); }
}
using namespace random_nsp;

namespace triple {
	#define TT1T2T3 template<class T1, class T2, class T3>
	#define TRT1T2T3  triple<T1, T2, T3>
	TT1T2T3 struct triple { T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c) {} };
	TT1T2T3 bool operator<(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.a != t2.a)return t1.a < t2.a; else if (t1.b != t2.b)return t1.b < t2.b; else return t1.c < t2.c; }
	TT1T2T3 bool operator>(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.a != t2.a)return t1.a > t2.a; else if (t1.b != t2.b)return t1.b > t2.b; else return t1.c > t2.c; }
	TT1T2T3 bool operator==(const TRT1T2T3&t1, const TRT1T2T3&t2) { return (t1.a == t2.a && t1.b == t2.b && t1.c == t2.c); }
	TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; }
	TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t) { return os << t.a << " " << t.b << " " << t.c; }
	#undef TRT1T2T3
	#undef TT1T2T3
	using tri =  triple<int, int, int>;
	using trll = triple<ll, ll, ll>;
	using vtri = vector<tri>;
    using vtrll = vector<trll>;
}
using namespace triple;

template <class T> T invGeneral(T a, T b) { // 0 < a < b, gcd(a,b) = 1
    a %= b;
    if (a == 0) return b == 1 ? 0 : -1;
    T x = invGeneral(b, a);
    return x == -1 ? -1 : ((1-(ll)b * x) / a + b) % b;
}

template<class T, int MOD> struct modular {
	T val;
	explicit operator T() const { return val; }
	modular() { val = 0; }
	modular(const ll& v) {
		val = (-MOD <= v && v <= MOD) ? v : v % MOD;
		if (val < 0) val += MOD;
	}

	friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
	friend void pr(const modular& a) { pr(a.val); }
	friend void re(modular& a) { ll x; re(x); a = modular(x); }

	friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
	friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }
	friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; }

	modular operator-() const { return modular(-val); }
	modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
	modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
	modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }
	friend modular pow(modular a, ll p) {
		modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
		return ans;
	}
	friend modular inv(const modular& a) {
		auto i = invGeneral(a.val,MOD); assert(i != -1);
		return i;
	} // equivalent to return exp(b,MOD-2) if MOD is prime
	modular& operator/=(const modular& m) { return (*this) *= inv(m); }

	friend modular operator+(modular a, const modular& b) { return a += b; }
	friend modular operator-(modular a, const modular& b) { return a -= b; }
	friend modular operator*(modular a, const modular& b) { return a *= b; }
	friend modular operator/(modular a, const modular& b) { return a /= b; }
};

using mi = modular<int, MOD>;
using pmi = pair<mi,mi>;
using vmi = vector<mi>;
using vpmi = vector<pmi>;

namespace debug {
    template < typename _T > inline void _debug(const char *s, _T x) { cerr << s << " = " << x << "\n"; }
    template < typename _T, typename... args > void _debug(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _debug(s + 1, a...); }

    #if 1
        #define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__)
        #define cerr cout
    #else
        #define debug(...) 1999
        #define cerr if(0) cout
    #endif
} // namespace debug
using namespace debug;

//////////////_________CODE_________//////////////

int main() {
    BOOST;
    int n; re(n);

	vi perm(n * n);

	int i = 0, j = n * n - 1;
	FOR(x, n) {
		int r = n / 2;
		int l = n - r;
		FOR(y, l) {
			perm[i++] = x;
		}
		FOR(y, r) {
			perm[j--] = x;
		}
	}

	vvi res(n);
	FOR(i, n) {
		FOR(j, n) {
			int id = i * n + j;
			res[perm[id]].PB(id + 1);
		}
	}

	FOR(i, n) {
		prln(res[i]);
	}
}