// Stephen Fulwider
//	Tricky Tolls - BHCSI 2010 Programming Contest

import java.io.File;
import java.util.Arrays;
import java.util.Scanner;


public class tolls
{

	public static void main(String[] args) throws Exception
	{
		new tolls();
	}
		
	int N; // number of tolls
	int[] D=new int[100]; // distance between tolls
	int[][] C=new int[2][100]; // 0:early cost 1:late cost
	
	int[][] memo=new int[60][100]; // memo table
	
	tolls() throws Exception
	{
		Scanner in=new Scanner(new File("tolls.in"));
		for (int T=in.nextInt(),TC=1; T-->0; ++TC)
		{
			N=in.nextInt();
			
			// get distances
			for (int i=0; i<N-1; ++i)
				D[i]=in.nextInt();
			D[N-1]=0; // no distance from the last toll to the exit
			
			// get toll costs
			for (int i=0; i<N; ++i)
			{
				C[0][i]=in.nextInt();
				C[1][i]=in.nextInt();
			}
			
			// clear memo table
			for (int[] m : memo)
				Arrays.fill(m, 0, N, -1);
			
			// since we can arrive at the first toll at any time, then try all times and keep min found
			int min=go(0,0);
			for (int t=1; t<60; ++t)
				min=Math.min(min, go(t,0));
			
			System.out.printf("Case #%d: %d%n",TC,min);
		}
	}
	
	// the minimum time to get through toll at,at+1,...,N-1 given the current time%60
	//	since the hours don't matter, we always keep time between 0..59
	//	this keeps our state small
	int go(int time, int at)
	{
		// we're through all the tolls, so no more cost is required
		if (at==N)
			return 0;
		
		// check if we've already solved this problem
		if (memo[time][at]>-1)
			return memo[time][at];
		
		// we have 2 choices:
		//	1) we can go straight through this toll
		//	2) we can wait here until the time changes to the next stage and then go through
		// we simply try both and return the one which results in the lower total cost, storing
		//	the result in our memo table for easy lookup later
		
		// choice 1
		int cost1=tollCost(time,at)+go((time+D[at])%60,at+1);
		
		// choice 2
		int nextTime=getNextTime(time);
		int cost2=(nextTime-time)+tollCost(nextTime,at)+go((nextTime+D[at])%60,at+1);
		
		int res=Math.min(cost1, cost2);
		memo[time][at]=res;
		return res;
	}
	
	// get the cost of the toll at this time at the given toll
	int tollCost(int time, int at)
	{
		time%=60;
		if (time<30) // early toll
			return C[0][at];
		return C[1][at]; // late toll
	}
	
	// get the next minute the toll type changes
	int getNextTime(int time)
	{
		time%=60;
		if (time<30) // next changes to late
			return 30;
		return 60; // next changes to early
	}

}
