#include <bits/stdc++.h>
using namespace std;
int n, k;
int cnt[200010];
int vis[200010], res[200010];
vector<pair<int, int> > v, g;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    int num = 0;
    for (int i = 0, x; i < n; i++) {
      cin >> x;
      v.push_back({x, i});
      cnt[x]++;
      if (cnt[x] <= k) num++;
      vis[x] = 1;
      res[i] = 0;
    }
    int val = num / k;
    sort(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++)
      if (vis[v[i].first]) {
        vis[v[i].first] = 0;
        for (int j = 0; j < min(k, cnt[v[i].first]); j++) {
          g.push_back(v[i + j]);
        }
      }
    int tmp = 0;
    for (int i = 0; i < val * k; i++) {
      res[g[i].second] += tmp + 1;
      tmp = (tmp + 1) % k;
    }
    for (int i = 0; i < n; i++) {
      cout << res[i] << " ";
      cnt[v[i].first] = 0;
    }
    cout << endl;
    v.clear();
    g.clear();
  }
}
