package template;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

public class TestCase {

    private boolean isSolved;
    private Object solution;
    private Map<String, Integer> intProperties;
    private Map<String, ArrayList<Integer>> intArrayProperties;
    private Map<String, ArrayList<ArrayList<Integer>>> intArray2DProperties;
    private Map<String, Double> doubleProperties;
    private Map<String, ArrayList<Double>> doubleArrayProperties;
    private Map<String, ArrayList<ArrayList<Double>>> doubleArray2DProperties;
    private Map<String, String> stringProperties;
    private Map<String, ArrayList<String>> stringArrayProperties;
    private Map<String, ArrayList<ArrayList<String>>> stringArray2DProperties;
    private Map<String, Boolean> booleanProperties;
    private Map<String, ArrayList<Boolean>> booleanArrayProperties;
    private Map<String, ArrayList<ArrayList<Boolean>>> booleanArray2DProperties;
    private Map<String, Long> longProperties;
    private Map<String, ArrayList<Long>> longArrayProperties;
    private Map<String, ArrayList<ArrayList<Long>>> longArray2DProperties;
    private int ref;
    private double time;

    public TestCase() {
        initialise();
    }

    private void initialise() {
        isSolved = false;
        intProperties = new HashMap<>();
        intArrayProperties = new HashMap<>();
        intArray2DProperties = new HashMap<>();
        doubleProperties = new HashMap<>();
        doubleArrayProperties = new HashMap<>();
        doubleArray2DProperties = new HashMap<>();
        stringProperties = new HashMap<>();
        stringArrayProperties = new HashMap<>();
        stringArray2DProperties = new HashMap<>();
        booleanProperties = new HashMap<>();
        booleanArrayProperties = new HashMap<>();
        booleanArray2DProperties = new HashMap<>();
        longProperties = new HashMap<>();
        longArrayProperties = new HashMap<>();
        longArray2DProperties = new HashMap<>();
        ref = 0;
    }

    public void setSolution(Object o) {
        solution = o;
        isSolved = true;
    }
    
    public Object getSolution() {
        if (!isSolved) {
            Utils.die("getSolution on unsolved testcase");
        }

        return solution;
    }

    public void setRef(int i) {
        ref = i;
    }
    public int getRef() {
        return ref;
    }

    public void setTime(double d) {
        time = d;
    }
    public double getTime() {
        return time;
    }
    
    public void setInteger(String s, Integer i) {
        intProperties.put(s, i);
    }
    
    public Integer getInteger(String s) {
        return intProperties.get(s);
    }

    public void setIntegerList(String s, ArrayList<Integer> l) {
        intArrayProperties.put(s, l);
    }

    public void setIntegerMatrix(String s, ArrayList<ArrayList<Integer>> l) {
        intArray2DProperties.put(s, l);
    }

    public ArrayList<Integer> getIntegerList(String s) {
        return intArrayProperties.get(s);
    }

    public Integer getIntegerListItem(String s, int i) {
        return intArrayProperties.get(s).get(i);
    }

    public ArrayList<ArrayList<Integer>> getIntegerMatrix(String s) {
        return intArray2DProperties.get(s);
    }
    
    public ArrayList<Integer> getIntegerMatrixRow(String s, int row) {
        return intArray2DProperties.get(s).get(row);
    }
    
    public Integer getIntegerMatrixItem(String s, int row, int column) {
        return intArray2DProperties.get(s).get(row).get(column);
    }

    public ArrayList<Integer> getIntegerMatrixColumn(String s, int column) {
        ArrayList<Integer> out = new ArrayList();
        
        for(ArrayList<Integer> row : intArray2DProperties.get(s)) {
            out.add(row.get(column));
        }
        return out;
    }


    public void setDouble(String s, Double i) {
        doubleProperties.put(s, i);
    }
    
    public Double getDouble(String s) {
        return doubleProperties.get(s);
    }

    public void setDoubleList(String s, ArrayList<Double> l) {
        doubleArrayProperties.put(s, l);
    }

    public void setDoubleMatrix(String s, ArrayList<ArrayList<Double>> l) {
        doubleArray2DProperties.put(s, l);
    }

    public ArrayList<Double> getDoubleList(String s) {
        return doubleArrayProperties.get(s);
    }

    public Double getDoubleListItem(String s, int i) {
        return doubleArrayProperties.get(s).get(i);
    }

    public ArrayList<ArrayList<Double>> getDoubleMatrix(String s) {
        return doubleArray2DProperties.get(s);
    }
    
    public ArrayList<Double> getDoubleMatrixRow(String s, int row) {
        return doubleArray2DProperties.get(s).get(row);
    }
    
    public Double getDoubleMatrixItem(String s, int row, int column) {
        return doubleArray2DProperties.get(s).get(row).get(column);
    }

    public ArrayList<Double> getDoubleMatrixColumn(String s, int column) {
        ArrayList<Double> out = new ArrayList();
        
        for(ArrayList<Double> row : doubleArray2DProperties.get(s)) {
            out.add(row.get(column));
        }
        return out;
    }

    
    public void setString(String s, String t) {
        stringProperties.put(s, t);
    }
    
