Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	x := make([]int, 100)
	y := make([]int, 100)
	r := make([]int, 100)
	for scanner.Scan() {
		a, _ := strconv.Atoi(scanner.Text())
		if a == 0 {
			break
		}
		for i := 0; i < a; i++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			tmpx, _ := strconv.Atoi(input[0])
			tmpy, _ := strconv.Atoi(input[1])
			tmpr, _ := strconv.Atoi(input[2])
			x[i], y[i], r[i] = tmpx, tmpy, tmpr
		}
		scanner.Scan()
		b, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < b; i++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			c, _ := strconv.Atoi(input[0])
			d, _ := strconv.Atoi(input[1])
			e, _ := strconv.Atoi(input[2])
			f, _ := strconv.Atoi(input[3])
			ok := false
			for j := 0; j < a; j++ {
				if (c-x[j])*(c-x[j])+(d-y[j])*(d-y[j]) >= r[j]*r[j] || (e-x[j])*(e-x[j])+(f-y[j])*(f-y[j]) >= r[j]*r[j] {
					if (e-c)*(x[j]-c)+(f-d)*(y[j]-d) < 0 {
						if (x[j]-c)*(x[j]-c)+(y[j]-d)*(y[j]-d) <= r[j]*r[j] {
							ok = true
						}
					} else {
						if (e-c)*(x[j]-c)+(f-d)*(y[j]-d)>(e-c)*(e-c)+(f-d)*(f-d) {
							if (x[j]-e)*(x[j]-e)+(y[j]-f)*(y[j]-f)<=r[j]*r[j] {
								ok = true
							}
						} else {
							if (x[j]-c)*(x[j]-c)+(y[j]-d)*(y[j]-d)-((e-c)*(x[j]-c)+(f-d)*(y[j]-d))*((e-c)*(x[j]-c)+(f-d)*(y[j]-d))/((e-c)*(e-c)+(f-d)*(f-d))<=r[j]*r[j] {
								ok = true
							}
						}
					}

				}
			}
			if ok {
				fmt.Println("Safe")
			} else {
				fmt.Println("Danger")
			}
		}
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Wall { int x, y, r; };

bool inside(Wall w, double x, double y) {
	return w.r * w.r >= (w.x - x) * (w.x - x) + (w.y - y) * (w.y - y);
}

