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<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<complex>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef complex<double> P;
/* àÏ */
static inline double inp(const P &a, const P &b){
  return (conj(a)*b).real();
}
/* OÏ */
static inline double outp(const P &a, const P &b){
  return (conj(a)*b).imag();
}

class Line{
public:
  P p; /* position */
  P d; /* direction */
  Line(){}
  Line(P pos, P dir){p=pos; d=dir/abs(dir);}
};

/* _Æûü©ç */
Line LineDirect(P pos, P dir){
  return Line(pos, dir);
}
/* 2_©ç */
Line LinePos(P p1, P p2){
  return Line(p1, p2-p1);
}

/* _Æ¼üÌ£ */
double dist(const Line &l, const P &p){
  return std::abs(outp(l.d, p-l.p) / abs(l.d));
}

P crossPoint(const Line &l1, const Line &l2){
  double num = outp(l2.d, l2.p-l1.p);
  double denom = outp(l2.d, l1.d);
  return P(l1.p + l1.d * num/denom);
}
int ccw(P p0, P p1, P p2){
  P d1 = p1-p0;
  P d2 = p2-p0;
  double dx1 = d1.real(), dx2 = d2.real();
  double dy1 = d1.imag(), dy2 = d2.imag();

  if(dx1*dy2 > dy1*dx2) return  1;//½vñè
  if(dx1*dy2 < dy1*dx2) return -1;//vñè
  if((dx1*dx2 < 0) || (dy1*dy2 < 0)) return -1;
  if((dx1*dx1+dy1*dy1) < (dx2*dx2+dy2*dy2)) return 1;
  return 0;
}

int main(){
  int n;
  scanf("%d",&n);
  P o[n];
  double r[n];
  REP(i,n)
    scanf("%lf%lf%lf",&o[i].real(),&o[i].imag(),&r[i]);
  int m;
  scanf("%d",&m);
  while(m --> 0){
    P p[2];
    scanf("%lf%lf%lf%lf",&p[0].real(),&p[0].imag(),&p[1].real(),&p[1].imag());

    bool ans = false;
    if(p[0] != p[1])
      REP(i,n){
        double d1 = abs(p[0] - o[i]) - r[i];
        double d2 = abs(p[1] - o[i]) - r[i];
        if((d1 < 0 && d2 > 0) || (d1 > 0 && d2 < 0)){
          ans = true;
          break;
        }
        if(d1 < 0 && d2 < 0)
          continue;
        if(dist(LinePos(p[0],p[1]), o[i]) > r[i])
          continue;

        Line l = LineDirect(o[i], (p[0]-p[1])*P(0,1));
        P pp = crossPoint(l, LinePos(p[0],p[1]));
        P l1 = pp - p[0];
        P l2 = pp - p[1];
        if((l1 / l2).real() < 0){
          ans = true;
          break;
        }
      }
    puts(ans?"Safe":"Danger");
  }
  return 0;
}

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

#define EPS 1e-8
#define sqr(x) ((x)*(x))
#define fi first
#define se second

class circle {
	public:
		int x, y, r;
		circle(int x, int y, int r) {
			this->x = x;
			this->y = y;
			this->r = r;
		}

		bool is_cross(int x1, int y1, int x2, int y2) {
			pair<int,int> range;
			int x0 = x, y0 = y;
			if(x1 == x2 && y1 == y2) {
				return false;
			}
			if(x1 == x2) {
				swap(x1, y1);
				swap(x2, y2);
				swap(x0, y0);
			}

			range.fi = min(x1, x2);
			range.se = max(x1, x2);

			double a, b;
			a = (double)(y2 - y1) / (x2 - x1);
			b = y1 - a * x1;

			double d;
			d = sqr(x0 - a * (b - y0)) - (1 + sqr(a)) * (sqr(x0) + sqr(b - y0) - sqr(r));

			if(d < 0)
				return false;

			double z, zz;
			z  = (x0 - a * (b - y0) - sqrt(d)) / (1 + sqr(a));
			zz = (x0 - a * (b - y0) + sqrt(d)) / (1 + sqr(a));

			if((range.fi <= z && z <= range.se) || (range.fi <= zz && zz <= range.se))
				return true;
			return false;
		}
};

int main(void) {
	int n, m;

	while(cin >> n, n) {
		vector<circle> v;
		for(int i = 0; i < n; i++) {
			int x, y, r;
			cin >> x >> y >> r;
			v.push_back(circle(x,y,r));
		}
		cin >> m;
		for(int i = 0; i < m; i++) {
			int x1, y1, x2, y2;
			bool flag = false;
			cin >> x1 >> y1 >> x2 >> y2;
			for(int j = 0; j < v.size(); j++) {
				if(v[j].is_cross(x1, y1, x2, y2))
					flag = true;
			}
			if(flag)
				cout << "Safe" << endl;
			else
				cout << "Danger" << endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <cmath>

using namespace std;

#define EPS 1e-7

class Point{
public:
  double x,y;
  Point(double x = 0.0,double y = 0.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(x*a, y*a); }
  Point operator / (double a){ return Point(x/a, y/a); }
  double abs(){ return sqrt(norm()); }
  double norm(){ return x*x + y*y; }
};

typedef Point Vector;
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;
}

class Segment{
public:
  Vector t,s;
  Segment(Vector t,Vector s) : t(t),s(s) {}
  Point projection(Point &p){
    Vector b = s - t;
    double d = dot(p-t,b) / b.norm();
    return t + b*d;
  }
};

class Circle{
public:
  double x,y,r;
  Circle(double x = 0.0,double y = 0.0,double r = 0.0) : x(x),y(y),r(r) {}
};

int N;
Point t,s;
Circle ci[300];

bool check(Point p0,Point p1,Point p2){
  Vector a = p1 - p0;
  Vector b = p2 - p0;
  if(cross(a,b) > EPS) return false;
  if(cross(a,b) < -EPS) return false;
  if(dot(a,b) < -EPS) return false;
  if(a.norm() < b.norm()) return false;
  return true;
}

bool getStateCS(Circle c,Segment seg){
  Point cp = Point(c.x,c.y);
  Point p = seg.projection(cp);
  Point pp = p - cp;


  Vector a = seg.t - cp;
  Vector b = seg.s - cp;
    
  if((a.abs() > c.r && b.abs() < c.r) ||
     (a.abs() < c.r && b.abs() > c.r) ) return true;
  if(a.abs() > c.r && b.abs() > c.r){
    if(pp.abs()-EPS < c.r && check(seg.t,seg.s,p)){
      return true;
    }
  }
  return false;
}

bool judge(){
  Segment seg = Segment(t,s);

  for(int i = 0 ; i < N ; i++){
    if(getStateCS(ci[i],seg)) return true;
  }
  return false;
}

int main(){
  int M;
 
  while(cin >> N,N){
    for(int i = 0 ; i < N ; i++){
      cin >> ci[i].x >> ci[i].y >> ci[i].r;
    }
    cin >> M;
    for(int i = 0 ; i < M ; i++){
      cin >> t.x >> t.y >> s.x >> s.y; 
      if(judge()){
        cout << "Safe" << endl;
      }else{
        cout << "Danger" << endl;
      }
    }
  }

  return 0;
}