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 probs;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Problem2 {

	public static void main(String[] args) {

		try {

			String filePath = "in-large-2.txt";

			List<String> input = readInput(filePath);
			int[] output = process(input);
			writeOutput(output);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static int[] process(List<String> input) {

		int[] result = new int[input.size()];
		int count = 0;
		for (String eachLine : input) {
			String[] split = eachLine.split(" ");
			int googlers = Integer.parseInt(split[0]);
			int surprisesAllowed = Integer.parseInt(split[1]);
			int criteriaBestScore = Integer.parseInt(split[2]);
			int[] totals = new int[googlers];
			for (int i = 0; i < googlers; i++) {
				totals[i] = Integer.parseInt(split[i + 3]);
			}
			int maxGooglersWithAtleaseRequiredBestResult = processEachTestCase(surprisesAllowed, criteriaBestScore, totals);
			result[count] = maxGooglersWithAtleaseRequiredBestResult;

			count++;
		}

		return result;
	}

	private static int processEachTestCase(int surprisesAllowed,
			int bestScore, int[] totals) {

		int result = 0;

		final int max=10;
		final int doubleMax = max*2;
		
		int maxRange = 2*(bestScore+1);
		int minRange = 2*(bestScore-1);
		if (maxRange>doubleMax) {
			maxRange=doubleMax;
		}
		if (minRange<0) {
			minRange=0;
		}

		int furtherMaxRange = 2*(bestScore+2);
		int furtherMinRange = 2*(bestScore-2);
		if (furtherMaxRange>doubleMax) {
			furtherMaxRange=doubleMax;
		}
		if (furtherMinRange<0) {
			furtherMinRange=0;
		}

		int surpriseCounter=0;

		for (int i = 0; i < totals.length; i++) {

			int eachTotal = totals[i];

			if (eachTotal>=3*bestScore) { // 333 to infinity
				result++;
			}else{			
				if(eachTotal>=(bestScore+minRange) && eachTotal<=(bestScore+maxRange)){ // 322 to 344
					result++;
				}else{
					if(eachTotal<(bestScore+minRange) && eachTotal>=(bestScore+furtherMinRange)){ // 311 to 321
						if (surpriseCounter<surprisesAllowed) {
							result++;
							surpriseCounter++;
						}
					}else if(eachTotal>(bestScore+maxRange) && eachTotal<=(bestScore+furtherMaxRange)){ // 345 to 355
						if (surpriseCounter<surprisesAllowed) {
							result++;
							surpriseCounter++;
						}
					}
				}
			}
		}
		
		return result;
	}

	private static List<String> readInput(String filePath)
	throws FileNotFoundException {

		List<String> list = new ArrayList<String>();

		File file = new File(filePath);
		Scanner scanner = new Scanner(file);
		if (scanner.hasNextLine()) {
			String line = scanner.nextLine();
			int testCases = Integer.parseInt(line);

		}
		while (scanner.hasNextLine()) {
			String line = scanner.nextLine();
			//System.out.println(line);
			list.add(line);
		}

		return list;
	}

	private static void writeOutput(int[] list) {

		int n = list.length;

		for (int i = 0; i < n; i++) {
			System.out.println("Case #" + (i + 1) + ": " + list[i]);
		}
	}

}
