import java.io.*; // Java I/O import java.util.*; public class Problem4 { public static void main (String args[]) throws IOException { BufferedReader input = new BufferedReader (new FileReader ("DATA41.txt")); PrintWriter output = new PrintWriter (new FileWriter ("OUT41.txt")); for (int cases = 0 ; cases < 5 ; cases++) { int types = Integer.parseInt (input.readLine ()); int[] [] blocks = new int [2] [types]; for (int i = 0 ; i < types ; i++) { StringTokenizer st = new StringTokenizer (input.readLine ()); blocks [0] [i] = Integer.parseInt (st.nextToken ()); blocks [1] [i] = Integer.parseInt (st.nextToken ()); } int total = Integer.parseInt (input.readLine ()); for (int y = 0 ; y < blocks [0].length - 1 ; y++) { for (int z = y + 1 ; z < blocks [0].length ; z++) { if (blocks [0] [y] < blocks [0] [z]) { int temp = blocks [0] [y]; int temp2 = blocks [1] [y]; blocks [0] [y] = blocks [0] [z]; blocks [1] [y] = blocks [1] [z]; blocks [0] [z] = temp; blocks [1] [z] = temp2; } } } int[] heights = new int [total + 1]; for (int i = 1 ; i < heights.length ; i++) { heights [i] = -1; } int[] [] b = new int [heights.length] [types]; for (int i = 1 ; i <= total ; i++) { int min = Integer.MAX_VALUE; int minClub = -1; for (int j = 0 ; j < types ; j++) { try { if (b [i - blocks [0] [j]] [j] + 1 <= blocks [1] [j]) { if (heights [i - blocks [0] [j]] + 1 < min && heights [i - blocks [0] [j]] != -1) { min = heights [i - blocks [0] [j]] + 1; minClub = j; } } } catch (ArrayIndexOutOfBoundsException e) { } } if (min != Integer.MAX_VALUE) { heights [i] = min; for (int k = 0 ; k < types ; k++) { b [i] [k] = b [i - blocks [0] [minClub]] [k]; } b [i] [minClub]++; } } } output.close (); } }