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;

#define EPS 1e-8

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) ||
     (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;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 

 
const int INF = 1000000;
const double EPS = 1e-8;
const int mod = 1e9 + 7;

const double PI = acos(-1);

int dx[] ={-1,0,1,0};
int dy[] ={0,-1,0,1};

struct Point
{
	double x, y;
	Point(){};
	Point(double x, double y):x(x), y(y){};

	Point operator - (const Point& p) const{
		return Point(x-p.x, y-p.y);
	}

	double getAng(){
		return atan2(y, x);
	}
	double getCross(const Point& p) const{
		return x*p.y-y*p.x;
	}
	double getDot(const Point& p) const{
		return x*p.x+y*p.y;
	}
};

struct Circle
{
	Point p;
	double r;
	Circle(){};
	Circle(double x, double y, double r):p(x, y), r(r){};
	Circle(Point p, double r) :p(p),r(r){};

};

struct Line{
	Point p;
	double ang;
	Line(){};
	Line(Point p1, Point p2):p(p1), ang((p2-p1).getAng()){};
	Line(Point p, double ang):p(p), ang(ang){};
};
	
int n;
Circle c[1010];
Point taro, oni;
Line l;
Point lv;

bool checkSigneCross(Point p){
	return lv.getCross(p-l.p) > 0;
}

Point getCrossPoint(Line L){
	Point ret;
	ret.x = L.p.x + 1000000 * cos(L.ang);
	ret.y = L.p.y + 1000000 * sin(L.ang);
	bool sign = checkSigneCross(L.p), sign2 = checkSigneCross(ret);
	sign2 = sign^sign2;
	//cout<<sign2<<endl;
	double l = 0.0, r = 1000000.0, m;
	rep(i, 1000){
		m = (l + r) / 2.0;
		ret.x = L.p.x + m * cos(L.ang) * (sign2?1:-1);
		ret.y = L.p.y + m * sin(L.ang) * (sign2?1:-1);
		if(checkSigneCross(ret) == sign) l = m;
		else r = m;
	}
	return ret;
}

bool checkInCircle(const Circle& C, const Point& P){
	Point p = C.p-P;
	return p.x*p.x+p.y*p.y < C.r*C.r + EPS;
}

bool check(){
	for(int i = 0; i < n; i++){
		if(checkInCircle(c[i], taro)^checkInCircle(c[i], oni)) return true;
		else if(checkInCircle(c[i], taro)&&checkInCircle(c[i], oni)) continue;
		Line l2(c[i].p, l.ang+PI/2.0);

		Point p =  getCrossPoint(l2);
		if(checkInCircle(c[i], p) && (taro-p).getDot(oni-p) < 0) return true;
	}
	return false;
}

int main(){
	int m;
	while(cin>>n && n){
		rep(i, n) cin>>c[i].p.x>>c[i].p.y>>c[i].r;
		cin>>m;
		rep(i, m){
			cin>>taro.x>>taro.y>>oni.x>>oni.y;
			l = Line(taro, oni);
			lv = Point(cos(l.ang), sin(l.ang));
			cout<<(check()?"Safe":"Danger")<<endl;
		}
	}
}