import java.util.*;
import java.io.*;

/**
* ACM Problem #11638 (Temperature Alarm)
*
* @author Felix Dietrich
* @version 1.0, 31/07/09
*
* Status : Accepted
*/
class Main
{

static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static void main(String... args) throws IOException
{
int n = Integer.parseInt(br.readLine().trim());

while (n > 0)
{
work();
n--;
}
}

static int casenumber = 1;

public static void work() throws IOException
{
int mI = Integer.parseInt(br.readLine().trim());
int sD = Integer.parseInt(br.readLine().trim());

StringTokenizer st = new StringTokenizer(br.readLine());
int t1 = Integer.parseInt(st.nextToken());
int t2 = Integer.parseInt(st.nextToken());
int t3 = Integer.parseInt(st.nextToken());
int t4 = Integer.parseInt(st.nextToken());

int C = Integer.parseInt(br.readLine().trim());

Alarms a = new Alarms(mI, sD, t1, t2, t3, t4, C);

int k = Integer.parseInt(br.readLine().trim());

while (k > 0)
{
st = new StringTokenizer(br.readLine());
a.check(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
k--;
}

printNumber(a);
}

private static void printNumber(Alarms a)
{
System.out.println(String.format("Case %d: %s %s %s %s", casenumber, a.get(0), a.get(1), a.get(2), a.get(3)));
casenumber++;
}

public static class Alarm
{
final int threshold;
final boolean enabled;
final boolean isLowerTrigger;
boolean triggered = false;

public Alarm(int t1, int c, int num)
{
threshold = t1;
enabled = (c & (int) Math.pow(2, num - 1)) > 0;
isLowerTrigger = ((c >> 4) & (int) Math.pow(2, num - 1)) == 0;
}

@Override
public String toString()
{
return triggered ? "yes" : "no";
}

public void check(int temp)
{
if(!enabled)
return;

if(isLowerTrigger && threshold > temp)
triggered = true;
if(!isLowerTrigger && threshold < temp)
triggered = true;
}
}

public static class Alarms
{
final Alarm[] myalarms = new Alarm[4];
final int mi;
final int sd;
boolean started = false;
int currenttime = 0;

public Alarms(int mi, int sd, int t1, int t2, int t3, int t4, int c)
{
myalarms[0] = new Alarm(t1, c, 1);
myalarms[1] = new Alarm(t2, c, 2);
myalarms[2] = new Alarm(t3, c, 3);
myalarms[3] = new Alarm(t4, c, 4);

this.mi = mi;
this.sd = sd;
}

public String get(int i)
{
return myalarms[i].toString();
}

public void check(int time, int temp)
{
int lasttime = currenttime;
currenttime += time;
if (!started && currenttime < sd)
return;
if (!started)
currenttime -= sd;

// next mi reached
if ((lasttime + time) / mi > 0 || !started)
{
for(Alarm a: myalarms)
{
a.check(temp);
}
}

started = true;

currenttime %= mi;
}

}
}