description: This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely.
  
  Recently, Paul and Mary have found a new favorite sequence of integers a_1, a_2, ..., a_n. They want to paint it using pieces of chalk of k colors. The coloring of a sequence is called wonderful if the following conditions are met:
  
    1. each element of the sequence is either painted in one of k colors or isn't painted; 
    2. each two elements which are painted in the same color are different (i. e. there's no two equal values painted in the same color); 
    3. let's calculate for each of k colors the number of elements painted in the color — all calculated numbers must be equal; 
    4. the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. 
  
  
  
  E. g. consider a sequence a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2] and k=3. One of the wonderful colorings of the sequence is shown in the figure.
  
  <image> The example of a wonderful coloring of the sequence a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2] and k=3. Note that one of the elements isn't painted.
  
  Help Paul and Mary to find a wonderful coloring of a given sequence a.
  
  Input
  
  The first line contains one integer t (1 ≤ t ≤ 10000) — the number of test cases. Then t test cases follow.
  
  Each test case consists of two lines. The first one contains two integers n and k (1 ≤ n ≤ 2⋅10^5, 1 ≤ k ≤ n) — the length of a given sequence and the number of colors, respectively. The second one contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n).
  
  It is guaranteed that the sum of n over all test cases doesn't exceed 2 ⋅ 10^5.
  
  Output
  
  Output t lines, each of them must contain a description of a wonderful coloring for the corresponding test case.
  
  Each wonderful coloring must be printed as a sequence of n integers c_1, c_2, ..., c_n (0 ≤ c_i ≤ k) separated by spaces where
  
    * c_i=0, if i-th element isn't painted; 
    * c_i>0, if i-th element is painted in the c_i-th color. 
  
  
  
  Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one.
  
  Example
  
  Input
  
  
  6
  10 3
  3 1 1 1 1 10 3 10 10 2
  4 4
  1 1 1 1
  1 1
  1
  13 1
  3 1 4 1 5 9 2 6 5 3 5 8 9
  13 2
  3 1 4 1 5 9 2 6 5 3 5 8 9
  13 3
  3 1 4 1 5 9 2 6 5 3 5 8 9
  
  
  Output
  
  
  1 1 0 2 3 2 2 1 3 3
  4 2 1 3
  1
  0 0 1 1 0 1 1 1 0 1 1 1 0
  2 1 2 2 1 1 1 1 2 1 0 2 2
  1 1 3 2 1 3 3 1 2 2 3 2 0
  
  Note
  
  In the first test case, the answer is shown in the figure in the statement. The red color has number 1, the blue color — 2, the green — 3.
solution: #include <bits/stdc++.h>
  using namespace std;
  template <typename A, typename B>
  string to_string(pair<A, B> p);
  string to_string(const char& ch) { return "'" + string(1, ch) + "'"; }
  string to_string(const string& s) { return '"' + s + '"'; }
  string to_string(const char* s) { return to_string((string)s); }
  string to_string(bool b) { return (b ? "true" : "false"); }
  string to_string(vector<bool> v) {
    bool first = true;
    string res = "{";
    for (int i = 0; i < static_cast<int>(v.size()); i++) {
      if (!first) {
        res += ", ";
      }
      first = false;
      res += to_string(v[i]);
    }
    res += "}";
    return res;
  }
  template <typename A>
  string to_string(A v) {
    bool first = true;
    string res = "{";
    for (const auto& x : v) {
      if (!first) {
        res += ", ";
      }
      first = false;
      res += to_string(x);
    }
    res += "}";
    return res;
  }
  template <typename A, typename B>
  string to_string(pair<A, B> p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
  }
  void debug_out() { cout << endl; }
  template <typename Head, typename... Tail>
  void debug_out(Head H, Tail... T) {
    cout << " " << to_string(H);
    debug_out(T...);
  }
  template <typename T>
  using vv = vector<vector<T>>;
  template <typename T>
  using mxpq = priority_queue<T>;
  template <typename T>
  using mnpq = priority_queue<T, vector<T>, greater<T>>;
  template <class A, size_t S>
  istream& operator>>(istream& in, array<A, S>& a) {
    for (A& x : a) in >> x;
    return in;
  }
  template <typename T>
  istream& operator>>(istream& in, vector<T>& a) {
    for (T& x : a) in >> x;
    return in;
  }
  template <class A, size_t S>
  ostream& operator<<(ostream& out, array<A, S>& a) {
    bool f = false;
    for (A& x : a) {
      if (f) out << " ";
      out << x;
      f = true;
    }
    return out;
  }
  template <typename T>
  ostream& operator<<(ostream& out, vector<T>& a) {
    bool f = false;
    for (T& x : a) {
      if (f) out << " ";
      out << x;
      f = true;
    }
    return out;
  }
  template <typename T, typename T1, typename... Tail>
  T amin(T& a, T1 b, Tail... c) {
    if (b < a) a = b;
    if constexpr (sizeof...(c) != 0) {
      amin(a, c...);
    }
    return a;
  }
  template <typename T, typename T1, typename... Tail>
  T amax(T& a, T1 b, Tail... c) {
    if (b > a) a = b;
    if constexpr (sizeof...(c) != 0) {
      amax(a, c...);
    }
    return a;
  }
  int64_t power(int64_t x, int64_t y, int64_t M = 0) {
    int64_t v = 1;
    if (M > int64_t(0)) {
      x = x % M;
      while (y > 0) {
        if (y & 1) v = (v * x) % M;
        y = y >> 1;
        x = (x * x) % M;
      }
      return v;
    } else {
      while (y > 0) {
        if (y & 1) v = v * x;
        y = y >> 1;
        x = x * x;
      }
      return v;
    }
  }
  int largest_bit(long long x) { return x == 0 ? -1 : 63 - __builtin_clzll(x); }
  const long double PI = acos(-1);
  const int64_t INF = 0x3f3f3f3f3f3f3f3fll;
  const int64_t mod = 1e9 + 7;
  const int64_t maxN = 1e6 + 5;
  void preSolve(int& testcases) { cin >> testcases; }
  void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> a(n), ans(n, 0);
    map<int, vector<int>> mp;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      mp[a[i]].push_back(i);
    }
    int cc = 0;
    set<int> s;
    int tot = n;
    for (auto& i : mp) {
      if (int(i.second.size()) >= k) {
        cc++;
        int c = k;
        s.insert(i.first);
        while (c--) {
          ans[i.second.back()] = c + 1;
          i.second.pop_back();
        }
      }
    }
    for (auto& i : s) {
      tot -= mp[i].size();
      mp.erase(i);
    }
    int col = k;
    vector<pair<int, int>> tmp;
    for (auto& i : mp) {
      for (int j = 0; j < i.second.size(); j++) {
        tmp.push_back({mp[i.first][j], col});
        col--;
        if (col == 0) {
          for (int kk = 0; kk < k; kk++) {
            ans[tmp[kk].first] = tmp[kk].second;
          }
          col = k;
          tmp.clear();
        }
      }
    }
    for (int i = 0; i < n; i++) cout << ans[i] << ' ';
    cout << '\n';
  }
  signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int testcases = 1;
    preSolve(testcases);
    for (int caseno = 1; caseno <= testcases; ++caseno) {
      solve();
    }
    return 0;
  }
  