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 <cmath>
#include <vector>

using namespace std;

typedef long double ld;

const ld eps = 1e-9;

struct Point { // ポイントクラス兼ベクトルクラス
    ld x, y;
    Point() {}
    Point(ld x, ld y) : x(x), y(y) {}
    Point operator+(const Point &o) const {
        return Point(x + o.x, y + o.y);
    }
    Point operator-(const Point &o) const {
        return Point(x - o.x, y - o.y);
    }
    Point operator*(ld a) const {
        return Point(a*x, a*y);
    }
    Point operator/(ld a) const {
        return Point(x/a, y/a);
    }
    ld norm() const {
        return sqrt(x*x + y*y);
    }
    Point unit() const { // 単位ベクトル
        return (*this)/norm();
    }
};
ld dot(const Point &a, const Point &b) {
    return a.x * b.x + a.y * b.y;
}
ld cross(const Point &a, const Point &b) {
   return a.x * b.y - a.y * b.x;
}
#define CCW 1
#define CW -1
#define CAB 2
#define ABC -2
#define ACB 0
int ccw(Point a, Point b, Point c) {
    // aを基準とする位置ベクトルを考える
	b = b - a; c = c - a;
	if (cross(b, c) > eps) return CCW; // 反時計周り
	if (cross(b, c) < -eps) return CW;// 時計周り
	if (dot(b, c) < 0) return CAB;     // c--a--bがこの順番に一直線上
	if (b.norm() < c.norm()) return ABC; // a--b--cがこの順番に一直線上
	return ACB;                         // a--c--bが一直線上
}
typedef Point Vector;

struct Segment {
    Point from, to;
    Segment() {}
    Segment(Point from, Point to) : from(from), to(to) {}
    ld length() const {
        return (to-from).norm();
    }
    bool contains(const Point &p) const {
        return ccw(from, to, p) == ACB;
    }
};

struct Line : public Segment {
    Line() {}
    Line(Segment s) : Segment(s) {}
    Line(Point from, Point to) : Segment(from, to) {}
    ld distance(const Point &p) const {
        Vector v1 = p - from;
        Vector v2 = to - from;
        ld S = abs(cross(v1, v2));   // v1,v2が張る平行四辺形の面積
        return S / length();         // それを線分の長さで割る
    }
    Point projection(const Point &p) const {
        Vector v1 = p - from, v2 = to - from;
        ld l = dot(v1, v2) / v2.norm();
        return from + (v2.unit()) * l;
    }
};

struct Circle {
    Point o;
    ld r;
    Circle() {}
    Circle(Point o, ld r) : o(o), r(r) {}
    Circle(ld x, ld y, ld r) : o(Point(x, y)), r(r) {}
    bool intersects(const Circle& c) const {
        ld dx = o.x - c.o.x;
        ld dy = o.y - c.o.y;
        return (r + c.r) * (r + c.r) + eps >= dx*dx + dy*dy;
    }
    bool intersects(const Segment& s) const {
        if (contains(s.from) && contains(s.to)) return false;
        Line l(s);
        if (l.distance(o) > r + eps) return false;
        if (contains(s.from) != contains(s.to)) return true;
        return s.contains(l.projection(o));
    }
    bool contains(const Point& p) const {
        ld dx = o.x - p.x;
        ld dy = o.y - p.y;
        return r*r + eps >= dx*dx + dy*dy;
    }
};

int main() {
    int n, m;
    while (cin >> n, n) {
        vector<Circle> walls(n);
        for (int i = 0; i < n; i++) {
            ld x, y, r; cin >> x >> y >> r;
            walls[i] = Circle(x, y, r);
        }
        cin >> m;
        for (int i = 0; i < m; i++) {
            Point t, s;
            cin >> t.x >> t.y >> s.x >> s.y;
            Segment eye(t, s);
            bool safe = false;
            for (int j = 0; j < n; j++) {
                if (walls[j].intersects(eye)) {
                    safe = true;
                    break;
                }
            }
            cout << (safe ? "Safe" : "Danger") << endl;
        }
    }
    return 0;
}

C++: #include <complex>
#include <cmath>
#include <cstdio>
using namespace std;

typedef complex<double> P;
typedef const P &rP;

#define EPS 1e-7

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

bool intersectlsc(rP p1, rP p2, rP pc, double r){
	P v12 = p2 - p1;
	P v1c = pc - p1;

	double a = norm(v12);
	double b = -dot(v12, v1c);
	double c = norm(v1c) - r * r;

	double D = b * b - a * c;

	if( D < -EPS ){
		return false;
	}
	else if( D < 0.0 ){
		D = 0.0;
	}

	double t1 = (-b + sqrt(D)) / a;
	double t2 = (-b - sqrt(D)) / a;

	if( (-EPS < t1 && t1 < 1.0 + EPS)
		|| (-EPS < t2 && t2 < 1.0 + EPS)
	){
		return true;
	}
	return false;
}

int n;
P w[100];
double r[100];

const char *solve(rP t, rP s){
	for(int i = 0; i < n; ++i){
		if( intersectlsc(t, s, w[i], r[i]) ){
			return "Safe";
		}
	}
	return "Danger";
}

int main(){
	int m;
	double x, y;
	double tx, ty, sx, sy;
	
	while( scanf("%d", &n), n != 0 ){
		for(int i = 0; i < n; ++i){
			scanf("%lf%lf%lf", &x, &y, &r[i]);
			w[i] = P(x, y);
		}
		
		scanf("%d", &m);
		for(int i = 0; i < m; ++i){
			scanf("%lf%lf%lf%lf", &tx, &ty, &sx, &sy);
			puts( solve( P(tx, ty), P(sx, sy) ) );
		}
	}
}