Untitled
3 years ago in C
//CF: aayuprasad
#include "bits/stdc++.h"
using namespace std;
#define prec(n) fixed<<setprecision(n)
#define int long long int
#define fast() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(x) begin(x),end(x)
#define rz(x) resize(x)
#define asn(x,y) assign(x,y)
#define mem(a,b) memset(a,b,sizeof(a))
#define sz(x) ((int)size(x))
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ins insert
#define vi vector<int>
#define pii pair<int,int>
#define mii map<int,int>
#define F first
#define S second
#define remax(a,b) a=max(a,b)
#define remin(a,b) a=min(a,b)
#define bitcount(x) __builtin_popcountll(x)
#define iceil(n,x) (((n)-1)/(x)+1)
#define flush fflush(stdout)
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define fo(i,n) for(int i=0;i<(n);++i)
using ull=unsigned long long;
using ll=long long;
using ld=long double;
const ld pi=acos(-1);
const ll inf=1e18,M=1e9+7;
const int N=1;
const string alphabet="abcdefghijklmnopqrstuvwxyz";
int32_t main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
fast();
ll tt = 1;
cin >> tt;
for(int t = 1; t <= tt; t++) {
int n,m;
cin>>n>>m;
int max_val=0;
vi res;
vector<pair<int,int>> a;
for(int i=0;i<n;i++){
int l,r;
cin>>l>>r;
a.pb({l,r});
max_val=max(max_val,r);
}
vi s;
fo(i,m){
int x;
cin>>x;
s.pb(x);
}
vi suffix(max_val,0);
for(auto& p: a){
int l=p.first,r=p.second;
suffix[r-1]++;
suffix[l]--;
}
for(int i=max_val-2;i>=0;i--) suffix[i]+=suffix[i+1];
vector<pair<int,int>> b;
fo(i,max_val){
if(suffix[i]!=0) b.pb({i,suffix[i]});
}
fo(i,m){
int sk=s[i];
auto it=lower_bound(all(b),{sk,0});
if(it==b.end()){
auto it1=it-1;
it1->second--;
res.pb(it1->first);
if(it1->second==0)b.erase(it1);
}
else if(it->first==sk&&it->second!=0){
it->second--;
res.pb(it->first);
if(it->second==0)b.erase(it);
}
else{
if(it!=b.begin()){
auto it1=it-1;
if(sk-it1->first<=it->first-sk){
it1->second--;
res.pb(it1->first);
if(it1->second==0) b.erase(it1);
}
else{
it->second--;
res.pb(it->first);
if(it->second==0)b.erase(it);
}
}
else{
it->second--;
res.pb(it->first);
if(it->second==0)b.erase(it);
}
}
}
cout<<"Case #"<<t<<": ";
fo(i,m)cout<<res[i]<<' ';cout<<'\n';
}
return 0;
}