// Arup Guha
// 1/27/2023
// Solution to COP 4516 Ind Contest 3 Problem: Ordering Paper

using namespace std;

#include <iostream>

int main() {

    int nC;
    cin >> nC;

    // Process cases.
    for (int loop=0; loop<nC; loop++) {

        int n, res = 0;
        cin >> n;

        // Add in each exam contribution.
        for (int i=0; i<n; i++) {

            // We want ceil(pages/4) sheets.
            int nS, p;
            cin >> nS >> p;
            int sheets = (p+3)/4;

            // This is the total contribution for this exam.
            res += (nS*sheets);
        }

        // Ta da!
        cout << res << endl;
    }

    return 0;
}
