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 <vector>
#include <cstdlib>
#include <complex>
#include <cstdio>

using namespace std;
typedef complex<double> P;

struct circle{
  P p;
  double r;
  circle(const P &p, double r) : p(p), r(r){ }
};

int main(void){
  int n,m;
  int tmp1, tmp2, r;
  int x1,y1,x2,y2;
  double a,c,d,dt,dj;
  vector<circle> cvec;
  
  while(cin >> n && n){
	for(int i=0;i<n;i++){
	  cin >> tmp1 >> tmp2 >> r;
	  P p(tmp1, tmp2);
	  circle c(p,r);
	  cvec.push_back(c);
	}
	cin >> m;
	for(int i=0;i<m;i++){
	  bool isok = false;
	  cin >> x1 >> y1 >> x2 >> y2;
	  //calculate y = ax + b;
	  if(x1 != x2){
		a = (double)(y2 - y1) / (x2 - x1);
		c = y1 - a * x1;
		//printf("y = %lfx%+lf\n",a,c);
		for(int j=0;j<n;j++){
		  d = fabs(a*cvec[j].p.real()-1*cvec[j].p.imag()+c) / sqrt(a*a+1);
		  dt = sqrt((cvec[j].p.real() - x1) * (cvec[j].p.real() - x1) + 
					(cvec[j].p.imag() - y1) * (cvec[j].p.imag() - y1));
		  dj = sqrt((cvec[j].p.real() - x2) * (cvec[j].p.real() - x2) + 
					(cvec[j].p.imag() - y2) * (cvec[j].p.imag() - y2));

		  if(!((dt <= r && dj <= r) || d >= r)){
			isok = true;
		  }
		}
	  }else{
		for(int j=0;j<n;j++){
		  d = fabs(x1 - cvec[j].p.real());
		  dt = sqrt((cvec[j].p.real() - x1) * (cvec[j].p.real() - x1) + 
					(cvec[j].p.imag() - y1) * (cvec[j].p.imag() - y1));
		  dj = sqrt((cvec[j].p.real() - x2) * (cvec[j].p.real() - x2) + 
					(cvec[j].p.imag() - y2) * (cvec[j].p.imag() - y2));
		  if(!((dt <= r && dj <= r) || d >= r)){
			isok = true;
		  }
		}
	  }
	  if(isok){
		cout << "Danger" << endl;
	  }else{
		cout << "Safe" << endl;
	  }
	}
	cvec.clear();
  }
}