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<stdio.h>
#include<algorithm>
using namespace std;
int x[100];
int y[100];
int r[100];
bool CL_intersect(int ax,int ay,int bx,int by,int cx,int cy,int r){
	int acx=cx-ax;
	int acy=cy-ay;
	int bcx=cx-bx;
	int bcy=cy-by;
	int abx=bx-ax;
	int aby=by-ay;
	if(acx*acx+acy*acy>=r*r||bcx*bcx+bcy*bcy>=r*r){
		if(abx*acx+aby*acy<0&&acx*acx+acy*acy<=r*r)return true;
		else if(abx*acx+aby*acy>abx*abx+aby*aby&&bcx*bcx+bcy*bcy<=r*r)return true;
		else if(acx*acx+acy*acy-(int)((long long)(abx*acx+aby*acy)*(abx*acx+aby*acy)/(abx*abx+aby*aby))<=r*r)return true;
	}
	return false;
}
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<a;i++)scanf("%d%d%d",x+i,y+i,r+i);
		int b;
		scanf("%d",&b);
		for(int i=0;i<b;i++){
			int c,d,e,f;
			scanf("%d%d%d%d",&c,&d,&e,&f);
			bool ok=false;
			for(int j=0;j<a;j++){
				if(CL_intersect(c,d,e,f,x[j],y[j],r[j]))ok=true;
			}
			if(ok)printf("Safe\n");
			else printf("Danger\n");
		}
	}
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

double const EPS = 1.0e-10;

class Point
{
public:
    double y, x;
    Point(){
        y = x = 0.0;
    }
    Point(double y0, double x0){
        y = y0;
        x = x0;
    }
    double dot(const Point& p) const{
        return y * p.y + x * p.x; // |a|*|b|*cosÆ
    }
    double cross(const Point& p) const{
        return x * p.y - y * p.x; // |a|*|b|*sinÆ
    }
    Point operator+(const Point& p) const{
        return Point(y + p.y, x + p.x);
    }
    Point operator-(const Point& p) const{
        return Point(y - p.y, x - p.x);
    }
    Point operator*(double a) const{
        return Point(y * a, x * a);
    }
    Point operator/(double a) const{
        return Point(y / a, x / a);
    }
};

class Line
{
public:
    double a, b, c; // a*x + b*y + c = 0
    Line(double a0, double b0, double c0){
        a = a0;
        b = b0;
        c = c0;
    }
    Line(const Point& p1, const Point& p2){
        double dy = p2.y - p1.y;
        double dx = p2.x - p1.x;
        if(abs(dy / dx) < EPS){
            a = 0.0;
            b = 1.0;
            c = -p1.y;
        }else if(abs(dx / dy) < EPS){
            a = 1.0;
            b = 0.0;
            c = -p1.x;
        }else{
            a = - dy / dx;
            b = 1.0;
            c = dy / dx * p1.x - p1.y;
        }
    }
};

vector<Point> circleLineIntersection(const Point& p, double r, const Line& l)
{
    double s = l.a * p.x + l.b * p.y + l.c;
    double t = (l.a*l.a + l.b*l.b) * r*r - s*s;

    if(t < -EPS)
        return vector<Point>(0);
    if(t < EPS)
        return vector<Point>(1, Point(l.b, l.a) * (-s / (l.a*l.a + l.b*l.b)) + p);

    vector<Point> ret(2, Point(l.a, -l.b) * sqrt(t));
    ret[1] = ret[1] * -1;
    for(int i=0; i<2; ++i)
        ret[i] = (ret[i] - Point(l.b, l.a) * s) / (l.a*l.a + l.b*l.b) + p;
    return ret;
}

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        vector<Point> center(n);
        vector<double> r(n);
        for(int i=0; i<n; ++i)
            cin >> center[i].x >> center[i].y >> r[i];

        int m;
        cin >> m;

        while(--m >= 0){
            Point p1, p2;
            cin >> p1.x >> p1.y >> p2.x >> p2.y;
            Line l(p1, p2);

            bool safe = false;
            for(int i=0; i<n; ++i){
                vector<Point> p = circleLineIntersection(center[i], r[i], l);
                for(unsigned j=0; j<p.size(); ++j){
                    if(min(p1.y, p2.y) - EPS < p[j].y && p[j].y < max(p1.y, p2.y) + EPS
                           && min(p1.x, p2.x) - EPS < p[j].x && p[j].x < max(p1.x, p2.x) + EPS)
                        safe = true;
                }
            }

            if(safe)
                cout << "Safe" << endl;
            else
                cout << "Danger" << endl;
        }
    }
}

C++: #include <iostream>
#include <complex>
#include <vector>
#include <algorithm>

#define EPS 1e-10

using namespace std;

typedef complex<double> P;

struct L : public vector<P> {
    L(const P &a, const P &b){
        push_back(a);
        push_back(b);
    }
};

struct C{
    P p;
    double r;
    C(const P &p, double r) : p(p), r(r) { }
};

double dot(const P &a, const P &b){
    return real(conj(a) * b);
}

bool intersectSP(const L &s, const P &p) {
    return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS; // triangle inequality
}

P projection(const L &l, const P &p) {
  double t = dot(p-l[0], l[0]-l[1]) / norm(l[0]-l[1]);
  return l[0] + t*(l[0]-l[1]);
}

double distanceSP(const L &s, const P &p) {
  const P r = projection(s, p);
  if (intersectSP(s, r)) return abs(r - p);
  return min(abs(s[0] - p), abs(s[1] - p));
}

int main(){
    int n,m;                                     
    int x,y,r;
    int a,b;
    vector<C> circles;
    while(cin >> n && n){
        for(int i=0;i<n;i++){
            cin >> x >> y >> r;
            circles.push_back(C(P(x,y), r));
        }
        cin >> m;
        for(int i=0;i<m;i++){
            bool isok = false;
            cin >> x >> y;
            cin >> a >> b;
            L line = L(P(x,y), P(a,b));
            for(int j=0;j<n;j++){
                if((abs(line[0]-circles[j].p) - circles[j].r < -EPS &&
                    abs(line[1]-circles[j].p) - circles[j].r < -EPS) ||
                   distanceSP(line, circles[j].p) - circles[j].r > EPS){
                }else{
                    isok = true;
                }
            }
            if(isok){
                cout << "Safe" << endl;
            }else{
                cout << "Danger" << endl;
            }
        }
    }
}