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 <map>
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

class Point{
public:
  double x, y;
  Point(double x = 0, double y = 0): x(x), y(y){}
  Point operator + (Point p){ return Point(x + p.x, y + p.y); }
  Point operator - (Point p){ return Point(x - p.x, y - p.y); }
  Point operator * ( double a ){ return Point(a*x, a*y); }
  double abs(){ return sqrt(norm()); }
  double norm(){ return x*x + y*y; }
  bool operator < ( const Point &p) const {
    return x != p.x ? x < p.x : y < p.y;
  }
  bool operator == (const Point &p) const {
    return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS;
  }
};
typedef Point Vector;

struct Segment{
  Point p1, p2;
};
typedef Segment Line;

class Circle{
public:
  Point p;
  int r;
};

int n;
Circle c[100];

double dot(Vector a, Vector b){
  return a.x*b.x + a.y*b.y;
}

double cross(Vector a, Vector b){
  return a.x*b.y - a.y*b.x;
}

double getDistanceLP(Line s, Point p){
  return abs(cross(s.p2 - s.p1, p - s.p1) / (s.p2 - s.p1).abs());
}

bool is_cross(Circle c, Line l){
  double dist1 = (c.p - l.p1).abs();
  double dist2 = (c.p - l.p2).abs();
  bool f1 = false, f2 = false;
  if(dist1 < (double) c.r || abs(dist1 - c.r) < EPS) f1 = true;
  if(dist2 < (double) c.r || abs(dist2 - c.r) < EPS) f2 = true;
  if(f1 && f2) return false;
  else if(f1 || f2) return true;
  Point p1, p2, p3, p4;
  p1 = l.p1 - l.p2;
  p2 = l.p2 - l.p1;
  p3 = c.p - l.p2;
  p4 = c.p - l.p1;
  //cout << dot(p1, p3) << ' ' << dot(p2, p4);
  if(dot(p1, p3) >= 0 && dot(p2, p4) >= 0){
    //cout << '!';
    double d = getDistanceLP(l, c.p);
    //cout << "(" << d << ", " << c.r << ")";
    if(d < c.r || abs(d - c.r) < EPS) return true;
  }
  return false;
}

string solve(Line l){
  for(int i=0;i<n;i++){
    //cout << i <<  ' ';
    if(is_cross(c[i], l)){
      return "Safe";
    }
    //cout << endl;
  }
  return "Danger";
}

main(){
  int m;
  while(cin >> n){
    if(n == 0) break;
    for(int i=0;i<n;i++){
      cin >> c[i].p.x >> c[i].p.y >> c[i].r;
    }
    cin >> m;
    for(int i=0;i<m;i++){
      Line input;
      cin >> input.p1.x >> input.p1.y >> input.p2.x >> input.p2.y;
      cout << solve(input) << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>
#include <cassert>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)
#define mp std::make_pair

typedef long long ll;
typedef unsigned long long ull;

const int INF = 1001001001;

// S N E W(南北東西)
const int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

struct P{
    P(){}
    P(ll _r, ll _i):r(_r), i(_i){}
    void real(const ll& v){r = v;}
    void imag(const ll& v){i = v;}
    ll r, i;
};

ll real(const P& p){return p.r;}
ll imag(const P& p){return p.i;}
ll norm(const P& p){ll r = real(p), i = imag(p); return r * r + i * i;}

P operator+(const P& lhs, const P& rhs){
    return P(real(lhs)+real(rhs), imag(lhs)+imag(rhs));
}
P operator-(const P& lhs, const P& rhs){
    return P(real(lhs)-real(rhs), imag(lhs)-imag(rhs));
}
P operator-(const P& p){
    return P(-real(p), -imag(p));
}

ll cross(const P& lhs, const P& rhs){
    return real(lhs)*imag(rhs) - imag(lhs)*real(rhs);
}

ll dot(const P& lhs, const P& rhs){
    return real(lhs)*real(rhs) + imag(lhs)*imag(rhs);
}

ll distance(const P& a, const P& b, const P& p){
    ll c = cross(p-a, b-a);
    return c * c;
}

bool doesLineCrossCircle(const P& a, const P& b, const P& cc, const ll& radius){
    if(distance(a, b, cc) > radius * radius * norm(b-a)){return false;}
    if(norm(a-cc) > radius * radius && norm(b-cc) > radius * radius){
        return dot(cc-a, b-a) * dot(cc-b, b-a) <= 0;
    }
    return (norm(a-cc) < radius * radius) ^ (norm(b-cc) < radius * radius);
}

int N, M;
P wps[100100]; ll wrs[100100];

int main(){
    std::cin >> N;

    assert(N < 100100);
    REP(i, N){
        ll re, im;
        std::cin >> re >> im >> wrs[i];

        wps[i].real(re); wps[i].imag(im);
    }

    std::cin >> M;
    
    REP(i, M){
        P taro, oni;
        ll re, im;
        std::cin >> re >> im;
        taro.real(re); taro.imag(im);
        std::cin >> re >> im;
        oni.real(re); oni.imag(im);

        bool f = false;
        REP(j, N){
            assert(norm(taro-wps[j]) != wrs[j] * wrs[j] &&
                   norm(oni-wps[j]) != wrs[j] * wrs[j]);
            if(doesLineCrossCircle(taro, oni, wps[j], wrs[j])){
                f = true;
            }
        }

        if(f){
            puts("Safe");
        }else{
            puts("Danger");
        }
    }
}

C++: #include <iostream>
#include <cassert>
#include <cmath>
#include <vector>
#include <complex>
#include <algorithm>
using namespace std;


typedef complex<double> P;
typedef vector<P> G;

#define EPS			(1e-10)
#define PI			(3.14159265358979323846)


#define X(a)		( (a).real() )
#define Y(a)		( (a).imag() )
#define EQ(a,b)		( -EPS<(a)-(b) && (a)-(b)<EPS )
#define EQV(a,b)	( EQ(X(a),X(b)) && EQ(Y(a),Y(b)) )

#define unit(a)		( (a)/abs(a) )
#define normal(a)	( (a)*P(0,1) )
#define dot(a,b)	( X((a)*conj(b)) )
#define cross(a,b)	( Y(conj(a)*(b)) )

double distLP(P a, P b, P p)
{
	return abs(cross(b-a, p-a))/abs(b-a);
}

bool intersectSC(P a, P b, P c, double r)
{
	if( !(distLP(a,b,c) <= r + EPS) ) return false;

	double da=abs(c-a);
	double db=abs(c-b);

	if(da< r-EPS && db< r-EPS) return false;
	if(da<=r+EPS || db<=r+EPS) return true;

	return dot(c-a,b-a) > 0;
}


int main()
{
	int n,m,i;
	double t;
	P a,b,p;
	vector<P> c;
	vector<double> r;

	while(cin >> n, n)
	{
		for(i=0; i<n; i++)
		{
			cin >> X(p) >> Y(p) >> t;
			c.push_back(p);
			r.push_back(t);
		}

		cin >> m;
		while(m--)
		{
			cin >> X(a) >> Y(a) >> X(b) >> Y(b);
			for(i=0; i<n; i++)
			{
				if(intersectSC(a,b,c[i],r[i]))
				{
					cout << "Safe" << endl;
					break;
				}
			}
			if(i==n)
			{
				cout << "Danger" << endl;
			}
		}
	}
}