#include <bits/stdc++.h>
using namespace std;
int n, vA, m, A[100010];
inline void Read(int &digit) {
  digit = 0;
  char c;
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; digit = digit * 10 + c - '0', c = getchar())
    ;
}
void read() {
  for (int i = 1; i <= n; ++i) Read(A[i]);
}
inline void update(int &a, int b) {
  if (a > b) a = b;
}
int LIS() {
  static int dp[2][100010], tmp[100010];
  static int use[100010], id;
  ++id;
  int cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (use[A[i]] != id) use[A[i]] = id, ++cnt;
  if (cnt <= m) return cnt;
  int cur = 0;
  memset(dp[cur], 0, sizeof(dp[cur]));
  for (int i = 1; i <= cnt; ++i) {
    int minv = dp[cur][0];
    bool mark = true;
    cur ^= 1;
    dp[cur][0] = 1 << 30;
    for (int j = 1; j <= n; ++j) dp[cur][A[j]] = 1 << 30;
    for (int j = 1; j <= vA; ++j)
      if (use[j] == id) {
        tmp[j] = minv;
        if (minv > dp[cur ^ 1][j]) minv = dp[cur ^ 1][j];
      }
    for (int j = n; j >= 1; --j) {
      int v = A[j], x = (tmp[v] - 1 + n) % n + 1;
      if (x < j)
        update(dp[cur][v], (tmp[v] - 1 + n) / n * n + j - n);
      else
        update(dp[cur][v], (tmp[v] - 1 + n) / n * n + j);
      if (dp[cur][v] <= n * m) mark = false;
    }
    if (mark) return i - 1;
  }
  return cnt;
}
int main() {
  int T;
  for (Read(T), Read(n), Read(vA), Read(m); T; --T) {
    read();
    printf("%d\n", LIS());
  }
  return 0;
}
