# Arup Guha
# Started 9/11/2026, completed 10/28/2026
# Not another Polyomino!

'''
# Note: It's unclear to me if this is actually correct. But, it passes all of
        test data, and then I ran quite a few other cases against the provably
        correct solution (Thomas's C++ one), and this solution produces the
        same answers on everything I threw at it, which includes:

        (a) All 100000 test cases of the form a b c d e with a, b, c, d and e
            in between 0 and 9, inclusive.

        (b) 1000 random cases where each item is in between 0 and 5000
        (c) 1000 random cases where each item is in between 0 and 1000000
        (d) 1000 random cases in 32 bins where, for all 32 masks, if there's
            a 0 bit I put 0 of that time and if there's 1 then I put a random
            number of that tile in between 0 and 1000000.

        Unfortunately, I can't come up with a proof this is correct, and the
        strategy is nearly "opposite" of the provably correct one.

        I recognized that we want to maximize 4 packs also. My first observation
        was that for everything but S, we could do 4 packs. So to me, S's were
        critical, because if I have leftover S's, they have to go in 3 packs.
        So my first goal was to get rid of S's. There are 3 four packs that
        have S's, so I try taking those first. Then I have two more packs with
        1 of something, then followed by pairs and then 4s.

        I found out for me that it's bad to go down too far on any tile except
        S, so what I did was when I take packs, I make sure I leave in between
        0 and 3 of S and 4 and 7 of everything else. Basically, I am leaving
        a buffer and I handle that buffer with a brute force which I run only
        once before I handle cases.

        So, instead of whittling it down all the way, I whittle down each case
        taking perfect packs (and then at the very end if there are too many S's
        I get these down to 7 or less), and I make sure what's left if "enough"
        so that my DP table (which has all perfect answers for in between 0 and 7
        of everything so I pre-store the answer to 2^15 cases...) and at the very
        end I just add the optimal number of packs for what's left.

        So what I can't prove is that what I am doing doesn't miss a possible 4 pack.
        But it works for all of the cases I tried and all of the judge data. I think
        it's possible that someone else could prove this correct, that I am leaving
        enough leeway with my greedy. Thomas's solution takes a limited number of
        packs that I call important, whereas I take a lot of these packs. This is why
        our approaches are almost opposite. What he does is only run his brute force
        with my important packs and he knows he'll never need more than 1 or 3 of these
        important packs because 2 or 4 of them can be replicated with our greedy set.
        It's easy to see why his logic is correct.

        Anyway, I've decided rather than copy his approach, I'll just post mine that
        worked. Of course, I did use the data to find cases mine failed on and then
        even when mine passed, I had Thomas find another case mine failed on (that's
        when I got the idea to just brute force everything small...)

'''

# This returns 1 short of the most number of this pack you could buy.
def getLimit(items,pack, isS):

    # Value to be overwritten.
    res = sum(items)

    # Look for limiting factor.
    for i in range(len(items)):
        if pack[i] == 0:
            continue

        # Update based on type i polynomino.
        res = min(res, items[i]//pack[i])

        limit = 8
        if isS:
            limit = 4
            
        left = items[i] - res*pack[i]
        while left + pack[i] < limit:
            left += pack[i]
            res -= 1
            
    # Okay, so here's where I go one short, to leave room for the DP so that nothing
    # ever gets completely emptied out until the end.
    return max(res,0)

# Once I commit to a certain packing, this updates items based on quantity number of pack.
def update(items,pack,quantity):
    for i in range(len(items)):
        items[i] -= (pack[i]*quantity)

# I just do this once; I am building a DP table to get all optimal answers upto 7 of each.
def buildDP():

    # All the four packs I was able to draw...oh, I did cheat and add a couple after using test case
    # 3 or 4.
    allP = [[0,2,0,2,0],[1,2,0,1,0],[0,1,0,1,2],[1,1,0,0,2],[1,2,1,0,0],
           [2,2,0,0,0],[2,0,2,0,0],[0,2,2,0,0],[4,0,0,0,0],[0,4,0,0,0],
           [0,0,4,0,0],[0,0,0,0,4]]

    # Five numbers 0 to 7 is 15 bits.
    table = [0]*(1<<15)

    # Run the DP.
    for mask in range(1,(1<<15)):

        # Turn this mask into a sub-problem (# of each type of polyomino)
        tmp = []
        bits = mask
        tot = 0
        for i in range(5):
            tmp.append(bits&7)
            tot += (bits&7)
            bits >>= 3

        # Worst we could do.
        table[mask] = (tot+2)//3

        # Try creating mask by using each different pack.
        for pack in allP:

            # Compute the old mask that we would add pack to, to get mask.
            oldM = 0
            for i in range(5):
                oldM += ((max(0,tmp[i]-pack[i])) << (3*i))

            # Update our answer using >=1 copy of pack.
            table[mask] = min(table[mask], table[oldM] + 1)

    # Return the whole thing.
    return table
            
def solve(items, table):

    # These are all the 4 packs except for all 4 of the same type
    important = [[0,2,0,2,0],[0,1,0,1,2],[1,2,0,1,0]]
    nextimp = [[1,1,0,0,2],[1,2,1,0,0]]
    twopairs  = [[2,2,0,0,0],[2,0,2,0,0],[0,2,2,0,0]]
    fours = [[4,0,0,0,0],[0,4,0,0,0],[0,0,4,0,0],[0,0,0,0,4]]

    # So I take important ones, making sure S remains the largest possible value
    # less than 4.
    res = 0
    for pack in important:
        tmp = getLimit(items, pack, True)
        res += tmp
        update(items, pack, tmp)

    # For these, I make sure all items except S remain as large as possible below 8.
    for pack in nextimp:
        tmp = getLimit(items, pack, False)
        res += tmp
        update(items, pack, tmp)
    for pack in twopairs:
        tmp = getLimit(items, pack, False)
        res += tmp
        update(items, pack, tmp)

    # Same here...this is supposed to take big numbers down...
    for pack in fours:
        tmp = getLimit(items, pack, False)
        res += tmp
        update(items, pack, tmp)

    # Due to limited options for S's, we can suck it up and get these down
    # to 7 or less.
    if items[3] > 7:
        needP = (items[3] - 7 + 2)//3
        res += needP
        items[3] -= 3*needP

    # Now that our case is down to 7 or less of everything, run the DP.
    mask = 0
    for i in range(5):
        mask += (items[i]<<(3*i))

    # Here's our answer, greedy until we get down to small numbers, then DP the rest.
    return res + table[mask]
    
# Get # of cases.
nC = int(input())

# Build table only once
table = buildDP()

# Process cases!
for i in range(nC):
    items = [int(x) for x in input().split()]
    print(solve(items,table))
    
