#include <bits/stdc++.h>
using namespace std;
int TC, N, mxb, t, Xn;
int B[100010];
vector<int> X;
map<int, int> dx;
vector<vector<int> > prec;
int la[5010];
pair<int, int> dp[2][5010];
int main() {
  scanf("%d %d %d %d", &TC, &N, &mxb, &t);
  while (TC--) {
    X.clear();
    dx.clear();
    for (int i = 0; i < N; i++) {
      scanf("%d", &B[i]);
      X.push_back(B[i]);
    }
    sort(X.begin(), X.end());
    X.resize(unique(X.begin(), X.end()) - X.begin());
    Xn = X.size();
    for (int i = 0; i < Xn; i++) dx[X[i]] = i;
    for (int i = 0; i < N; i++) {
      B[i] = dx[B[i]];
    }
    prec = vector<vector<int> >(N, vector<int>(Xn));
    memset(la, -1, sizeof(la));
    for (int i = N - 1; i >= 0; i--) {
      la[B[i]] = i;
    }
    for (int i = N - 1; i >= 0; i--) {
      for (int j = 0; j < Xn; j++) {
        prec[i][j] = la[j];
      }
      la[B[i]] = i;
    }
    int ans = 0;
    for (int i = 0; i < 2; i++)
      for (int j = 0; j <= Xn; j++) dp[i][j] = pair<int, int>(1e9, 1e9);
    dp[0][0] = pair<int, int>(1, -1);
    for (int i = 0; i < Xn; i++) {
      for (int j = 0; j <= Xn; j++) {
        pair<int, int> p = dp[i & 1][j];
        if (p.first > t) continue;
        dp[(i + 1) & 1][j + 1] = min(dp[(i + 1) & 1][j + 1], p);
        dp[(i + 1) & 1][j] =
            min(dp[(i + 1) & 1][j],
                prec[(N + p.second) % N][i] <= p.second
                    ? pair<int, int>(p.first + 1, prec[(N + p.second) % N][i])
                    : pair<int, int>(p.first, prec[(N + p.second) % N][i]));
      }
      for (int j = 0; j <= Xn; j++) {
        dp[i & 1][j] = pair<int, int>(1e9, 1e9);
      }
    }
    for (int i = 0; i <= Xn; i++) {
      if (dp[Xn & 1][i].first <= t) {
        ans = Xn - i;
        break;
      }
    }
    printf("%d\n", ans);
  }
}