    public String getString(String s) {
        return stringProperties.get(s);
    }

    public void setStringList(String s, ArrayList<String> l) {
        stringArrayProperties.put(s, l);
    }

    public void setStringMatrix(String s, ArrayList<ArrayList<String>> l) {
        stringArray2DProperties.put(s, l);
    }

    public ArrayList<String> getStringList(String s) {
        return stringArrayProperties.get(s);
    }

    public String getStringListItem(String s, int i) {
        return stringArrayProperties.get(s).get(i);
    }

    public ArrayList<ArrayList<String>> getStringMatrix(String s) {
        return stringArray2DProperties.get(s);
    }
    
    public ArrayList<String> getStringMatrixRow(String s, int row) {
        return stringArray2DProperties.get(s).get(row);
    }
    
    public String getStringMatrixItem(String s, int row, int column) {
        return stringArray2DProperties.get(s).get(row).get(column);
    }

    public ArrayList<String> getStringMatrixColumn(String s, int column) {
        ArrayList<String> out = new ArrayList();
        
        for(ArrayList<String> row : stringArray2DProperties.get(s)) {
            out.add(row.get(column));
        }
        return out;
    }

    
    public void setBoolean(String s, Boolean b) {
        booleanProperties.put(s, b);
    }
    
    public Boolean getBoolean(String s) {
        return booleanProperties.get(s);
    }

    public void setBooleanList(String s, ArrayList<Boolean> l) {
        booleanArrayProperties.put(s, l);
    }

    public void setBooleanMatrix(String s, ArrayList<ArrayList<Boolean>> l) {
        booleanArray2DProperties.put(s, l);
    }

    public ArrayList<Boolean> getBooleanList(String s) {
        return booleanArrayProperties.get(s);
    }

    public Boolean getBooleanListItem(String s, int i) {
        return booleanArrayProperties.get(s).get(i);
    }

    public ArrayList<ArrayList<Boolean>> getBooleanMatrix(String s) {
        return booleanArray2DProperties.get(s);
    }
    
    public ArrayList<Boolean> getBooleanMatrixRow(String s, int row) {
        return booleanArray2DProperties.get(s).get(row);
    }
    
    public Boolean getBooleanMatrixItem(String s, int row, int column) {
        return booleanArray2DProperties.get(s).get(row).get(column);
    }

    public ArrayList<Boolean> getBooleanMatrixColumn(String s, int column) {
        ArrayList<Boolean> out = new ArrayList();
        
        for(ArrayList<Boolean> row : booleanArray2DProperties.get(s)) {
            out.add(row.get(column));
        }
        return out;
    }

    public void setLong(String s, Long b) {
        longProperties.put(s, b);
    }
    
    public Long getLong(String s) {
        return longProperties.get(s);
    }

    public void setLongList(String s, ArrayList<Long> l) {
        longArrayProperties.put(s, l);
    }

    public void setLongMatrix(String s, ArrayList<ArrayList<Long>> l) {
        longArray2DProperties.put(s, l);
    }

    public ArrayList<Long> getLongList(String s) {
        return longArrayProperties.get(s);
    }

    public Long getLongListItem(String s, int i) {
        return longArrayProperties.get(s).get(i);
    }

    public ArrayList<ArrayList<Long>> getLongMatrix(String s) {
        return longArray2DProperties.get(s);
    }
    
    public ArrayList<Long> getLongMatrixRow(String s, int row) {
        return longArray2DProperties.get(s).get(row);
    }
    
    public Long getLongMatrixItem(String s, int row, int column) {
        return longArray2DProperties.get(s).get(row).get(column);
    }

    public ArrayList<Long> getLongMatrixColumn(String s, int column) {
        ArrayList<Long> out = new ArrayList();
        
        for(ArrayList<Long> row : longArray2DProperties.get(s)) {
            out.add(row.get(column));
        }
        return out;
    }

    

    


}

