#include <cstdio>
#include <algorithm>
using namespace std;
double A[55], tmp;
int B[55], Q, N, ans;
bool vis[55];
void dfs(int d, int x, double p, int s)
{
    if (d > 3) return;
    if (int(p) == p && int(p) % 5 == 0)
        ans = max(ans, s);
    for (int i = 0; i < N; i++)
        if (!vis[i])
        {
            vis[i] = true;
            dfs(d + 1, i, p + A[i], s + B[i]);
            vis[i] = false;
        }
}
int main()
{
    scanf("%d", &Q);
    while (Q--)
    {
        ans = 0;
        scanf("%d", &N);
        for (int i = 0; i < N; i++)
            scanf("%lf%d", A + i, B + i);
        dfs(0, 0, 0, 0);
        printf("%d\n", ans);
    }
    return 0;
}