1 #include <queue>
   2 #include <iostream>
   3 #include <sstream>
   4 #include <string>
   5 #include <algorithm>
   6 #include <vector>
   7 #include <map>
   8 using namespace std;
   9 
  10 #define GI ({int _t; scanf("%d", &_t); _t;})
  11 #define FOR(i, a, b) for (int i=a; i<b; i++)
  12 #define REP(i, a) FOR(i, 0, a)
  13 template<class T> string toString(T n){ostringstream ost;ost<<n;ost.flush();return ost.str();}
  14 int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}
  15 #define DBGV(_v) { REP(_i, _v.size()) { cout << _v[_i] << "\t";} cout << endl;}
  16 
  17 
  18 int main() {
  19 	int kase = 0;
  20 	while (1) {
  21 		int grid[31][31];
  22 		REP(i, 31) REP(j, 31) grid[i][j] = 0;
  23 		map <int, int> lookup;
  24 		int cnt = GI, vcnt = 0;
  25 		if (cnt == 0) break;
  26 		while (cnt--) {
  27 			int v1 = GI, v2 = GI;
  28 			if (lookup.find(v1) == lookup.end()) {
  29 				lookup[v1] = vcnt;
  30 				vcnt++;
  31 			}
  32 			if (lookup.find(v2) == lookup.end()) {
  33 				lookup[v2] = vcnt;
  34 				vcnt++;
  35 			}
  36 			grid[lookup[v1]][lookup[v2]] = grid[lookup[v2]][lookup[v1]] = 1;
  37 		}
  38 		for (map <int, int>::iterator it = lookup.begin(); it != lookup.end(); it++) {
  39 			//cout << it->first << "\t" << it->second << endl;
  40 		}
  41 		while (1) {
  42 			vector <bool> visited(vcnt, false);
  43 			vector <int> time (vcnt, 0);
  44 			int start = GI, ttl = GI;
  45 			int len = ttl;
  46 			if (start == 0 && ttl == 0) break;
  47 			//Do BFS here
  48 			queue <int> q;
  49 			q.push(lookup[start]);
  50 			time[lookup[start]] = ttl+1;
  51 			visited[lookup[start]] = true;
  52 			while (!q.empty()) {
  53 				int cur = q.front();
  54 				//cout << cur << " --> " << time[cur] << endl;
  55 				q.pop();
  56 				REP(i, vcnt) {
  57 					if (grid[cur][i] == 1 && visited[i] == false ) {
  58 						q.push(i);
  59 						visited[i] = true;
  60 						time[i] = time[cur]-1;
  61 					}
  62 				}
  63 			}
  64 			int res = 0;
  65 			REP(i, vcnt) {
  66 				if (time[i] <= 0) res++;
  67 			}
  68 			//cout << endl;
  69 			kase++;
  70 			printf("Case %d: %d nodes not reachable from node %d with TTL = %d.\n", kase, res, start, len);
  71 		}
  72 	}
  73 	return 0;
  74 }