int main() {
	int n, wx, wy, r, m, tx, ty, sx, sy;
	while (cin >> n, n) {
		vector<Wall> w;
		for (int i=0; i<n; ++i) {
			cin >> wx >> wy >> r;
			w.push_back((Wall){wx, wy, r});
		}
		cin >> m;
		for (int i=0; i<m; ++i) {
			cin >> tx >> ty >> sx >> sy;
			bool safe = false;
			for (int j=0; j<n && !safe; ++j) {
				int k = inside(w[j], tx, ty) + inside(w[j], sx, sy);
				if (k == 0) {
					double dx, dy, x, y, minx, maxx, miny, maxy;
					dx = (sx - tx) / 100000.0;
					dy = (sy - ty) / 100000.0;
					x = tx;
					y = ty;
					minx = min(sx, tx);
					maxx = max(sx, tx);
					miny = min(sy, ty);
					maxy = max(sy, ty);
					while ( ! safe && minx <= x && x <= maxx && miny <= y && y <= maxy) {
						if (inside(w[j], x, y)) {
							safe = true;
							break;
						}
						x += dx;
						y += dy;
					}
				} else if (k == 1) {
					safe = true;
				}
			}
			cout << (safe ? "Safe" : "Danger") << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <algorithm>

using namespace std;

int distsq(int x1, int y1, int x2, int y2){
	return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
}

// 線分と円の外周との当たり判定
bool checkLineCircle(int tx, int ty, int sx, int sy, int wx, int wy, int wr){
	bool t_in = (distsq(tx, ty, wx, wy) < wr*wr);
	bool s_in = (distsq(sx, sy, wx, wy) < wr*wr);
	if(t_in && s_in) return false; // お互いが中なら接触していない
	if(t_in != s_in) return true; // お互いの中/外が異なれば接触している

	// 線分の始点/終点が円の外部
	int vx = sx - tx; // 始点→終点 へのベクトル
	int vy = sy - ty;
	int cx = wx - tx; // 始点→円の中心 へのベクトル 
	int cy = wy - ty;
	int cv = vx*cx + vy*cy; // cvの内積
	int vv = vx*vx + vy*vy;
	int cc = cx*cx + cy*cy;
	if(cv < 0) return false; // 内積<0 -> 円の中心は線分の始点方向の外
	if(cv > vx*vx+vy*vy) return false; // |c・v|>|v・v| なら 円の中心は終点方向の外
	if( cc-cv*cv/vv <= wr*wr) return true; // c^2 - (c*cosθ)^2 < wr^2 なら接触
	return false;
}

int main(){
	int n, m;
	while(cin >> n, n){
		vector<int> wx(n), wy(n), wr(n);
		for(int i = 0; i < n; i++){
			cin >> wx[i] >> wy[i] >> wr[i];
		}
		cin >> m;

		int tx, ty, sx, sy;
		for(int i = 0; i < m; i++){
			cin >> tx >> ty >> sx >> sy;
			bool safe = false;
			for(int j = 0; j < n; j++){
				if(checkLineCircle(tx, ty, sx, sy, wx[j], wy[j], wr[j])){
					safe = true;
					break;
				}
			}
			if(safe) cout << "Safe" << endl;
			else cout << "Danger" << endl;
		}

	}

	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <climits>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <functional>
#include <set>
#include <numeric>
#include <stack>
#include <utility>
#include <time.h>
#include <iterator>
//#include "util.h"

using namespace std;
typedef unsigned uint;
typedef long long lint;
typedef unsigned long long ulint;



//??????
template <typename _KTy, typename _Ty> ostream& operator << (ostream& ostr, const pair<_KTy, _Ty>& m) { cout << "{" << m.first << ", " << m.second << "}"; return ostr; }
template <typename _KTy, typename _Ty> ostream& operator << (ostream& ostr, const map<_KTy, _Ty>& m) { if (m.empty()) { cout << "{ }"; return ostr; } cout << "{" << *m.begin(); for (auto itr = ++m.begin(); itr != m.end(); itr++) { cout << ", " << *itr; } cout << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const vector<_Ty>& v) { if (v.empty()) { cout << "{ }"; return ostr; }	cout << "{" << v.front(); for (auto itr = ++v.begin(); itr != v.end(); itr++) { cout << ", " << *itr; }	cout << "}"; return ostr; }
template <typename _Ty> ostream& operator << (ostream& ostr, const set<_Ty>& s) { if (s.empty()) { cout << "{ }"; return ostr; } cout << "{" << *(s.begin()); for (auto itr = ++s.begin(); itr != s.end(); itr++) { cout << ", " << *itr; }	cout << "}"; return ostr; }
//template <typename T> void print(T* v, int N) { if (N == 0) cout << "{ }"; cout << "{" << v[0]; for (int i = 1; i < N; i++) cout << ", " << v[i]; cout << "}"; }
#define PI 3.14159265358979323846
#define EPS 1e-8
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define all(x) (x).begin(), (x).end()

typedef pair<int, int> pii;
typedef pair<pii, int> pi3;

bool inCircle(pi3 circle, pii point)
{
	int cx = circle.first.first;
	int cy = circle.first.second;
	int r = circle.second;
	int x = point.first;
	int y = point.second;

	return ((cx - x) * (cx - x) + (cy - y) * (cy - y) < r * r);
}

int main()
{
	while (1) {
		int n;
		cin >> n;
		if (n == 0) break;

		vector<pi3> w(n);
		for (int i = 0; i < n; i++) cin >> w[i].first.first >> w[i].first.second >> w[i].second;

		int m;
		cin >> m;
		
		for (int i = 0; i < m; i++) {
			int tx, ty, sx, sy;
			cin >> tx >> ty >> sx >> sy;
			string t_state, s_state;
			for (int j = 0; j < n; j++) {
				t_state.push_back((inCircle(w[j], pii(tx, ty)) ? '1' : '0'));
				s_state.push_back((inCircle(w[j], pii(sx, sy)) ? '1' : '0'));
			}
			if (t_state != s_state) {
				cout << "Safe" << endl;
				continue;
			}

			bool danger = true;
			for (int j = 0; j < n; j++) {
				// ???????????¨???????????´??????????????????
				if (t_state[j] == '1') continue;

				int cx = w[j].first.first;
				int cy = w[j].first.second;
				int r = w[j].second;

				int dct, dts, dsc;
				dct = (cx - tx) * (cx - tx) + (cy - ty) * (cy - ty);
				dts = (tx - sx) * (tx - sx) + (ty - sy) * (ty - sy);
				dsc = (sx - cx) * (sx - cx) + (sy - cy) * (sy - cy);

				if (dct + dts <= dsc || dsc + dts <= dct) continue;

				int a = sy - ty;
				int b = tx - sx;
				int c = sy * (sx - tx) - sx * (sy - ty);

				// r < d
				if (r * r * (a * a + b * b) < (a * cx + b * cy + c) * (a * cx + b * cy + c)) continue;
				
				danger = false;
				break;
			}

			cout << (danger ? "Danger" : "Safe") << endl;
		}
	}

	return 0;
}