#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#ifdef PRINTERS
#include "printers.hpp"
using namespace printers;
#define tr(a) cerr<<#a<<": "<<a<<endl;
#else
#define tr(a)
#endif
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define hell 1000000007
#define endl '\n'
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int test=1;
void solve(){
cout<<"Case #"<<test++<<": ";
int AC,AJ;
cin>>AC>>AJ;
int N=AC+AJ;
vector<pair<pii,int>>v(AC+AJ);
int ACcnt=0,AJcnt=0,othercnt=0,ACmid=0,AJmid=0;
vi AJs,ACs;
rep(i,0,AC){
cin>>v[i].F.F>>v[i].F.S;
v[i].S=1;
ACcnt+=v[i].F.S-v[i].F.F;
}
rep(i,0,AJ){
cin>>v[AC+i].F.F>>v[AC+i].F.S;
v[AC+i].S=2;
AJcnt+=v[AC+i].F.S-v[AC+i].F.F;
}
sort(all(v));
int ans=0;
rep(i,0,N){
auto x=v[i];
auto y=v[(i+1)%N];
if(y.F.F<x.F.S)y.F.F+=1440,y.F.S+=1440;
if(x.S!=y.S){
othercnt+=(y.F.F-x.F.S);
ans++;
}
else if(x.S==1){
ACmid+=(y.F.F-x.F.S);
ACs.pb(y.F.F-x.F.S);
}
else{
AJmid+=(y.F.F-x.F.S);
AJs.pb(y.F.F-x.F.S);
}
}
sort(all(ACs),greater<int>());
sort(all(AJs),greater<int>());
if(ACcnt+othercnt+ACmid>=720 and AJcnt+othercnt+AJmid>=720){
cout<<ans<<endl;
return;
}
if(ACcnt+othercnt+ACmid<720){
int reqd=720-(ACcnt+othercnt+ACmid);
for(auto i:AJs){
if(reqd>0)reqd-=i,ans+=2;
}
cout<<ans<<endl;
return;
}
if(AJcnt+othercnt+AJmid<720){
int reqd=720-(AJcnt+othercnt+AJmid);
for(auto i:ACs){
if(reqd>0)reqd-=i,ans+=2;
}
cout<<ans<<endl;
return;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGJpdHNldD4KI2luY2x1ZGUgPGNhc3NlcnQ+CiNpbmNsdWRlIDxjbGltaXRzPgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDxjc3RkaW8+CiNpbmNsdWRlIDxjc3RkbGliPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGRlcXVlPgojaW5jbHVkZSA8aW9tYW5pcD4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8bnVtZXJpYz4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8c2V0PgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlIDxzdHJpbmc+CgojaWZkZWYgUFJJTlRFUlMKI2luY2x1ZGUgInByaW50ZXJzLmhwcCIKdXNpbmcgbmFtZXNwYWNlIHByaW50ZXJzOwojZGVmaW5lIHRyKGEpICAgICAgICBjZXJyPDwjYTw8IjogIjw8YTw8ZW5kbDsKI2Vsc2UKI2RlZmluZSB0cihhKSAgICAKI2VuZGlmCgojZGVmaW5lIGxsICAgICAgICAgIGxvbmcgbG9uZwojZGVmaW5lIHBiICAgICAgICAgIHB1c2hfYmFjawojZGVmaW5lIG1wICAgICAgICAgIG1ha2VfcGFpcgojZGVmaW5lIHBpaSAgICAgICAgIHBhaXI8aW50LGludD4KI2RlZmluZSB2aSAgICAgICAgICB2ZWN0b3I8aW50PgojZGVmaW5lIGFsbChhKSAgICAgIChhKS5iZWdpbigpLChhKS5lbmQoKQojZGVmaW5lIEYg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k7CiAgICAgICAgICAgIGFucysrOwogICAgICAgIH0KICAgICAgICBlbHNlIGlmKHguUz09MSl7CiAgICAgICAgICAgIEFDbWlkKz0oeS5GLkYteC5GLlMpOwogICAgICAgICAgICBBQ3MucGIoeS5GLkYteC5GLlMpOwogICAgICAgIH0KICAgICAgICBlbHNlewogICAgICAgICAgICBBSm1pZCs9KHkuRi5GLXguRi5TKTsKICAgICAgICAgICAgQUpzLnBiKHkuRi5GLXguRi5TKTsKICAgICAgICB9CiAgICB9CgogICAgc29ydChhbGwoQUNzKSxncmVhdGVyPGludD4oKSk7CiAgICBzb3J0KGFsbChBSnMpLGdyZWF0ZXI8aW50PigpKTsKICAgIGlmKEFDY250K290aGVyY250K0FDbWlkPj03MjAgYW5kIEFKY250K290aGVyY250K0FKbWlkPj03MjApewogICAgICAgIGNvdXQ8PGFuczw8ZW5kbDsKICAgICAgICByZXR1cm47CiAgICB9CiAgICBpZihBQ2NudCtvdGhlcmNudCtBQ21pZDw3MjApewogICAgICAgIGludCByZXFkPTcyMC0oQUNjbnQrb3RoZXJjbnQrQUNtaWQpOwogICAgICAgIGZvcihhdXRvIGk6QUpzKXsKICAgICAgICAgICAgaWYocmVxZD4wKXJlcWQtPWksYW5zKz0yOwogICAgICAgIH0KICAgICAgICBjb3V0PDxhbnM8PGVuZGw7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgaWYoQUpjbnQrb3RoZXJjbnQrQUptaWQ8NzIwKXsKICAgICAgICBpbnQgcmVxZD03MjAtKEFKY250K290aGVyY250K0FKbWlkKTsKICAgICAgICBmb3IoYXV0byBpOkFDcyl7CiAgICAgICAgICAgIGlmKHJlcWQ+MClyZXFkLT1pLGFucys9MjsKICAgICAgICB9CiAgICAgICAgY291dDw8YW5zPDxlbmRsOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIAp9CgppbnQgbWFpbigpewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKDApOwogICAgY291dC50aWUoMCk7CiAgICBpbnQgdD0xOwogICAgY2luPj50OwogICAgd2hpbGUodC0tKXsKICAgICAgICBzb2x2ZSgpOwogICAgfQogICAgcmV0dXJuIDA7Cn0=
NQoxIDEKNTQwIDYwMAo4NDAgOTAwCjIgMAo5MDAgMTI2MAoxODAgNTQwCjEgMQoxNDM5IDE0NDAKMCAxCjIgMgowIDEKMTQzOSAxNDQwCjE0MzggMTQzOQoxIDIKMyA0CjAgMTAKMTQyMCAxNDQwCjkwIDEwMAo1NTAgNjAwCjkwMCA5NTAKMTAwIDE1MAoxMDUwIDE0MDA=
5
1 1
540 600
840 900
2 0
900 1260
180 540
1 1
1439 1440
0 1
2 2
0 1
1439 1440
1438 1439
1 2
3 4
0 10
1420 1440
90 100
550 600
900 950
100 150
1050 1400