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

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
double dot(P a, P b) {
  return (a.real() * b.real() + a.imag() * b.imag());
}
double cross(P a, P b) {
  return (a.real() * b.imag() - a.imag() * b.real());
}
double distance_ls_p(P a, P b, P c) {
  if ( dot(b-a, c-a) < EPS ) return abs(c-a);
  if ( dot(a-b, c-b) < EPS ) return abs(c-b);
  return abs(cross(b-a, c-a)) / abs(b-a);
}

int main(void){
  int n,m;
  while(cin>>n){
    if(n==0) break;
    vector<P> wall;
    vector<double> wall2;
    REP(i,n){
      double x,y,r;
      cin>>x>>y>>r;
      wall.push_back(P(x,y));
      wall2.push_back(r);
    }
    cin>>m;
    while(m--){
      double x1,y1,x2,y2;
      P p1,p2;
      cin>>x1>>y1>>x2>>y2;
      p1 = P(x1,y1);
      p2 = P(x2,y2);
      bool unsame = false;
      REP(i,n){
        if(abs(p1-wall[i])<wall2[i]+EPS||abs(p2-wall[i])<wall2[i]+EPS){
          if(!(abs(p1-wall[i])<wall2[i]+EPS&&abs(p2-wall[i])<wall2[i]+EPS)){
            unsame = true;
            break;
          }
        }
      }
      if(unsame){
        cout<<"Safe"<<endl;
      }else{
        bool cross = false;
        REP(i,n){
          if(distance_ls_p(p1,p2,wall[i])<wall2[i]+EPS&&abs(p1-wall[i])>wall2[i]){
            cross = true;
            break;
          }
        }
        if(cross){
          cout<<"Safe"<<endl;
        }else{
          cout<<"Danger"<<endl;
        }
      }
    }
  }
  return 0;
}

C++: #include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<cstdlib>
#include<iomanip>
#include<queue>
#include<set>
#include <valarray>
#include<stack>
#include<sstream>

using namespace std;

double EPS = 1e-10;
double PI = 3.141592653589793238462643383279;

double add(double a, double b)
{
    if(abs(a + b) < EPS * (abs(a) + abs(b)))return 0;
    return a + b;
}

struct point
{
    double x, y;
    point(){}
    point(double x,double y) : x(x) , y(y){}
    
    point operator + (point p)
    {
        return point(add(x, p.x), add(y, p.y));
    }
    
    point operator - (point p)
    {
        return point(add(x, -p.x), add(y, -p.y));
    }
};

struct circle
{
    point p;
    double r;
};

double dot(point a, point b){return (a.x * b.x + a.y * b.y);}
double cross(point a, point b){return (a.x * b.y - a.y * b.x);}
double norm(point a){return sqrt(a.x*a.x+a.y*a.y);}

double dist(point a,point b)
{
    return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}

double distance_ls_p(point a, point b, point c)
{
    if ( dot(b-a, c-a) < EPS ) return norm(c-a);
    if ( dot(a-b, c-b) < EPS ) return norm(c-b);
    
    return abs(cross(b-a, c-a)) / norm(b-a);
}

int crossCS(point a, point b, circle c)
{
    double res=distance_ls_p(a,b,c.p);
    double ac=dist(a,c.p);
    double bc=dist(b,c.p);
    
    if(res>c.r || (ac<c.r && bc<c.r))return 0;
    else if(c.r-res<EPS ||(ac<c.r && bc>=c.r)||(ac>=c.r && bc<c.r))return 1;
    else return 2;
}

int main()
{
    int n,m;
    circle c[100];
    point t,s;
    
    while(cin >> n,n)
    {
        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++)
        {
            cin >> t.x >> t.y >> s.x >> s.y;
            
            int fg=0;
            for(int j=0;j<n;j++)fg+=crossCS(t,s,c[j]);
            
            if(fg==0)cout << "Danger" << endl;
            else cout << "Safe" << endl;
        }
    }
    
    return 0;
}

C++: #include <iostream>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
double dot(double ax, double ay, double bx, double by) {
	return ax*bx+ay*by;
}
int main() {
	int n,m;
	while(cin >> n, n) {
		double x[100],y[100],r[100];
		rep(i,n) {
			cin >> x[i] >> y[i] >> r[i];
		}
		cin >> m;
		double tx,ty,sx,sy;
		rep(i,m) {
			cin >> tx >> ty >> sx >> sy;
			bool flag = 0;
			rep(j,n) {
				double vx1 = tx-sx, vy1 = ty-sy;
				double vx2 = x[j]-sx, vy2 = y[j]-sy;
				double slen = sqrt(vx2*vx2+vy2*vy2);
				double tlen = sqrt(pow(x[j]-tx,2)+pow(y[j]-ty,2));				
				if(slen<r[j] && tlen>r[j] || slen>r[j] && tlen<r[j]) {
					flag = 1;
					break;
				} else {
					double n1 = dot(vx1,vy1,vx2,vy2);
					double n2 =	dot(vx1,vy1,vx1,vy1);
					double n3 = dot(vx2,vy2,vx2,vy2);
					if(n1>0 && n2>n1 && !(tlen<r[j]&&slen<r[j]) && n3-(n1/n2)*n1<=r[j]*r[j]) {
						flag = 1;
						break;
					}
				}
			}
			if(flag) cout << "Safe" << endl;
			else cout << "Danger" << endl;
		}
	}
	return 0;
}