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>
using namespace std;

const double EPS = 1e-9;

struct Circle{
	int x;
	int y;
	int r;
};

struct Segment{
	int sx;
	int sy;
	int ex;
	int ey;
};

class Vector2{
public:
	Vector2( const int _x, const int _y ) : x( _x ), y( _y ){}
	int x;
	int y;

	int dotProduct( const Vector2& v )
	{
		return x * v.x + y * v.y;
	}
	int clossProduct( const Vector2& v )
	{
		return x * v.y - y * v.x;
	}
	double length()
	{
		return sqrt( static_cast<double>( x * x + y * y ) );
	}
};

int main()
{
	int n, m;
	while( cin >> n ){
		if( n == 0 ){
			break;
		}
		Circle* circles = new Circle[ n ];
		for( int i = 0; i < n; ++i ){
			cin >> circles[ i ].x >> circles[ i ].y >> circles[ i ].r;
		}

		cin >> m;
		if( m == 0 ){
			delete[] circles;
			continue;
		}
		Segment seg;
		for( int i = 0; i < m; ++i ){
			cin >> seg.sx >>seg.sy >> seg.ex >> seg.ey;
			bool safety = false;

			for( int j = 0 ; j < n; ++j ){
				Circle* cir = &circles[ j ];
				bool collision = false;

				Vector2 se( seg.ex - seg.sx, seg.ey - seg.sy );
				Vector2 sc( cir->x - seg.sx, cir->y - seg.sy );
				Vector2 ec( cir->x - seg.ex, cir->y - seg.ey );
				double d = se.clossProduct( sc ) / se.length();
				if( !( sc.length() + EPS < static_cast<double>( cir->r )
					&& ec.length() + EPS < static_cast<double>( cir->r ) ) ){
					if( d <= static_cast<double>( cir->r ) + EPS ){
						if( sc.dotProduct( se ) * ec.dotProduct( se ) <= 0 ){
							collision = true;
						}else if( sc.length() + EPS < static_cast<double>( cir->r )
							|| ec.length() + EPS < static_cast<double>( cir->r ) ){
							collision = true;
						}
					}
				}
				if( collision ){
					safety = true;
					break;
				}
			}
			cout << ( safety ? "Safe" : "Danger" ) << endl;
		}
		delete[] circles;
	}
	return 0;
}

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<numeric>
#include<cassert>

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

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); 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()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

#include<complex>

#define EPS 1e-10

using std::complex;
typedef complex<double> P; /* _ */

static inline bool eq(const P &a, const P &b){
  return abs(a-b) < EPS;
}
/* àÏ */
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);
}

/* 2ÂÌ¼üÌð_ */
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);
}

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

/* p0, p1, p2ª
 * ½vñè:  1
 * vñè  : -1
 *
 * R_ª¼üãÉ éê
 * p0ªp2Æp1ÌÔÉ é: -1
 * p2ªp0Æp1ÌÔÉ é:  0
 * p1ªp0Æp2ÌÔÉ é:  1
 */
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;
}

template<class T> 
inline T dbl(T x){ return x * x; }

int main(){
  int n;
  while(n = getInt()){
    vector<P> c(n);
    vector<int> r(n);

    REP(i,n){
      c[i].real() = getInt();
      c[i].imag() = getInt();
      r[i]        = getInt();
    }

    int m = getInt();

    REP(cc,m){
      P tarou;
      P oni;

      tarou.real() = getInt();
      tarou.imag() = getInt();
      oni.real() = getInt();
      oni.imag() = getInt();

      bool ans = true;

      REP(i,n){
	bool tarouIn = ((int)(abs(tarou - c[i])+EPS)) < r[i];
	bool oniIn = ((int)(abs(oni - c[i])+EPS)) < r[i];

	if(tarouIn != oniIn){
	  ans = false;
	  break;
	}

	if(!tarouIn){
	  Line to(LinePos(tarou, oni));
	  if(((int)(dist(to, c[i]) + EPS)) <= r[i]){
	    Line co(LineDirect(c[i], to.d * P(0, 1.0)));
	    P cross(crossPoint(to, co));
	    
	    double rate = ((oni - tarou) / (cross - tarou)).real();

	    if(rate >= 1.0){
	      ans = false;
	      break;
	    }
	  }
	}
      }

      puts(ans ? "Danger" : "Safe");
    }
 
  }
  return 0;
}