import java.io.*;
import java.util.*;

public class DancingWithGooglers {

  public static class Googlers {

    public int T;
    public int surp;
    public boolean surprising;
    public boolean pSurprising;
    public boolean antiSurprising;
    public boolean pAntiSurprising;

    public Googlers(int T, int surp) {
      this.T = T;
      this.surp = surp;
    }

    public void set(int a, int b, int c) {
      if (c >= 0 && (a + b + c) == T) {
        int diff = (Math.max(a, Math.max(b, c)) - Math.min(a, Math.min(b, c)));
        if (diff <= 2) {
          if (diff == 2) {
            if (a >= surp || b >= surp || c >= surp) {
              pSurprising = true;
            } else {
              surprising = true;
            }
          } else {
            if (a >= surp || b >= surp || c >= surp) {
              pAntiSurprising = true;
            } else {
              antiSurprising = true;
            }
          }
        }
      }
    }
  }

  public static void main(String... args) throws IOException {
//    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    BufferedReader reader = new BufferedReader(new FileReader("B-large.in"));
//    PrintWriter writer = new PrintWriter(System.out);
    PrintWriter writer = new PrintWriter("B-large.out");

    int len, cases = Integer.parseInt(reader.readLine());
    List<Googlers> satisfied = new ArrayList<Googlers>();
    List<Googlers> unsatisfied = new ArrayList<Googlers>();
    String[] inputs;
    int N, S, p;
    for (int i = 1; i <= cases; i++) {
      inputs = reader.readLine().split(" ");
      p = Integer.parseInt(inputs[1]);
      S = Integer.parseInt(inputs[2]);
      satisfied.clear();
      unsatisfied.clear();
      for (int j = 3; j < inputs.length; j++) {
        int t = Integer.parseInt(inputs[j]);
        Googlers googlers = new Googlers(t, S);

        for (int k = 0; k <= 10; k++) {
          int till = k + 2 > 10 ? 10 : k + 2;
          for (int l = k; l <= till; l++) {
            googlers.set(k, l, (t - (k + l)));
          }
        }
        if (googlers.pAntiSurprising) {
          satisfied.add(googlers);
        } else {
          unsatisfied.add(googlers);
        }
      }
      int pSurprising = 0;
      if (p > 0) {
        for (Iterator<Googlers> iter = unsatisfied.iterator(); iter.hasNext(); ) {
          Googlers googlers = iter.next();
          if (googlers.pSurprising) {
            pSurprising++;
            iter.remove();
            p--;
            if (p <= 0) break;
          }
        }
      }
      if(p > 0){
        for (Iterator<Googlers> iter = unsatisfied.iterator(); iter.hasNext(); ) {
          Googlers googlers = iter.next();
          if (googlers.surprising) {
            iter.remove();
            p--;
            if (p <= 0) break;
          }
        }
      }
      if(p > 0){
        for (Iterator<Googlers> iter = satisfied.iterator(); iter.hasNext(); ) {
          Googlers googlers = iter.next();
          if (googlers.pSurprising) {
            iter.remove();
            pSurprising++;
            p--;
            if (p <= 0) break;
          }
        }
      }
      if(p > 0){
        for (Iterator<Googlers> iter = satisfied.iterator(); iter.hasNext(); ) {
          Googlers googlers = iter.next();
          if (googlers.surprising) {
            iter.remove();
            p--;
            if (p <= 0) break;
          }
        }
      }

      if(p > 0){
        writer.print("Case #" + i + ": 0");
      }else {
        writer.print("Case #" + i + ": " + (satisfied.size() + pSurprising));
      }
      writer.println();
    }
    writer.flush();
  }


}

package com.first;

public class BDancingWiththeGooglers {

	ReadWriteFileForCodeJam flatFile;

	public BDancingWiththeGooglers() {
		flatFile = new ReadWriteFileForCodeJam();
		initializeFlatFileParameters();
		performSolution();
		finalizeSolutionFileToSend();
	}

	private void performSolution(){
		String[] items;
		int noDancers, noOfSurprises, bestScore;
		int maxGooglers;
		int maxAchieved;
		boolean surprise;
		for(int i=1; i<=Integer.parseInt(flatFile.GetSingleLineFromFile(1)); i++){
			maxGooglers = 0;
			surprise = true;
			items = flatFile.GetSingleLineFromFile(i + 1).split(" ");
			noDancers = Integer.parseInt(items[0]);
			noOfSurprises = Integer.parseInt(items[1]);
			bestScore = Integer.parseInt(items[2]);
			flatFile.setFileContentToWrite("Case #" + i + ":", true, true);
			for(int j=0; j<noDancers; j++){
				if ((noOfSurprises <= 0) && surprise)
					surprise = false;
				maxAchieved = isBestScorePossible(Integer.parseInt(items[j+3]), bestScore);
				switch (maxAchieved){
				case 0: break;
				case 1: maxGooglers++;	break;
				case 2:
					if (surprise){
						maxGooglers++;
						noOfSurprises--;
					}
					break;
				}
			}
			flatFile.setFileContentToWrite(String.valueOf(maxGooglers), true, false);
		}
	}

	private int isBestScorePossible(int total, int least){
		/* return 0 if not possible
		 * 1 if possible without surprise
		 * 2 if possible with surprise
		 */
		
		if (total == 0){
			if (least == 0)
				return 1;
			else
				return 0;
		}
		
		if (total == 1){
			if ((least <= 1))
				return 1;
			else
				return 0;
		}
		
		if (least * 3 - 2 <= total)
			return 1;
		if (least * 3 - 4 <= total)
			return 2;
		return 0;
	}

	private void initializeFlatFileParameters() {
		flatFile.setFileNameForRead("C:\\Users\\i067221\\Downloads\\B-small-attempt0.in");
		flatFile.setFileNameForWrite("C:\\Users\\i067221\\Downloads\\Upload\\B-small-attempt0.txt");
		flatFile.setFileSeperatorForWrite(" ");
		System.out.println("File read, (" + flatFile.ReadFile(false) + ") returned.");
	}

	private void finalizeSolutionFileToSend() {
		System.out.println("Successful write to file performed: " + flatFile.WriteFile(false));
	}

}
