com sed :
sed '
/^Query_/{ #starts loop when meet patten
:a
$!{
N
/\nQuery_/!ba #untill meet next pattern
}
/\(\n.*\)\{6,\}/{ #checks how many lines in block
$b #for end of file
s/\nQuery_/\n&/ #marks lines to print
}
}
/\n\n/P #prints marked lines
D #remove 1st line in block, go to start
'
Outro formato de script para awk :
awk '
/^Query/{c=0;lines=$0;next}
++c<5{lines=lines "\n" $0;next}
c==5{print lines}
1 #short for {print}
'