#include <bits/stdc++.h>
using namespace std;
namespace mine {
long long qread() {
  long long ans = 0, f = 1;
  char c = getchar();
  while (c < '0' or c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while ('0' <= c and c <= '9') ans = ans * 10 + c - '0', c = getchar();
  return ans * f;
}
void write(long long num) {
  if (num < 0) putchar('-'), num = -num;
  if (num >= 10) write(num / 10);
  putchar('0' + num % 10);
}
void write1(long long num) {
  write(num);
  putchar(' ');
}
void write2(long long num) {
  write(num);
  putchar('\n');
}
template <typename T>
inline bool chmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool chmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
bool IN(long long x, long long l, long long r) { return l <= x and x <= r; }
void GG() {
  puts("-1");
  exit(0);
}
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
int mm(const int x) { return x >= MOD ? x - MOD : x; }
template <typename T>
void add(T &x, const int &y) {
  x = (x + y >= MOD ? x + y - MOD : x + y);
}
long long qpower(long long x, long long e, int mod = MOD) {
  long long ans = 1;
  while (e) {
    if (e & 1) ans = ans * x % mod;
    x = x * x % mod;
    e >>= 1;
  }
  return ans;
}
long long invm(long long x) { return qpower(x, MOD - 2); }
const int MM = 1e3 + 10;
long long fac[MM], facinv[MM], Inv[MM];
long long Comb(int n, int m) {
  return n < 0 or n < m ? 0 : fac[n] * facinv[m] % MOD * facinv[n - m] % MOD;
}
void PRE() {
  fac[0] = 1;
  for (int i = (1), I = (MM - 1); i <= I; i++) fac[i] = fac[i - 1] * i % MOD;
  facinv[MM - 1] = invm(fac[MM - 1]);
  for (int i = (MM - 1), I = (1); i >= I; i--)
    facinv[i - 1] = facinv[i] * i % MOD;
  Inv[1] = 1;
  for (int i = (2), I = (MM - 1); i <= I; i++)
    Inv[i] = (MOD - MOD / i) * Inv[MOD % i] % MOD;
}
const int N = 1e6 + 10;
int cur[N], deg[N], id;
vector<pair<int, int> > to[N];
int col[N];
void solve(int x, int c1, int c2) {
  col[to[x][c1].second] = c2;
  swap(to[x][c1], to[x][c2]);
  if (to[x][c2].first) solve(to[x][c2].first, c2, c1);
}
void main() {
  int n1 = qread(), n2 = qread(), m = qread(), K = qread();
  for (int i = (1), I = (m); i <= I; i++) {
    int x = qread(), y = n1 + qread();
    if (deg[x] % K == 0) to[cur[x] = ++id].resize(K);
    deg[x]++;
    x = cur[x];
    if (deg[y] % K == 0) to[cur[y] = ++id].resize(K);
    deg[y]++;
    y = cur[y];
    int c1;
    for (int t = (0), I = (K - 1); t <= I; t++)
      if (!to[x][t].first) c1 = t;
    if (to[y][c1].first) {
      int c2;
      for (int t = (0), I = (K - 1); t <= I; t++)
        if (!to[y][t].first) c2 = t;
      solve(y, c1, c2);
    }
    to[x][c1] = {y, i}, to[y][c1] = {x, i}, col[i] = c1;
  }
  int ans = 0;
  for (int i = (1), I = (n1 + n2); i <= I; i++) ans += (deg[i] % K > 0);
  write2(ans);
  for (int i = (1), I = (m); i <= I; i++) write1(1 + col[i]);
}
};  // namespace mine
signed main() {
  srand(time(0));
  mine::PRE();
  mine::main();
  printf("");
}
