import java.util.*;

public class paper {

	final public static int PAGES_PER_SHEET = 4;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Process each case.
		for (int loop=0; loop<numCases; loop++) {

			int n = stdin.nextInt();
			int sheets = 0;

			// Go through each exam.
			for (int i=0; i<n; i++) {
				int exams = stdin.nextInt();
				int pages = stdin.nextInt();

				// This is important - calculate the sheets needed for a single exam.
				int examSheets = (pages + PAGES_PER_SHEET - 1)/PAGES_PER_SHEET;

				// Then this part is easy.
				sheets += (exams*examSheets);
			}

			// Here is the result for the case.
			System.out.println(sheets);
		}
	}
}