#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 5005, maxm = 5005;
struct edge_color {
  int deg[2][maxn];
  pair<int, int> has[2][maxn][205];
  int color[maxm];
  int c[2];
  void clear(int n) {
    for (int t = 0; t < 2; t++) {
      for (int i = 0; i <= n; i++) {
        deg[t][i] = 0;
        for (int j = 0; j <= n; j++) {
          has[t][i][j] = pair<int, int>(0, 0);
        }
      }
    }
  }
  void dfs(int x, int p) {
    auto i = has[p][x][c[!p]];
    if (has[!p][i.first][c[p]].second)
      dfs(i.first, !p);
    else
      has[!p][i.first][c[!p]] = pair<int, int>(0, 0);
    has[p][x][c[p]] = i;
    has[!p][i.first][c[p]] = pair<int, int>(x, i.second);
    color[i.second] = c[p];
  }
  int solve(vector<pair<int, int>> v, vector<int> &cv) {
    int m = v.size();
    int ans = 0;
    for (int i = 1; i <= m; i++) {
      int x[2];
      x[0] = v[i - 1].first;
      x[1] = v[i - 1].second;
      for (int d = 0; d < 2; d++) {
        deg[d][x[d]] += 1;
        ans = max(ans, deg[d][x[d]]);
        for (c[d] = 1; has[d][x[d]][c[d]].second; c[d]++)
          ;
      }
      if (c[0] != c[1]) dfs(x[1], 1);
      for (int d = 0; d < 2; d++) has[d][x[d]][c[0]] = pair<int, int>(x[!d], i);
      color[i] = c[0];
    }
    cv.resize(m);
    for (int i = 1; i <= m; i++) {
      cv[i - 1] = color[i];
      color[i] = 0;
    }
    return ans;
  }
} EC;
int N, M, T, K;
int deg[2][maxn], idx[2][maxn];
vector<pair<int, int>> edges;
signed main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> N >> M >> K >> T;
  int n, m;
  n = m = 0;
  while (K--) {
    int u, v;
    cin >> u >> v;
    if (deg[0][u] == 0) idx[0][u] = ++n;
    if (deg[1][v] == 0) idx[1][v] = ++m;
    ++deg[0][u];
    ++deg[1][v];
    if (deg[0][u] == T) deg[0][u] = 0;
    if (deg[1][v] == T) deg[1][v] = 0;
    edges.emplace_back(idx[0][u], idx[1][v]);
  }
  int res = 0;
  for (int i = 1; i <= N; ++i) {
    res += (deg[0][i] > 0);
  }
  for (int i = 1; i <= M; ++i) {
    res += (deg[1][i] > 0);
  }
  cout << res << '\n';
  vector<int> col;
  EC.solve(edges, col);
  for (int i : col) cout << i << ' ';
}
