#include using namespace std; using ll = long long; using ld = long double; template using vec = vector; template using pqu = priority_queue; template using pqug = priority_queue, greater>; using Pint = pair; using Pll = pair; const int inf = 2147483647; const ll linf = 9223372036854775807LL; const Pint dxy4[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; const Pint dxy8[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}; map dir4 {{'L', {0, -1}}, {'R', {0, 1}}, {'U', {-1, 0}}, {'D', {1, 0}}}; #define v1 first #define v2 second #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define bisl(v, n) (lower_bound(v.begin(), v.end(), n) - v.begin()) #define bisr(v, n) (upper_bound(v.begin(), v.end(), n) - v.begin()) #define index(v, a) (find(all(v), a) - v.begin()) #define pcnt(n) __builtin_popcountll(n) #define overload4(a, b, c, d, e, ...) e #define range1(n) for (int _ = 0; _ < n; _++) #define range2(i, n) for (int i = 0; i < n; i++) #define range3(i, a, b) for (int i = a; i < b; i++) #define range4(i, a, b, c) for (int i = a; i < b; i += c) #define range(...) overload4(__VA_ARGS__, range4, range3, range2, range1)(__VA_ARGS__) #define rrange2(i, n) for (int i = n; i > 0; i--) #define rrange3(i, a, b) for (int i = a; i > b; i--) #define rrange4(i, a, b, c) for (int i = a; i > b; i -= c) #define rrange(...) overload4(__VA_ARGS__, rrange4, rrange3, rrange2, range1)(__VA_ARGS__) #define lcases(c) for (char c = 'a'; c <= 'z'; c++) #define ucases(c) for (char c = 'A'; c <= 'Z'; c++) /* #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") //*/ struct _IOSetUp { _IOSetUp() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(12); cerr << fixed << setprecision(12); } } _iosetup; // ac-library #if __has_include() #include using namespace atcoder; using mint = modint998244353; using mint1 = modint1000000007; istream &operator>>(istream &is, mint &m) { string S; is >> S; for (char c: S) m = m * 10 + c; return is; } ostream &operator<<(ostream &os, const mint &m) { os << m.val(); return os; } istream &operator>>(istream &is, mint1 &m) { string S; is >> S; for (char c: S) m = m * 10 + c; return is; } ostream &operator<<(ostream &os, const mint1 &m) { os << m.val(); return os; } #endif template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, pair &p) { os << p.first << ' ' << p.second; return os; } template istream &operator>>(istream &is, vector &v) { for (auto &e: v) { is >> e; } return is; } template ostream &operator<<(ostream &os, vector &v) { for (int i = 0; i < v.size() - 1; i++) { os << v[i] << ' '; } os << v.back(); return os; } template vector &operator++(vector &v) { for (auto &e: v) e++; return v; } template vector operator++(vector &v, int) { auto res = v; for (auto &e: v) e++; return res; } template vector &operator--(vector &v) { for (auto &e: v) e--; return v; } template vector operator--(vector &v, int) { auto res = v; for (auto &e: v) e--; return res; } template vector repl(vector v, T a, T b) { vector res; for (T i: v) { res.push_back((i == a ? b : i)); } return res; } string repl(string v, char a, char b) { string res; for (char i: v) { res.push_back((i == a ? b : i)); } return res; } template map repl(map m, T a, T b) { map res; for (auto [i, j]: m) { res[i] = (j == a ? b : j); } return res; } template void replace(vector &v, T a, T b) { for (int i = 0; i < v.size(); i++) { if (v[i] == a) v[i] = b; } } void replace(string &v, char a, char b) { for (int i = 0; i < v.size(); i++) { if (v[i] == a) v[i] = b; } } template void replace(map &m, T a, T b) { for (auto [i, j]: m) { m[i] = (j == a ? b : j); } } void move_dxy4(int &x, int &y, char c) { x += dir4[c].first; y += dir4[c].second; } int main() { int T; cin >> T; range(T) { int n; cin >> n; vec P(n); cin >> P; fenwick_tree fw(n); deque dq; long long ans = 0; for (int i: P) { int a = fw.sum(0, i - 1); int b = fw.sum(i, n); ans += min(a, b); if (a < b) { dq.push_front(i); } else if (a > b) { dq.push_back(i); } else { if (!dq.empty()) { int b = dq.front(); if (i < b) dq.push_front(i); else dq.push_back(i); } else { dq.push_back(i); } } fw.add(i - 1, 1); } cout << ans << '\n'; for (int i: dq) cout << i << ' '; cout << '\n'; } }