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 <cmath>
#include <vector>
using namespace std;

#define N_MAX 100

class Point {
public:
  double x, y;
  Point(double x=0, double y=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); }
};
typedef Point Vector;

class Segment {
public:
  Point p1, p2;
  Segment(Point p1, Point p2) {
    this->p1 = p1;
    this->p2 = p2;
  }
};

typedef Segment Line;

class Circle {
public:
  Point o;
  double r;
};


double norm(Vector a) { return a.x*a.x + a.y*a.y; }
double abs(Vector a) { return sqrt(norm(a)); }
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; }

double getDistanceSP(Segment s, Point p) {
  if(dot(s.p2 - s.p1, p - s.p1) < 0) return abs(p - s.p1);
  if(dot(s.p1 - s.p2, p - s.p2) < 0) return abs(p - s.p2);
  return fabs(cross(s.p2 - s.p1, p - s.p1)) / abs(s.p2 - s.p1);
}

bool isInCircle(Circle c, Point p) {
  return c.r - abs(c.o - p) > 0;
}

vector<Circle> w(N_MAX);
Point t, s;


void solve() {
  {
    vector<Circle>::iterator it = w.begin();
    while(it != w.end()) {
      bool tic, sic;
      tic = isInCircle(*it, t);
      sic = isInCircle(*it, s);
      if(tic && sic) {
	it = w.erase(it);
      } else if(tic || sic) {
	cout << "Safe" << endl;
	return;
      } else {
	it++;
      }
    }
  }

  Segment u(t, s);
  for(int i = 0; i < w.size(); ++i) {
    if(w[i].r - getDistanceSP(u, w[i].o) >= 0) {
      cout << "Safe" << endl;
      return;
    }
  }
  cout << "Danger" << endl;
}

main() {
  int n;
  while((cin >> n) && n) {
    w.clear();
    for(int i = 0; i < n; ++i) {
      Circle c;
      cin >> c.o.x >> c.o.y >> c.r;
      w.push_back(c);
    }
    int m;
    cin >> m;
    for(int i = 0; i < m; ++i) {
      cin >> t.x >> t.y >> s.x >> s.y;
      solve();
    }
  }
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int n,m;
vector<P> vec;
vector<int> rad;
P s,t;
bool in(int x,P a)
{
	return hypot(a.fi-vec[x].fi,a.sec-vec[x].sec)<(double)rad[x];
}
double sq(double x)
{
	return x*x;
}
bool check()
{
	for(int i=0;i<n;i++)
	{
		bool in1,in2;
		in1=in(i,s);in2=in(i,t);
		if(in1!=in2)return false;
		if(in1&&in2)continue;
		double a=s.fi,b=s.sec,c=t.fi,d=t.sec,p=vec[i].fi,q=vec[i].sec;
		if(a>c)
		{
			swap(a,c);swap(c,d);
		}
		double A=sq(d-b)+sq(c-a);
		double B=(b*c-a*d)*(d-b)-p*sq(c-a)-(c-a)*(d-b)*q;
		double C=p*p*sq(c-a)+sq(b*c-a*d-q*c+q*a)-sq(rad[i])*sq(c-a);
		//cout << A << ' ' << B << ' ' << C << endl;
		if(B*B-A*C<0)continue;
		else if(abs(B*B-A*C)<eps)
		{
			if(a<=(-B/A)&&(-B/A)<=c)return false;
			else continue;
		}
		else
		{
			double ans1=(-B+sqrt(B*B-A*C))/A;
			double ans2=(-B-sqrt(B*B-A*C))/A;
			if(ans1>=a&&ans2<=c)return false;
			else continue;
		}
	}
	return true;
}
int main()
{
	while(1)
	{
		vec.clear();
		rad.clear();
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++)
		{
			P k;
			int r;
			scanf("%d %d %d",&k.fi,&k.sec,&r);
			vec.pb(k);
			rad.pb(r);
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d %d %d %d",&t.fi,&t.sec,&s.fi,&s.sec);
			if(check())puts("Danger");
			else puts("Safe");
		}
	}
	return 0;
}

C++: //36
#include<iostream>
#include<cmath>
#include<complex>

using namespace std;

typedef complex<double> P;

double cr(P a,P b){
  return a.real()*b.imag()-a.imag()*b.real();
}

int main(){
  for(int n;cin>>n,n;){
    P w[100];
    double r[100];
    for(int i=0;i<n;i++){
      double x,y;
      cin>>x>>y>>r[i];
      w[i]=P(x,y);
    }
    int m;
    cin>>m;
    while(m--){
      P t,s,l;
      double tx,ty,sx,sy;
      cin>>tx>>ty>>sx>>sy;
      t=P(tx,ty);
      s=P(sx,sy);
      l=t-s;
      int i;
      for(i=0;i<n;i++){
	if((abs(w[i]-t)-r[i])*(abs(w[i]-s)-r[i])<0)break;
	if(abs(w[i]-t)>r[i]&&cr((t-s)*P(0,1),w[i]-s)*cr((s-t)*P(0,1),w[i]-t)>0&&abs(w[i]-s)*fabs(sin(arg(w[i]-s)-arg(l)))<=r[i]+1e-9)break;
      }
      cout<<((i<n)?"Safe":"Danger")<<endl;
    }
  }
  return 0;
}