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 <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
using namespace std;

#define REP(i,n) for(int i=0;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()

const double EPS = 1e-8;
const double INF = 1e12;

typedef complex<double> P;
double dot(const P& a, const P& b) {
  return real(conj(a)*b);
}
struct L : public vector<P> {
  L(const P &a, const P &b) {
    push_back(a); push_back(b);
  }
};
struct C {
  P p; double r;
  C(const P &p, double r) : p(p), r(r) { }
};
bool intersectSC2(const L &s, const C &c) { // î{IÈ­zÍãÆ¯¶¾ªA]vÈÖðìéKvªÈ­Èé
  if (abs(s[0]-c.p) < c.r && abs(s[1]-c.p) < c.r) return false; // ~ÌÉüéÌÍð·Æ·é©µÈ¢©B
  P vc = c.p-s[0], vs = s[1]-s[0];
  if (dot(vc,vs) < 0) // n_æèàO 
    return abs(vc) < c.r + EPS;
  if (dot(c.p-s[1],-vs) < 0) // I_æèàO
    return abs(c.p-s[1]) < c.r + EPS;
  return norm(vc)-dot(vc,vs)/norm(vs)*dot(vc,vs) < c.r*c.r + EPS;
}

int main() {
  int n;
  while(cin >> n, n) {
    vector<C> v;
    REP(i,n) {
      double x,y,r;
      cin >> x >> y >> r;
      v.push_back(C(P(x,y),r));
    }
    int m;
    cin >> m;
    while(m--) {
      int tx,ty,sx,sy;
      cin >> tx >>ty >> sx >> sy;
      L l(P(tx,ty), P(sx,sy));
      bool danger = true;
      FOR(it, v) {
        if (intersectSC2(l, *it)) {
          cout << "Safe" << endl;
          danger = false;
          break;
        }
      }
      if (danger) {
        cout << "Danger" << endl;
      }
    }
  }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define EPS 1e-8

using namespace std;

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

    Point operator+(const Point &o) const 
	{
        return Point(x+o.x, y+o.y);
    }

    Point operator-(const Point &o) const 
	{
        return Point(x-o.x, y-o.y);
    }
 
    Point operator*(const double m) const 
	{
        return Point(x*m, y*m);
    }
 
    Point operator/(const double d) const 
	{
        return Point(x/d, y/d);
    }

	bool operator < (const Point &a) const
	{
		return x == a.x ? y < a.y : x < a.x;
	}
 
    double cross(const Point &o) const 
	{
        return x * o.y - y * o.x;
    }
 
    double dot(const Point &o) const 
	{
        return x * o.x + y * o.y;
    }
 
    double atan() const 
	{
        return atan2(y, x);
    }

    double norm() const 
	{
        return dot(*this);
    }

	double distance (const Point &o) const 
	{
        return sqrt((x - o.x) * (x - o.x) + (y - o.y) * (y - o.y));
    }

	double distance_Line(const Point &a,const Point &b) const
	{
		Point p(x,y);

		Point d = b-a;

		if(d.x == 0 && d.y == 0)
		{
			return p.distance(a);
		}

		double r2 = d.x*d.x + d.y*d.y;
		double t = (d.x*(p.x - a.x) + d.y*(p.y - a.y)) / r2;

		if(t < 0)
		{
			return p.distance(a);
		}
		if(t > 1)
		{
			return p.distance(b);
		}

		Point c;
		c.x = (1-t) * a.x + t*b.x;
		c.y = (1-t) * a.y + t*b.y;
		return p.distance(c);
	}

	double area(const Point &a,const Point &b) const
	{
		double t = (a.x - x) * (b.y - y);
		double t2 = (a.y - y) * (b.x - x);
		double areas = abs(t-t2);
		return  areas/2;
	}
};


int main()
{
	int n;
	vector<Point> kp;
	vector<double> r;

	while(cin >> n && n)
	{
		double a,b,c,d;
		
		rep(i,n)
		{
			cin >> a>> b >> c;
			kp.push_back(Point(a,b));
			r.push_back(c);
		}

		int m;
		cin >> m;
		rep(i,m)
		{
			cin >> a >> b >> c >> d;
			Point p1(a,b);
			Point p2(c,d);

			bool flag = true;
			rep(j,kp.size())
			{
				double d1 = p1.distance(kp[j]);
				double d2 = p2.distance(kp[j]);

				if(d1 > r[j] + EPS && d2 < r[j] + EPS)
				{
					flag = false;
					cout << "Safe" << endl;
					break;
				}
				else if(d1 < r[j] + EPS && d2 > r[j] + EPS)
				{
					flag = false;
					cout << "Safe" << endl;
					break;
				}

				else if(d1 > r[j] && d2 > r[j])
				{
					double dd = kp[j].distance_Line(p1,p2);

					if(dd < r[j] + EPS)
					{
						flag = false;
						cout << "Safe" << endl;
						break;
					}
				}
			}
			if(flag)
			{
				std::cout << "Danger" << endl;
			}
		}


	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>

using namespace std;

class Point
{
public:
	double x, y;
};

class Circle
{
public:
	double x, y, r;
};

bool isInCircle(Point p, Circle c)
{
	double a = pow(p.x - c.x, 2.0) + pow(p.y - c.y, 2.0);
	if(a < c.r * c.r)
	{
		return(true);
	}
	else
	{
		return(false);
	}
}

bool isIntersectCircleAndLine(Point p1, Point p2, Circle c)
{
	if(isInCircle(p1, c) && isInCircle(p2, c)) 
	{
		return(false);
	}

	double vx1 = (c.x - p1.x);
	double vy1 = (c.y - p1.y);
	double vx2 = (p2.x - p1.x);
	double vy2 = (p2.y - p1.y);

	double n1 = vx1 * vx2 + vy1 * vy2;
	double n2 = vx2 * vx2 + vy2 * vy2;
	double n3 = vx1 * vx1 + vy1 * vy1;
	
	if(n1 < 0)
	{
		if(pow(p1.x - c.x, 2.0) + pow(p1.y - c.y, 2.0) <= c.r * c.r)
		{
			return(true);
		}
		else
		{
			return(false);
		}
	}
	else if(n1 > n2)
	{
		if(pow(p2.x - c.x, 2.0) + pow(p2.y - c.y, 2.0) <= c.r * c.r)
		{
			return(true);
		}
		else
		{
			return(false);
		}
	}
	else
	{
		if(n2 <= 0) 
		{
			if(n3 * n2 - n1 * n1 >= c.r * c.r * n2)
			{
				return(true);
			}
			else
			{
				return(false);
			}
		}
		else
		{
			if(n3 * n2 - n1 * n1 <= c.r * c.r * n2)
			{
				return(true);
			}
			else
			{
				return(false);
			}
		} 
	}
}

void solve()
{
	int n;
	while(cin >> n, n)
	{
		vector<Circle> circle(n);
		for(int i = 0; i < n; ++i)
		{
			cin >> circle[i].x >> circle[i].y >> circle[i].r;
		}
		int m;
		cin >> m;
		for(int i = 0; i < m; ++i)
		{
			Point taro, oni;
			cin >> taro.x >> taro.y >> oni.x >> oni.y;
			bool isSafe = false;
			for(int j = 0; j < circle.size(); ++j)
			{
				if(isIntersectCircleAndLine(taro, oni, circle[j]))
				{
					isSafe = true;
				}
			}
			if(isSafe)
			{
				cout << "Safe" << endl;
			}
			else
			{
				cout << "Danger" << endl;
			}
		}
	}
}

int main()
{
	solve();
	return(0);
}