/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IO;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
    
/**
 * 
 * @author dannocz
 */
public class InputReader {
    
    public static Input readFile(String filename){
        try {
              
              /*  Sets up a file reader to read the file passed on the command
          77        line one character at a time */
              FileReader input = new FileReader(filename);
              
              /* Filter FileReader through a Buffered read to read a line at a
                 time */
              BufferedReader bufRead = new BufferedReader(input);
              
              String line;    // String that holds current file line
              int count = 0;  // Line number of count 
              ArrayList<String> cases= new ArrayList<String>();
              
              // Read first line
              line = bufRead.readLine();
              int noTestCases=Integer.parseInt(line);
              count++;
              
              // Read through file one line at time. Print line # and line
              while (count<=noTestCases){
                  //System.out.println("Reading. "+count+": "+line);
                  line = bufRead.readLine();
                  cases.add(line);
                  count++;
              }
              
              bufRead.close();
              
              return new Input(noTestCases,cases);
              
          }catch (ArrayIndexOutOfBoundsException e){
              /* If no file was passed on the command line, this expception is
              generated. A message indicating how to the class should be
              called is displayed */
              e.printStackTrace();        
  
          }catch (IOException e){
              // If another exception is generated, print a stack trace
              e.printStackTrace();
          }
        
        return null;
          
      }// end main
    }

package com.cuteants.problems.codejam.y2012.qualification;

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

public class DancingWithTheGooglers {

    public static void main(String[] args) throws Exception {
        InputReader sc = new StreamInputReader(new FileInputStream("C:\\Users\\aurel\\Downloads\\B-large.in"));
        PrintWriter out = new PrintWriter(new BufferedOutputStream(new FileOutputStream("C:\\Users\\aurel\\Downloads\\B_large_out.txt"), 10000), false);

        int tests = sc.nextInt();
        for (int i = 1; i <= tests; i++) {
            int n = sc.nextInt();
            int s = sc.nextInt();
            int p = sc.nextInt();

            int sum = 0;
            for (int j = 0; j < n; j++) {
                int t = sc.nextInt();
                int a = t / 3;
                switch (t % 3) {
                    case 0:
                        if (a >= p) {
                            // a + a + a
                            sum++;
                        } else if (s > 0 && a + 1 >= p && a >= 1 && a <= 9) {
                            // (a - 1) + a + (a + 1)
                            sum++;
                            s--;
                        }
                        break;
                    case 1:
                        if (a + 1 >= p) {
                            // a + a + (a + 1)
                            sum++;
                        }
                        break;
                    case 2:
                        if (a + 1 >= p) {
                            // a + (a + 1) + (a + 1)
                            sum++;
                        } else if (s > 0 && a + 2 >= p && a <= 8) {
                            // a + a + (a + 2)
                            sum++;
                            s--;
                        }
                        break;
                }
            }
            out.println("Case #" + i + ": " + sum);
        }
        out.close();
    }

    abstract static class InputReader {

        public abstract int read();

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public long readLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }

    static class StreamInputReader extends InputReader {

        private InputStream stream;
        private byte[] buf = new byte[2048];
        private int curChar, numChars;

        public StreamInputReader(InputStream stream) {
            this.stream = stream;
        }

        @Override
        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }
    }

}
