#include <bits/stdc++.h>
using namespace std;
const int MAXO = 1e9;
int node, edge;
int src, des;
vector<int> adjl[(610 + 610)];
vector<int> rc[(610 + 610)];
vector<int> rev_idx[(610 + 610)];
void add_edge(int u, int v, int w) {
  adjl[u].push_back(v);
  adjl[v].push_back(u);
  rc[u].push_back(w);
  rc[v].push_back(0);
  rev_idx[u].push_back(adjl[v].size() - 1);
  rev_idx[v].push_back(adjl[u].size() - 1);
}
void init_flow() {
  for (int i = 0; i <= node; i++) {
    adjl[i].clear();
    rc[i].clear();
    rev_idx[i].clear();
  }
}
int lev[(610 + 610)];
int vis[(610 + 610)];
bool dinic_bfs() {
  memset(vis, 0, sizeof(vis));
  memset(lev, 0, sizeof(lev));
  lev[src] = 0;
  vis[src] = 1;
  queue<int> myq;
  myq.push(src);
  while (!myq.empty()) {
    int u = myq.front();
    myq.pop();
    int sz = adjl[u].size();
    for (int i = 0; i <= sz - 1; i++) {
      int v = adjl[u][i];
      if (rc[u][i] == 0) {
        continue;
      }
      if (vis[v] == 0) {
        vis[v] = 1;
        lev[v] = lev[u] + 1;
        myq.push(v);
      }
    }
  }
  if (vis[des]) {
    return 1;
  }
  return 0;
}
int shuru[(610 + 610)];
int dinic_dfs(int u, int flow) {
  if (u == des) {
    return flow;
  }
  for (int i = shuru[u]; i <= (int)adjl[u].size() - 1; i++) {
    shuru[u] = i;
    int v = adjl[u][i];
    if ((lev[v] == lev[u] + 1) && (rc[u][i])) {
      int ff = dinic_dfs(v, min(flow, rc[u][i]));
      if (ff) {
        rc[u][i] -= ff;
        rc[v][rev_idx[u][i]] += ff;
        return ff;
      }
    }
  }
  shuru[u] = adjl[u].size();
  return 0;
}
int dinic() {
  int ans = 0;
  while (dinic_bfs()) {
    memset(shuru, 0, sizeof(shuru));
    while (1) {
      int ff = dinic_dfs(src, MAXO);
      if (ff == 0) break;
      ans += ff;
    }
  }
  return ans;
}
int d[(610 + 610)], ans[(610 + 610)];
vector<int> took[(610 + 610)];
void solve() {
  int n, m, k, u, v, c = 0;
  cin >> n >> m >> k;
  src = 0;
  node = des = n + m + 1;
  init_flow();
  memset(d, 0, sizeof(d));
  memset(ans, 0, sizeof(ans));
  for (int i = 1; i <= m; i++) add_edge(src, i + n, 1);
  for (int i = 1; i <= m; i++) {
    cin >> u >> v;
    add_edge(i + n, u, 1);
    add_edge(i + n, v, 1);
    d[u]++;
    d[v]++;
  }
  for (int i = 1; i <= n; i++)
    if (d[i] > k) add_edge(i, des, (d[i] - k) * 2), c += (d[i] - k) * 2;
  if (dinic() == c) {
    for (int i = 0; i <= n; i++) took[i].clear();
    for (int x = 1; x <= m; x++) {
      u = x + n;
      for (int i = 0; i <= (int)adjl[u].size() - 1; i++) {
        int v = adjl[u][i];
        if (rc[u][i] == 0 && v >= 1 && v <= n) took[v].push_back(x);
      }
    }
    int id = 1;
    for (int u = 1; u <= n; u++) {
      for (int i = 0; i <= (int)took[u].size() - 1; i++) {
        int p = took[u][i];
        ans[p] = id;
        if (i & 1) id++;
      }
    }
    for (int i = 1; i <= m; i++)
      if (!ans[i]) ans[i] = id++;
  }
  for (int i = 1; i <= m; i++) cout << ans[i] << ' ';
  cout << endl;
}
int main() {
  ios::sync_with_stdio(false);
  int tc;
  cin >> tc;
  for (int tt = 1; tt <= tc; tt++) solve();
  return 0;
}
