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 <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <bitset>
#include <stack>
#include <cstdlib>
#include <complex>
#include <unordered_map>
#define REP(i,n) for(int i = 0; n > i; i++)
#define MODU 33
#define Range(x,a,b) ((a) <= (x) && (x) <= (b))
#define POWT(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
using namespace std;
typedef vector<int> Ivec;
typedef pair<int, int> pii;
typedef long long int ll;

////???????????¬////
const double EPS = 1e-8;
const double INF = 1e12;
typedef complex<double> Pt;//???
namespace std {
	bool operator < (const Pt& a, const Pt& b) {
		return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
	}
}
double cross(const Pt& a, const Pt& b) {
	return imag(conj(a)*b);
}
double dot(const Pt& a, const Pt& b) {
	return real(conj(a)*b);
}

struct Ln : public vector<Pt> {//???
	Ln(const Pt &a, const Pt &b) {
		push_back(a); push_back(b);
	}
};

typedef vector<Pt> Pol;//????§???¢

struct Cir {//???
	Pt p; double r;
	Cir(const Pt &p, double r) : p(p), r(r) { }
};

int ccw(Pt a, Pt b, Pt c) {
	b -= a; c -= a;
	if (cross(b, c) > 0)   return +1;       // counter clockwise
	if (cross(b, c) < 0)   return -1;       // clockwise
	if (dot(b, c) < 0)     return +2;       // c--a--b on line
	if (norm(b) < norm(c)) return -2;       // a--b--c on line
	return 0;
}

Pt projection(const Ln &l, const Pt &p) {
	double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
	return l[0] + t*(l[0] - l[1]);
}
bool intersectSP(const Ln &s, const Pt &p) {//????????????
	return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS; // triangle inequality
}
double distanceSP(const Ln &s, const Pt &p) {
	const Pt r = projection(s, p);
	if (intersectSP(s, r)) return abs(r - p);
	return min(abs(s[0] - p), abs(s[1] - p));
}

bool intersectCL(const Ln &s, const Cir &c) {
	double dis = distanceSP(s, c.p);
	return dis <= c.r;
}
double distancePP(const Pt &p, const Pt &q) {
	return hypot(p.real() - q.real(), p.imag() - q.imag());
}
bool insideCir(Pt &p, Cir &c) {
	return distancePP(p, c.p) <= c.r;
}
int main() {
	int n, m;
	while (scanf("%d", &n)) {
		if (n == 0)
			return 0;
		vector<Cir> walls;
		REP(i, n) {
			double px, py, r;
			scanf("%lf %lf %lf", &px, &py, &r);
			walls.push_back({ {px,py},r });
		}

		scanf("%d", &m);
		REP(i, m) {
			Pt t, s;
			double x, y;
			scanf("%lf %lf", &x, &y);
			t = { x,y };
			scanf("%lf %lf", &x, &y);
			s = { x,y };

			bool f = 0;
			REP(j, n) {
				f = intersectCL({ t,s }, walls[j]);
				if (f) {
					if (insideCir(t, walls[j]) && insideCir(s, walls[j]))
						f = 0;
					else
						break;
				}
			}
			if (f) {
				printf("Safe\n");
			}
			else
				printf("Danger\n");
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define EPS (1e-8)

typedef complex<double> P;
typedef complex<double> Vec;

struct Line {
	P p;
	Vec v;
	
	Line(P p, Vec v) : p(p), v(v) {}
	
	P getPoint(double t) {
		return p + v * t;
	}
};

struct Circle {
	P p;
	double r;
	
	Circle() {}
	Circle(P p, double r) : p(p), r(r) {}
};

double dot(Vec a, Vec b) {
	return a.real() * b.real() + a.imag() * b.imag();
}

double cross(Vec a, Vec b) {
	return a.real() * b.imag() - a.imag() * b.real();
}

bool isInCircle(P p, Circle c) {
	return norm(c.p - p) < c.r*c.r + EPS;
}

bool isIntersectionSC(Line s, Circle c) {
	if (isInCircle(s.p, c) && isInCircle(s.getPoint(1.0), c)) return false;
	
	Vec a = c.p - s.p, b = c.p - s.getPoint(1.0);
	
	if (dot(s.v, a) < -EPS) return abs(a) < c.r - EPS;
	if (dot(s.v, a) > dot(s.v, s.v) + EPS) return abs(b) < c.r - EPS;
	else return abs(cross(s.v, a)) / abs(s.v) < c.r + EPS;
}

int main() {
	while (true) {
		int n;
		cin >> n;
		if (!n) break;
		
		Circle wall[100];
		REP(i, n) {
			double x, y, r;
			scanf("%lf %lf %lf", &x, &y, &r);
			wall[i] = Circle(P(x, y), r);
		}
		
		int m;
		cin >> m;
		REP(i, m) {
			double tx, ty, sx, sy;
			scanf("%lf %lf %lf %lf", &tx, &ty, &sx, &sy);
			
			Line s(P(sx, sy), P(tx, ty) - P(sx, sy));
			
			int flag = false;
			REP(j, n) if (isIntersectionSC(s, wall[j])) flag = true;
			
			if (flag) puts("Safe");
			else puts("Danger");
		}
	}
	return 0;
}