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 year2012.qualification.b;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

public class DancingWithTheGooglers {
	public static void main(String[] args) throws Exception{
		//GCJÃÃÂ±ÃÃÃ¶
		String filename = "src/year2012/qualification/b/B-large";
		PrintWriter out = new PrintWriter(new File(filename + ".out"));
		Scanner scan = new Scanner(new File(filename + ".in"));
		
		final int T = scan.nextInt();
		
		for(int i=0;i<T;i++){
			int N = scan.nextInt();
			int S = scan.nextInt();
			int p = scan.nextInt();
			int[] t = new int[N];
			int ans = 0;
			for(int j=0;j<N;j++){
				t[j] = scan.nextInt();
			}

			//surprisingâÃÃÃÃÃ´ÃÂ£Ãº
			List<Integer> probableSurprising = new ArrayList<Integer>();
			
			//30âÃÂ®29âÃÂ®28âÃÃâ°Ã¦Ã£ÃÂ§Ã±
			for(int j=0;j<N;j++){
				if(t[j] == 30 || t[j] == 29 || t[j] == 28){
					ans++;
				}else if(t[j] == 0){
					if(p==0)
						ans++;
				}else if(t[j] == 1){
					if(p==1)
						ans++;
				}else{
					if((int) Math.ceil((double)t[j] / 3.0) < p){
						probableSurprising.add(t[j]);
					}else{
						ans++;
					}
				}
				
			}
			//âÃÃ¥surprisingâÃÃ£âÃÃâÃÃ³âÃÃ¥âÃâ¢âÃÃÃÃ¯ââÃÃ§âÃÃ­ÃÂ¶Ã£âÃÂ¶âÃÃâÃÃ¨âÃÃ
			Collections.sort(probableSurprising, new Comparator<Integer>(){
			      public int compare(Integer t1, Integer t2) {
			        return t2 - t1;
			      }
			    });
			for(int j=0;j<probableSurprising.size();j++){
				int best=-1;
				//ÃÃºÃÃÃ Ã¹âÃÃSÃÃÃ£âÃÃsurprising
				if(j<S){
					int mod =probableSurprising.get(j) %3;
					if(mod == 1){
						best = (probableSurprising.get(j) + 2) / 3;
					}else if(mod == 2){
						best = (probableSurprising.get(j) + 4) / 3;
					}else if(mod == 0){
						best = (probableSurprising.get(j) + 3) / 3;
					}
				}else{
					//surprisingâÃÃâÃÃâÃâ¢âÃÃÃÃ¯ââÃÃ3âÃÃÃÃ¢â¤âÃÂ£âÃÂ¶ÃÃ Ã¡âÃÃ¤â°âÃ¤âÃÃ­âÃÃ¥âÃâbestâÃÃÃÃÏâÃÃ¥ÃÃ¡â«âÃÃ£
					best =(int) Math.ceil((double)probableSurprising.get(j) / 3.0);	
				}
				if(best >= p){
					ans++;
				}
				
			}
			System.out.println("Case #"+(i+1)+": "+ ans);
			out.printf("Case #%d: %s\n", i+1, ans);
		}
		out.flush();
	}
}
