//CF: aayuprasad #include "bits/stdc++.h" using namespace std; #define prec(n) fixed< #define pii pair #define mii map #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<<"="<> tt; for(int t = 1; t <= tt; t++) { int n,m; cin>>n>>m; int max_val=0; vi res; vector> a; for(int i=0;i>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> 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 #"<