Problem: There is a developed network of flights between Berland and Beerland. All of them belong to the Berland state company BerAvia. Each flight connects some Berland city with some Beerland city. For each flight airplanes fly in both directions.

Changes are coming to Berland — the state decided to privatize BerAvia, namely, to sell out all flights to t private companies. Each of these companies wants to get the maximal number of flights, so if the Berland flights are sold unevenly, Berland can be accused of partiality. Berland Government decided to sell the flights as evenly as possible between the t companies.

The unevenness of the distribution of flights between companies is calculated as follows. For each city i (both Berland and Beerland) we'll calculate the value of 

<image> where aij is the number of flights from city i, which belong to company j. The sum of wi for all cities in both countries is called the unevenness of the distribution. The distribution with the minimal unevenness is the most even one.

Help the Berland government come up with the most even distribution plan of selling flights.

Input

The first input line contains four integers n, m, k and t (1 ≤ n, m, t ≤ 200;1 ≤ k ≤ 5000), where n, m are the numbers of cities in Berland and Beerland, correspondingly, k is the number of flights between them, and t is the number of private companies. Next k lines describe the flights, one per line, as pairs of positive integers xi, yi (1 ≤ xi ≤ n;1 ≤ yi ≤ m), where xi and yi are the indexes of cities in Berland and Beerland, correspondingly, connected by the i-th flight. There is at most one flight between any pair of cities, each flight connects cities of different countries. The cities in Berland are indexed from 1 to n, and in Beerland — from 1 to m.

Output

Print the unevenness of the sought plan on the first line. On the second line print a sequence of k integers c1, c2, ..., ck (1 ≤ ci ≤ t), where ci is the index of the company that should buy the i-th flight. Assume that the flights are indexed from 1 to k in the order they appear in the input. If there are multiple solutions, print any of them.

Examples

Input

3 5 8 2
1 4
1 3
3 3
1 2
1 1
2 1
1 5
2 2


Output