package com.eevoskos.codejam;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class DancingWithTheGooglers {
	
	static boolean DEBUG = false;
	
	class Googler implements Comparable<Googler> {
		List<Triplet> triplets;

		public Googler(List<Triplet> triplets) {
			Collections.sort(triplets);
			this.triplets = triplets;
		}

		List<Triplet> getSuprisingTriplets() {
			List<Triplet> surprising = new ArrayList<Triplet>();
			for (Triplet t : triplets) {
				if (t.isSurprising()) {
					surprising.add(t);
				}
			}
			return surprising;
		}

		Triplet bestScoreTriplet() {
			Collections.sort(triplets);
			return triplets.get(0);
		}

		@Override
		public int compareTo(Googler another) {
			return this.bestScoreTriplet().compareTo(another.bestScoreTriplet());
		}
		
		List<Triplet> tripletsWithBestScoreAtLeast(int p) {
			List<Triplet> list = new ArrayList<Triplet>();
			for (Triplet t : triplets) {
				if (t.bestResult() >= p) {
					list.add(t);
				}
			}
			return list;
		}
		
		int[] numOftripletsWithBestScoreAtLeast(int p) {
			// result[0]: number of triplets
			// result[1]: number of the triplets that are surprising
			int[] result = new int[2];
			result[0] = 0;
			result[1] = 0;
			for (Triplet t : triplets) {
				if (t.bestResult() >= p) {
					result[0]++;
					if (t.isSurprising()) {
						result[1]++;
					}
				}
			}
			return result;
		}
	}

	class Triplet implements Comparable<Triplet> {
		int x, y, z;

		Triplet(int x, int y, int z) {
			int[] n = new int[] { x, y, z };
			Arrays.sort(n);
			this.x = n[0];
			this.y = n[1];
			this.z = n[2];
		}

		int sum() {
			return x + y + z;
		}

		@Override
		public boolean equals(Object obj) {
			if (obj == null)
				return false;

			if (!(obj instanceof Triplet))
				return false;

			return x == ((Triplet) obj).x 
					&& y == ((Triplet) obj).y
					&& z == ((Triplet) obj).z;
		}
		
		boolean isSurprising() {
			int[] n = new int[] {x, y, z};
			Arrays.sort(n);
			return n[2] - n[0] == 2;
		}
		
		@Override
		public String toString() {
			String s = x + " " + y + " " + z;
			if (isSurprising()) {
				s += " (*)";
			}
			return s;
		}
		
		int bestResult() {
			return z;
		}

		@Override
		public int compareTo(Triplet another) {
			if (this.bestResult() > another.bestResult()) {
				return 1;
			} else if (this.bestResult() < another.bestResult()) {
				return -1;
			} else {
				return 0;
			}
		}
	}

	int solveCase(String input) {
		String[] args = input.split(" ");

		// Number of Googlers
		int N = Integer.valueOf(args[0]);

		// Number of surprising triplets
		int S = Integer.valueOf(args[1]);

		// Min best score
		int p = Integer.valueOf(args[2]);

		// Total points of Googlers
		int[] t = new int[args.length - 3];
		for (int i = 0; i < t.length; i++) {
			t[i] = Integer.valueOf(args[i + 3]);
		}

		if (DEBUG)
			printInput(N, S, p, t);

		return calcResult(N, S, p, t);
	}

	int calcResult(int N, int S, int p, int[] t) {
		int result = 0;

		List<Googler> googlers = new ArrayList<Googler>();
		
		// For each Googler
		for (int i = 0; i < N; i++) {
			// Calculate possible combinations of scores
			List<Triplet> triplets = getCombinations(t[i]);
			googlers.add(new Googler(triplets));
			if (DEBUG) {
				System.out.println("Googler #" + (i + 1));
				for (Triplet tri : triplets) {
					System.out.println(tri);
				}
				System.out.println("");
			}
		}
		
		// Sort by best score
		Collections.sort(googlers);
		
		int surprising = 0;
		
		for (Googler g : googlers) {
			List<Triplet> qt = g.tripletsWithBestScoreAtLeast(p);
			boolean finishThis = false;
			for (Triplet tri : qt) {
				if (finishThis) {
					continue;
				} else if (!tri.isSurprising()) {
					result++;
					finishThis = true;
					continue;
				} else {
					if (surprising >= S) {
						continue;
					} else {
						surprising++;
						result++;
						finishThis = true;
						continue;
					}
				}
			}
			
		}

		return result;
	}

	List<Triplet> getCombinations(int sum) {
		List<Triplet> triplets = new ArrayList<Triplet>();
		for (int x = 0; x <= 10; x++) {
			for (int y = 0; y <= 10; y++) {
				for (int z = 0; z <= 10; z++) {
					Triplet triplet = new Triplet(x, y, z);
					if (triplet.sum() == sum) {
						if (triplet.z - triplet.x > 2) {
							// Impossible case, skip
							continue;
						} else if(triplets.contains(triplet)) {
							// Don't add if duplicate
							continue;
						} else {
							triplets.add(triplet);
						}
					} else {
						// Sum is not proper, skip
						continue;
					}
				}
			}
		}
		return triplets;
	}

	static int factorial(int n) {
		if (n <= 1) {
			return 1;
		} else {
			return n * factorial(n - 1);
		}
	}

	static void printInput(int N, int S, int p, int[] t) {
		String output = "\n";
		output += N + " Googlers, ";
		output += S + " surprising results. ";
		output += "How many had best score equal to or greater than " + p + "? ";
		output += "(scores: ";
		for (int i = 0; i < t.length; i++) {
			if (i != 0) {
				output += ", ";
			}
			output += t[i];
		}
		output += ")";
		System.out.println(output);
	}

	public static void main(String... args) {
		DancingWithTheGooglers dancing = new DancingWithTheGooglers();
		
		try {

			BufferedReader c = new BufferedReader(new InputStreamReader(System.in));

			// Number of test cases
			int T = Integer.valueOf(c.readLine());

			System.out.println();

			// Do, for each test case
			for (int i = 0; i < T; i++) {

				// Googlerese string
				String input = c.readLine();

				// Process input
				int output = dancing.solveCase(input);

				// Print the translated text
				System.out.println("Case #" + (i + 1) + ": " + output);

			}

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

}
