import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class CodeJam {

//	public static final String INPUT_FILE_PATH = "D://CodeJamInput.txt";
	public static final String INPUT_FILE_PATH = "D://B-large.in";
	public static final String OUTPUT_FILE_PATH = "D://CodeJamOutput.txt";

	public static List<String> readInput() {
		List<String> list = new ArrayList<String>();
		try {
			BufferedReader input = new BufferedReader(new FileReader(
					INPUT_FILE_PATH));
			try {
				String line = null;

				while ((line = input.readLine()) != null) {
					list.add(line);
				}
			} finally {
				input.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		
		return list;
	}

	public static void writeOutput(List<String> output) {
		PrintWriter writer = null;
		try {
			writer = new PrintWriter(new FileWriter(OUTPUT_FILE_PATH));
			for (String line : output) {
				writer.println(line);
			}
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null)
				writer.close();
		}
	}
	
	public static void main(String[] args) {
		List<DanceFloor> danceFloors = DanceFloor.parseFromFile(CodeJam.readInput());
		List<String> output = new ArrayList<String>();

		for(int i = 0; i < danceFloors.size(); i++) {
			System.out.println(danceFloors.get(i));
			String line = "";
			line += "Case #" + (i + 1) + ": " + danceFloors.get(i).calculate();
			output.add(line);
		}
		
		CodeJam.writeOutput(output);
	}
}

class DanceFloor {

	private List<Integer> tripletsTotalCounts;
	private int surpriseCount;
	private int targetScore;
	private int dancersCount;

	public DanceFloor(List<Integer> tripletsTotalCounts, int surpriseCount,
			int targetScore, int dancersCount) {
		this.tripletsTotalCounts = tripletsTotalCounts;
		this.surpriseCount = surpriseCount;
		this.targetScore = targetScore;
		this.dancersCount = dancersCount;
	}

	public int calculate() {
		int result = 0;
		
		if (targetScore == 0) return dancersCount;
		
		for (int i = 0; i < tripletsTotalCounts.size(); i++) {
			int total = tripletsTotalCounts.get(i);

			if (total < (targetScore * 3 - 4)) {
				tripletsTotalCounts.remove(i--);
			} else if (total == 0) {
				tripletsTotalCounts.remove(i--);
			}
		}
		
		for (int i = 0; i < tripletsTotalCounts.size(); i++) {
			int total = tripletsTotalCounts.get(i);

			if ((surpriseCount > 0) && (total == targetScore * 3 - 4)) {
				result++;
				surpriseCount--;
				tripletsTotalCounts.remove(i--);
			} else if ((surpriseCount == 0) && (total == targetScore * 3 - 4)) {
				tripletsTotalCounts.remove(i--);
			}
		}
		
		for (int i = 0; i < tripletsTotalCounts.size(); i++) {
			int total = tripletsTotalCounts.get(i);

			if ((surpriseCount > 0) && (total == targetScore * 3 - 3)) {
				result++;
				surpriseCount--;
				tripletsTotalCounts.remove(i--);
			} else if ((surpriseCount == 0) && (total == targetScore * 3 - 3)) {
				tripletsTotalCounts.remove(i--);
			}
		}
		
		result += tripletsTotalCounts.size();
		
		return result;
	}

	public static List<DanceFloor> parseFromFile(List<String> readInput) {
		List<DanceFloor> result = new ArrayList<DanceFloor>();
		int testCaseCount = Integer.parseInt(readInput.get(0));

		for (int i = 0; i < testCaseCount; i++) {
			String line[] = readInput.get(i + 1).split(" ");

			List<Integer> totalCounts = new ArrayList<Integer>();
			for (int j = 0; j < Integer.parseInt(line[0]); j++) {
				totalCounts.add(Integer.parseInt(line[3 + j]));
			}

			result.add(new DanceFloor(totalCounts, Integer.parseInt(line[1]),
					Integer.parseInt(line[2]), Integer.parseInt(line[0])));
		}

		return result;
	}

	@Override
	public String toString() {
		return "dancersCount = " + dancersCount + "; surpriseCount = " + surpriseCount 
								 + "; targetScore = " + targetScore + "; tripletsTotalCounts" 
								 + tripletsTotalCounts.toString();
	}
}


package utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Fabien Renaud
 */
public class File {

  public static boolean delete(String filename) {
    java.io.File f = new java.io.File(filename);
    if (f != null) {
      return f.delete();
    }
    return false;
  }

  public static String[] readAllLines(String filename) {
    return readAllLines(new java.io.File(filename));
  }

  public static String[] readAllLines(java.io.File file) {
    List<String> list = new ArrayList<String>();

    try {
      BufferedReader input = new BufferedReader(new FileReader(file));
      try {
        String line = null;

        while ((line = input.readLine()) != null) {
          list.add(line);
        }
      } finally {
        input.close();
      }
    } catch (IOException ex) {
      Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
    }

    String[] s = new String[list.size()];
    return list.toArray(s);
  }

  public static String readAllText(String filename) {
    return readAllText(new java.io.File(filename));
  }

  public static String readAllText(java.io.File file) {
    StringBuilder content = new StringBuilder();
    String lf = System.getProperty("line.separator");

    try {
      BufferedReader input = new BufferedReader(new FileReader(file));
      try {
        String line = null;

        while ((line = input.readLine()) != null) {
          content.append(line);
          content.append(lf);
        }
      } finally {
        input.close();
      }
    } catch (IOException ex) {
      Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
    }

    return content.toString();
  }

  public static byte[] readAllBytes(String filename) {
    return readAllBytes(new java.io.File(filename));
  }

  public static byte[] readAllBytes(java.io.File file) {
    byte[] bytes = new byte[0];
    InputStream is = null;
    try {
      is = new FileInputStream(file);
      long length = file.length();
      if (length > Integer.MAX_VALUE) {
        // File is too large
      }

      bytes = new byte[(int) length];
      int offset = 0;
      int numRead = 0;
      while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
      }
      if (offset < bytes.length) {
        throw new IOException("Could not completely read file " + file.getName());
      }
      is.close();
    } catch (Exception ex) {
      Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      try {
        is.close();
      } catch (IOException ex) {
        Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    return bytes;
  }

  public static void writeAllLines(String filename, String[] lines) {
    writeAllLines(new java.io.File(filename), lines);
  }

  public static void writeAllLines(java.io.File file, String[] lines) {
    String lf = System.getProperty("line.separator");

    try {
      FileWriter fw = new FileWriter(file);
      for (String l : lines) {
        fw.write(l);
        fw.write(lf);
        fw.flush();
      }
      fw.close();
    } catch (IOException ex) {
      Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public static void writeAllText(String filename, String text) {
    writeAllText(new java.io.File(filename), text);
  }

  public static void writeAllText(java.io.File file, String text) {
    String lf = System.getProperty("line.separator");

    try {
      FileWriter fw = new FileWriter(file);
      fw.write(text);
      fw.flush();
      fw.close();
    } catch (IOException ex) {
      Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public static void writeAllBytes(String filename, byte[] bytes) {
    writeAllBytes(new java.io.File(filename), bytes);
  }

  public static void writeAllBytes(java.io.File file, byte[] bytes) {
    try {
      FileOutputStream fos = new FileOutputStream(file);
      fos.write(bytes);
      fos.close();
    } catch (Exception ex) {
      Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}