4
2 1 2 1 2 1 2 2 

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
int n, m, ne, nc;
const int inff = 0x3f3f3f3f;
const int infc = 0x3f3f3f3f;
const int E = 40000;
const int N = 420;
struct network {
  int nv, ne, pnt[E], nxt[E], shou[E], lsh;
  int vis[N], que[N + 2], head[N], pv[N], pe[N];
  int flow, cap[E];
  int cost, dis[E], d[N];
  bool can[E];
  bool can1[E];
  void addedge(int u, int v, int c, int w) {
    pnt[ne] = v;
    cap[ne] = c;
    dis[ne] = +w;
    nxt[ne] = head[u];
    head[u] = (ne++);
    pnt[ne] = u;
    cap[ne] = 0;
    dis[ne] = -w;
    nxt[ne] = head[v];
    head[v] = (ne++);
  }
  int mincost(int src, int sink) {
    int i, k, f, r;
    int mxf;
    lsh = 0;
    memset(can1, 0, sizeof(can));
    for (flow = 0, cost = 0;;) {
      memset(pv, -1, sizeof(pv));
      memset(vis, 0, sizeof(vis));
      for (i = 0; i < nv; ++i) d[i] = infc;
      d[src] = 0;
      pv[src] = src;
      vis[src] = 1;
      for (f = 0, r = 1, que[0] = src; r != f;) {
        i = que[f++];
        vis[i] = 0;
        if (N == f) f = 0;
        for (k = head[i]; k != -1; k = nxt[k])
          if (!(can[k]) && cap[k] && dis[k] + d[i] < d[pnt[k]]) {
            d[pnt[k]] = dis[k] + d[i];
            if (0 == vis[pnt[k]]) {
              vis[pnt[k]] = 1;
              que[r++] = pnt[k];
              if (N == r) r = 0;
            }
            pv[pnt[k]] = i;
            pe[pnt[k]] = k;
          }
      }
      if (-1 == pv[sink]) break;
      for (k = sink, mxf = inff; k != src; k = pv[k])
        if (cap[pe[k]] < mxf) mxf = cap[pe[k]];
      flow += mxf;
      cost += d[sink] * mxf;
      for (k = sink; k != src; k = pv[k]) {
        cap[pe[k]] -= mxf;
        cap[pe[k] ^ 1] += mxf;
        if (pv[k] > k) {
          can1[pe[k]] = 1;
          can1[pe[k] ^ 1] = 1;
        }
        shou[lsh++] = pe[k] ^ 1;
      }
    }
    for (i = 0; i < lsh; i++) {
      if (can1[shou[i]]) continue;
      can[shou[i]] = 1;
      can[shou[i] ^ 1] = 1;
    }
    return cost;
  }
} g;
inline void changeedge(int no, int cap, int f) {
  if (f) {
    g.cap[no] = cap;
    g.cap[no + 1] = 0;
    g.can[no] = 0;
    g.can[no + 1] = 0;
  } else {
    g.dis[no] = cap;
    g.dis[no + 1] = -cap;
    g.can[no] = 0;
    g.can[no + 1] = 0;
  }
}
int capcity[N];
int ans[N][N];
int orig[E][2];
int main() {
  memset(ans, -1, sizeof(ans));
  scanf("%d%d%d%d", &n, &m, &ne, &nc);
  memset(g.head, -1, sizeof(g.head));
  for (int i = 0; i < ne; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g.addedge(a, b + n, 1, 0);
    capcity[a]++;
    capcity[b + n]++;
    orig[i][0] = a - 1;
    orig[i][1] = b - 1;
  }
  int k = 0;
  for (int i = 1; i < n + m + 1; i++)
    if (capcity[i] % nc) k++;
  printf("%d\n", k);
  for (int i = 1; i < n + 1; i++) {
    g.addedge(0, i, capcity[i] / nc, 0);
    g.addedge(0, i, capcity[i] / nc, 1);
  }
  for (int i = 1; i < m + 1; i++) {
    g.addedge(i + n, n + m + 1, capcity[i + n] / nc, 0);
    g.addedge(i + n, n + m + 1, capcity[i + n] / nc, 1);
  }
  g.nv = n + m + 2;
  memset(g.can, 0, sizeof(g.can));
  for (int i1 = 0; i1 < nc; i1++) {
    int p1 = g.head[0];
    while (p1 != -1) {
      if (g.dis[p1] == 0)
        changeedge(p1, capcity[g.pnt[p1]] / (nc - i1), 1);
      else
        changeedge(p1, bool(capcity[g.pnt[p1]] % (nc - i1)), 1);
      p1 = g.nxt[p1];
    }
    for (int i = 1; i < m + 1; i++) {
      int temph = g.head[i + n];
      changeedge(temph, bool(capcity[i + n] % (nc - i1)), 1);
      temph = g.nxt[temph];
      changeedge(temph, capcity[i + n] / (nc - i1), 1);
      temph = 0;
    }
    k = g.mincost(0, n + m + 1);
    for (int i = 1; i < n + 1; i++) {
      int ranh = g.head[i];
      while (ranh != -1) {
        if (g.pnt[ranh] != 0 && g.cap[ranh] == 0 &&
            ans[i - 1][g.pnt[ranh] - n - 1] == -1) {
          ans[i - 1][g.pnt[ranh] - n - 1] = i1;
          capcity[i]--;
          capcity[g.pnt[ranh]]--;
          g.can[ranh] = 1;
          g.can[ranh ^ 1] = 1;
        }
        ranh = g.nxt[ranh];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    int col[N], max = -1, min = 100000;
    memset(col, 0, sizeof(col));
    for (int j = 0; j < m; j++)
      if (ans[i][j] != -1) col[ans[i][j]]++;
    for (int j = 0; j < nc; j++) {
      if (min > col[j]) min = col[j];
      if (max < col[j]) max = col[j];
    }
    if (max > min + 1) {
      printf("WA i %d\n", i);
      for (int j = 0; j < nc; j++) printf("%d ", col[j]);
      return 0;
    }
  }
  for (int i = 0; i < m; i++) {
    int col[N], max = -1, min = 100000;
    memset(col, 0, sizeof(col));
    for (int j = 0; j < n; j++)
      if (ans[j][i] != -1) col[ans[j][i]]++;
    for (int j = 0; j < nc; j++) {
      if (min > col[j]) min = col[j];
      if (max < col[j]) max = col[j];
    }
    if (max > min + 1) {
      printf("WA j %d\n", i);
      for (int j = 0; j < nc; j++) printf("%d ", col[j]);
      return 0;
    }
  }
  for (int i = 0; i < ne; i++) {
    while (ans[orig[i][0]][orig[i][1]] == -1)
      ;
    printf("%d ", ans[orig[i][0]][orig[i][1]] + 1);
  }
  return 0;
}