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: /**
   * GFG
   */
  import java.util.*;
  import java.io.*;
  public class GFG {
  static boolean issafe(char ch[][],int i,int j,int dp[][]){
      if(i>=0&&i<ch.length&&j>=0&&j<ch.length&&ch[i][j]!='#'&&dp[i][j]!=1){
          return true;
      }
      return false;
  }
  static class Edge{
      int first;
      int second;
      Edge(int first,int second){
          this.first=first;
          this.second=second;
      }
  }
  static void print(ArrayList<Edge>A){
      for(Edge ob:A){
          System.out.println(ob.first+" "+ob.second);
      }
     
  }
  static void DFS(char ch[][],int i,int j,int dp[][],ArrayList<Edge>A){
  int row[]={-1,1,0,0};
  int cols[]={0,0,-1,1};
  dp[i][j]=1;
  A.add(new Edge(i,j));
  for(int k=0;k<row.length;++k){
     // System.out.println("Checking");
      if(issafe(ch,i+row[k],j+cols[k],dp)){
         DFS(ch,i+row[k],j+cols[k],dp,A);
  
      }
             print(A);
    if(A.size()!=0){
        System.out.println();
          A.remove(A.size()-1);
      }
    
    
  }
  
  }
  static void count(char ch[][]){
  
      for(int i=0;i<ch.length;++i){
          for(int j=0;j<ch.length;++j){
              int dp[][]=new int[ch.length][ch.length];
              ArrayList<Edge>A=new ArrayList<>();
              if(ch[i][j]=='.'){
                  DFS(ch,i,j,dp,A);
              }
          }
      }
  }
  static final int MAXN = 100001;
        
      // stores smallest prime factor for every number
      static int spf[] = new int[MAXN];
        
      // Calculating SPF (Smallest Prime Factor) for every
      // number till MAXN.
      // Time Complexity : O(nloglogn)
      static void sieve()
      {
          spf[1] = 1;
          for (int i=2; i<MAXN; i++)
        
              // marking smallest prime factor for every
              // number to be itself.
              spf[i] = i;
        
          // separately marking spf for every even
          // number as 2
          for (int i=4; i<MAXN; i+=2)
              spf[i] = 2;
        
          for (int i=3; i*i<MAXN; i++)
          {
              // checking if i is prime
              if (spf[i] == i)
              {
                  // marking SPF for all numbers divisible by i
                  for (int j=i*i; j<MAXN; j+=i)
        
                      // marking spf[j] if it is not
                      // previously marked
                      if (spf[j]==j)
                          spf[j] = i;
              }
          }
      }
        
      // A O(log n) function returning primefactorization
      // by dividing by smallest prime factor at every step
      static int count=0;
      static Vector<Integer> getFactorization(int x)
      {
          Vector<Integer> ret = new Vector<>();
          while (x != 1)
          {
              ++count;
              ret.add(spf[x]);
              if(spf[x]!=0)
              x = x / spf[x];
          }
          return ret;
   
      }
  static int check(String m,String n,int index,String ans,int count){
  if(index<0||index>=m.length()){
      return 0;
  }
  else
  if(count<n.length()&&m.charAt(index)!=n.charAt(count)){
      return 0;
  
  }
  else 
  if(count==n.length()){
      return 1;
  }
  
  
  
  //we have two choices 4
  int first=check(m,n,index-1,ans,count+1);
  int second=check(m,n,index+1,ans,count+1);
  return first|second;
  
  }
  static void print(String txt,int index,String ans,int sum){
  if(sum%3==0&&sum!=0){
      System.out.println(ans);
      
  }
  if(index>=txt.length()){
    
      return;
  }
  print(txt,index+1,ans+txt.charAt(index),sum+(txt.charAt(index)-48));
  print(txt,index+1,ans,sum);
  
  }
  public static void main(String[] args) {
  Scanner scan=new Scanner(System.in);
  int t=scan.nextInt();
  while(t-->0){
      int n=scan.nextInt();
      int  m=scan.nextInt();
      int arr[]=new int[n];
      int temp[]=new int[n];
      ArrayList<ArrayList<Integer>>A=new ArrayList<>();
      for(int i=0;i<=n;++i){
          A.add(new ArrayList<Integer>());
      }
      for(int i=0;i<n;++i){
          arr[i]=scan.nextInt();
          A.get(arr[i]).add(i);
  
      }
      //Count the size of the number of frequency one
      int count=0;
      for(int i=0;i<arr.length;++i){
          if(A.get(arr[i]).size()==1){
              ++count;
          }
      }
      if(count!=0){
      int total=m/count;
      }
      int count_=0;
      for(int i=0;i<arr.length;++i){
          if(A.get(arr[i]).size()==1||A.get(arr[i]).size()<m){
              ++count_;
          }
          /*if(A.get(arr[i]).size()==1){
           temp[A.get(arr[i]).get(0)]=count_;
           ++count_;
           count_=count_%(m+1);
           if(count_==0){
               count_=1;
           }
          }
          */
      }
      for(int i=0;i<=n;++i){
          if(A.get(i).size()>=m){
  
             for(int j=1;j<=A.get(i).size();++j){
                 if(j<=m)
                 temp[A.get(i).get(j-1)]=j;
                
  
             }
          }
      }
      int total=0;
      if(count_!=0){
           total=count_/m;
          total=total*m;
      }
      int value=1;
      for(int i=0;i<=n&&total!=0;++i){
          if(A.get(i).size()==1&&total!=0){
              ++value;
            value=value%(m+1);
            if(value==0)value=1;
            temp[A.get(i).get(0)]=value;
            
            --total;
          }
          else 
          if(A.get(i).size()<m&&A.get(i).size()!=0){
              for(int index:A.get(i)){
                 if(total==0){
                     break;
                 }
                 ++value;
                 value=value%(m+1);
                 if(value==0)value=1;
                 temp[index]=value;
                 
                 
                 --total;
              }
          }
      }
  
  
      for(int number:temp){
          System.out.print(number+" ");
      }
     
      System.out.println();
     
  
      
  }
  
  
  }}
  
  