#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define max(a,b) ((a)>(b))?(a):(b)
#define min(a,b) ((a)<(b))?(a):(b)
#define si(n) scanf("%d",&n)
#define ss(s) scanf("%s",s)
#define sort(a,n) qsort(a,n,sizeof(int),compare)
#define pi(n) printf("%d ",n)
#define ps(s) printf("%s",s)
#define loop(i,n) for(i=0;i<n;i++)
#define Loop(i,n) for(i=1;i<=n;i++)
typedef long long int lld;
#define MAX 100005
int a[MAX];
       int compare(const void *a,const void *b)
       {
       return *(int *)a-*(int *)b;
       }
int main(void)
{
   int i,n,ans,k,l,m,t;
   si(t);
   if(t==1 || t==2)
   {
       puts("-1");
       return 0;
   }
   if(t==3)
   {
       puts("210");
       return 0;
   }

   a[4]=160;
   for(i=5;i<MAX;++i)
   {
       a[i]=(a[i-1]*10)%210;
   }
   ans=210-a[t];
   printf("1");
   for(i=0;i<t-4;++i)
       printf("0");
   printf("%03d",ans);
   return 0;
}

   //while(t--)
   // {

   // }


#include<stdio.h>
int modo(int num[],int no,int n)
{
	int ans=0,i;
	for(i=0;i<n;i++)
	ans =((ans*10)%no +num[i]%no)%no;
	return ans;
}
int main()
{
	int n,i;
	scanf("%d",&n);
	int num[n];
	num[0]=1;
	for(i=1;i<n;i++)
	{
		num[i]=0;
	}
	int flag=1;
	i=n-1;
	while(flag==1 &&  num[0]<10)
	{
		num[i]++;
		while(num[i]>9 && i!=0)
		{
			num[i]=0;
			i--;
			num[i]++;
		}
		i=n-1;
		if(modo(num,210,n)==0)
		{
			flag=0;
		}

	}

	if(flag)
		printf("-1\n");
	else
		for(i=0;i<n;i++)
		{
			printf("%d",num[i]);
		}

	return 0;
}


#include<stdio.h>
int main()
{
    int n,i;
    scanf("%d",&n);
    int a=-1;
    int b=210;
    if(n<3)
    {
           printf("%d",a);
           return 0;
    }
    if(n==3)
    {
            printf("%d",b);
            return 0;
            }
    int r=1;
    for(i=1;i<n;i++)
    r=r*10%210;
    r=210-r;
    printf("1");
    for(i=1;i<n-3;i++)
    printf("0");
    printf("%03d\n",r);
    return 0;
}


#include<stdio.h>
#include<conio.h>
int main()
{
    int n,i;
    scanf("%d",&n);
    int a=-1;
    int b=210;
    if(n<3)
    return a;
    if(n==3)
    return b;
    int r=1;
    for(i=1;i<n;i++)
    r=r*10%210;
    r=210-r;
    printf("1");
    for(i=1;i<n-3;i++)
    printf("0");
    printf("%03d",r);
    return 0;
}


#include<stdio.h>
#include<conio.h>
int main()
{
    int n,i;
    scanf("%d",&n);
    int a=-1;
    int b=210;
    if(n<3)
    {
           printf("%d",a);
           return 0;
    }
    if(n==3)
    {
            printf("%d",b);
            return 0;
            }
    int r=1;
    for(i=1;i<n;i++)
    r=r*10%210;
    r=210-r;
    printf("1");
    for ( int i=1; i<n-3; i++ )
    printf("0");
    printf("%03d\n",r);
    return 0;
}


#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n, i, j, a;

    scanf("%d", &n);
    if(n==1 || n==2){
        printf("-1");
        return 0;
    }
    else if(n==3){
        printf("210");
        return 0;
    }
    else{
        printf("1");
        for(i=0;i<n-4;i++){
            printf("0");
        }
        n=n-4;
        a = n%6;
        if(a==0){
            printf("050");
        }
        else if(a==1){
            printf("080");
        }
        else if(a==2){
            printf("170");
        }
        else if(a==3){
            printf("020");
        }
        else if(a==4){
            printf("200");
        }
        else{
            printf("110");
        }

    }

    return 0;
}


for q in range(input()):
	a,b,n,s = [int(r) for r in raw_input().split(" ")]
	if (s%n) <= b and ((a*n)+b) >= s:
		print "YES"
	else:
		print "NO"


q=int(raw_input().strip(' '))
while q:
    q-=1
    inp=list(map(int,raw_input().strip().split(' ')))
    a=inp[0]
    b=inp[1]
    n=inp[2]
    S=inp[3]
    if S/n==a and S%n<=b:
        print "YES"
    else:
        print "NO"


q=int(raw_input().strip(' '))
while q:
    q-=1
    inp=list(map(int,raw_input().strip().split(' ')))
    a=inp[0]
    b=inp[1]
    n=inp[2]
    S=inp[3]
    if S%(n*a)==b:
        print "Yes"
    else:
        print "No"


q=int(raw_input().strip(' '))
while q:
    q-=1
    inp=list(map(int,raw_input().strip().split(' ')))
    a=inp[0]
    b=inp[1]
    n=inp[2]
    S=inp[3]
    if S/n<=a and S%n<=b:
        print "YES"
    else:
        print "NO"


q=int(raw_input().strip(' '))
while q:
    q-=1
    inp=list(map(int,raw_input().strip().split(' ')))
    a=inp[0]
    b=inp[1]
    n=inp[2]
    S=inp[3]
    if S/a<=n and S%n<=b:
        print "YES"
    else:
        print "NO"


q=int(raw_input().strip(' '))
while q:
    q-=1
    inp=list(map(int,raw_input().strip().split(' ')))
    a=inp[0]
    b=inp[1]
    n=inp[2]
    S=inp[3]
    if S%(n*a)==b:
        print "YES"
    else:
        print "NO"


q=int(raw_input().strip(' '))
while q:
    q-=1
    inp=list(map(int,raw_input().strip().split(' ')))
    a=inp[0]
    b=inp[1]
    n=inp[2]
    S=inp[3]
    x=0
    y=0
    num_a=S/a
    if num_a<=n:
        x=num+a
    else:
        x=n
    num_b=S-x*a
    if num_b<=b:
        y=num_b
    else:
        y=b
    if (x*a+y)==S:
        print "YES"
    else:
        print "NO"


q=int(raw_input().strip(' '))
while q:
    q-=1
    inp=list(map(int,raw_input().strip().split(' ')))
    a=inp[0]
    b=inp[1]
    n=inp[2]
    S=inp[3]
    x=0
    y=0
    num_a=S/n
    if num_a<=a:
        x=num_a
    else:
        x=a
    num_b=S-x*n
    if num_b<=b and b:
        y=num_b
    else:
        y=b
    #print x,y
    #print x*n+y
    if (x*n+y)==S:
        print "YES"
    else:
        print "NO"


q = int(raw_input())

for i in range(q):
	a,b,n,s = map(int,raw_input().split())

	c = s%n

	if b < c:
		print "NO"
		continue

	if a*n + b < s:
		print "NO"
		continue
	print "YES"


from sys import stdin, stdout

for xzc in xrange(int(stdin.readline())):
    case = stdin.readline().split()
    aNeces = int(case[3]) // int(case[2])
    resto = int(case[3]) % int(case[2])
    if int(case[0]) < aNeces:
        suma = int(case[0]) * int(case[2])
        if int(case[1]) >= int(case[3]) - suma:
            stdout.write("YES\n")
        else:
            stdout.write("NO\n")
    else:
        if int(case[1]) >= resto:
            stdout.write("YES\n")
        else:
            stdout.write("NO\n")


import math
t = input()
for i in range(t):
    a, b, n, s = raw_input().split()
    banyak_n = int(s)//int(n)
    if (int(s)-banyak_n*int(n)-int(b))<=0 and banyak_n <= int(a):
        print "YES"
    else:
        print "NO"


import math
t = input()
for i in range(t):
    a, b, n, s = raw_input().split()
    banyak_n = int(s)//int(n)
    if banyak_n <= int(a):
        if (int(s)-banyak_n*int(n)-int(b))<=0:
            print "YES"
        else:
            print "NO"
    elif (int(s)-int(a)*int(n)-int(b))<=0:
        print "YES"
    else:
        print "NO"


q = int(raw_input())
for _ in xrange(q):
    a, b, n, s = map(int, raw_input().split())
    if s > a * n + b or s % n > b:
        print 'NO'
    else:
        print 'YES'


t=input()
for i in range(0,t):
    x=map(int,raw_input().rstrip().split())
    if x[3]>x[0]*x[2]+x[1]:
        print "no"
    else:
        if x[0]*x[2]>=x[3]:
            r=x[3]%x[2]
            if r<=x[1]:
                print "yes"
            else:
                print "no"
        else:
            r=x[3]-x[2]*x[0]
            if r<=x[1]:
                print "yes"
            else:
                print "no"


__author__ = 'Devesh Bajpai'

'''
https://codeforces.com/problemset/problem/1256/A

Solution: At the first look it does appear as a coin change DP problem but we don't need to use that since we have to
only check if the payment is possible and only two types of bills are available.
First, we observe that no matter how many $n bills we have, we cannot do the payment if we don't have the S%n number of
$1 bills. Therefore we check that first.
Second, we can now be assured we have no issues of divisibility and hence we just need money >= to the required $S. Hence
we compute that sum of a * $n and b * $1 is >= $S. Return the decision string accordingly.

Tidbit:
Ternary operator:
Python: [on_true] if [expression] else [on_false]
Java: [expression] ? [on_true] : [on_false]
'''

def solve(a, b, n, S):

    # check the presence of enough $1 bills
    if S % n > b:
        return "NO"

    # check the total money value is enough to build $S
    return "YES" if (a * n) + (b * 1) >= S else "NO"

if __name__ == "__main__":
    testcases = int(raw_input())
    results = list()
    for testcase in xrange(0, testcases):
        a, b, n, S = map(int, raw_input().split(" "))
        results.append(solve(a, b, n, S))
    for result in results:
        print result


def solve(a, b, n, S):

    # check the presence of enough $1 bills
    if S % n > b:
        return "NO"

    # check the total money value is enough to build $S
    return "YES" if (a * n) + (b * 1) >= S else "NO"

if __name__ == "__main__":
    testcases = int(raw_input())
    results = list()
    for testcase in xrange(0, testcases):
        a, b, n, S = map(int, raw_input().split(" "))
        results.append(solve(a, b, n, S))
    for result in results:
        print result


#!/usr/bin/env python

def matching(c):
    a = c[0]
    b = c[1]
    n = c[2]
    s = c[3]
    r = min(a, s/n)
    if (s - r) > b:
        return 'NO'
    return 'YES'

t = input()
for i in range(t):
    a = raw_input()
    c = map(int, a.split(' '))
    print(matching(c))


#!/usr/bin/env python

def matching(c):
    a = c[0]
    b = c[1]
    n = c[2]
    s = c[3]
    r = s/n
    if r > a and b != s:
        return 'NO'
    if (s-(r*n)) > b:
        return 'NO'
    return 'YES'

t = input()
for i in range(t):
    a = raw_input()
    c = map(int, a.split(' '))
    print(matching(c))


#!/usr/bin/env python

def matching(c):
    a = c[0]
    b = c[1]
    n = c[2]
    s = c[3]
    r = n * min(a, s/n)
    if (s - r) > b:
        return 'NO'
    return 'YES'

t = input()
for i in range(t):
    a = raw_input()
    c = map(int, a.split(' '))
    print(matching(c))


#!/usr/bin/env python

def matching(c):
    a = c[0]
    b = c[1]
    n = c[2]
    s = c[3]
    r = s/n
    if r > a:
        return 'NO'
    if (s-(r*n)) > b:
        return 'NO'
    return 'YES'

t = input()
for i in range(t):
    a = raw_input()
    c = map(int, a.split(' '))
    print(matching(c))


# codeforce can not import modules
#import numpy as np
def test_line(num, in_list):
    #in_list = in_list
    #out_list = []

    flag ='NO'
    a = in_list[0]
    b = in_list[1]
    n = in_list[2]
    s = in_list[3]

    if n==0:
        out_run = a
    elif s/n <= a:
        out_run = s/n
    else:
        out_run=a

    for j in range(out_run+1):
        y = s-j*n
        if y>=0 and y <= b:
            flag='YES'
    return flag

if __name__ == '__main__':
    num = int(input())
    for i in range(num):
        in_list = []
        out = raw_input()
        in_list = [int(n) for n in out.split()]
        #print(in_list)
        out = test_line(num, in_list)
        print(out)


# codeforce can not import modules
#import numpy as np
def test_line(num, in_list):
    #in_list = in_list
    #out_list = []

    flag ='NO'
    a = in_list[0]
    b = in_list[1]
    n = in_list[2]
    s = in_list[3]

    if n==0:
        out_run = a
    elif s/n <= a:
        out_run = s/n
    else:
        out_run=a

    #for j in range(out_run+1):
    y = s-out_run*n
    if y <= b:
        flag='YES'
    return flag

if __name__ == '__main__':
    num = int(input())
    for i in range(num):
        in_list = []
        out = raw_input()
        in_list = [int(n) for n in out.split()]
        #print(in_list)
        out = test_line(num, in_list)
        print(out)


# codeforce can not import modules
#import numpy as np
def test_line(num, in_list):
    #in_list = in_list
    #out_list = []

    flag ='NO'
    a = in_list[0]
    b = in_list[1]
    n = in_list[2]
    s = in_list[3]

    # if n==0:
    #     out_run = a
    # elif s/n <= a:
    #     out_run = s/n
    # else:
    out_run=a

    for j in range(out_run+1):
        y = s-j*n
        if y>=0 and y <= b:
            flag='YES'
    return flag

if __name__ == '__main__':
    num = int(input())
    for i in range(num):
        in_list = []
        out = raw_input()
        in_list = [int(n) for n in out.split()]
        #print(in_list)
        out = test_line(num, in_list)
        print(out)


# codeforce can not import modules
#import numpy as np
def test_line(num, in_list):
    in_list = in_list
    out_list = []

    flag ='NO'
    a = in_list[0]
    b = in_list[1]
    n = in_list[2]
    s = in_list[3]
    for j in range(int(s/n)+1):
        y = s-j*n
        if y>=0 and y <= b and j<a:
            flag='YES'

    return flag

if __name__ == '__main__':
    num = int(input())
    for i in range(num):
        in_list = []
        out = raw_input()
        in_list = [int(n) for n in out.split()]
        #print(in_list)
        out = test_line(num, in_list)
        print(out)


# codeforce can not import modules
#import numpy as np
def test_line(num, in_list):
    #in_list = in_list
    #out_list = []

    flag ='NO'
    a = in_list[0]
    b = in_list[1]
    n = in_list[2]
    s = in_list[3]

    if s/n <= a:
        out_run = s/n
    else:
        out_run=a

    for j in range(out_run+1):
        y = s-j*n
        if y>=0 and y <= b:
            flag='YES'
    return flag

if __name__ == '__main__':
    num = int(input())
    for i in range(num):
        in_list = []
        out = raw_input()
        in_list = [int(n) for n in out.split()]
        #print(in_list)
        out = test_line(num, in_list)
        print(out)


# codeforce can not import modules
#import numpy as np
def test_line(num, in_list):
    in_list = in_list
    out_list = []

    flag ='NO'
    a = in_list[0]
    b = in_list[1]
    n = in_list[2]
    s = in_list[3]

    if s/n <= a:
        out_run = s/n
    else:
        out_run=a

    for j in range(out_run+1):
        y = s-j*n
        if y>=0 and y <= b and j*n<=s:
            flag='YES'

    return flag

if __name__ == '__main__':
    num = int(input())
    for i in range(num):
        in_list = []
        out = raw_input()
        in_list = [int(n) for n in out.split()]
        #print(in_list)
        out = test_line(num, in_list)
        print(out)


# codeforce can not import modules
#import numpy as np
def test_line(num, in_list):
    in_list = in_list
    out_list = []

    flag ='NO'
    a = in_list[0]
    b = in_list[1]
    n = in_list[2]
    s = in_list[3]
    for j in range(int(s/n)+1):
        y = s-j*n
        if y>=0 and y <= b:
            flag='YES'

    return flag

if __name__ == '__main__':
    num = int(input())
    for i in range(num):
        in_list = []
        out = raw_input()
        in_list = [int(n) for n in out.split()]
        #print(in_list)
        out = test_line(num, in_list)
        print(out)


# codeforce can not import modules
#import numpy as np
def test_line(num, in_list):
    in_list = in_list
    out_list = []

    flag ='NO'
    a = in_list[0]
    b = in_list[1]
    n = in_list[2]
    s = in_list[3]
    for j in range(a+1):
        y = s-j*n
        if y>=0 and y <= b:
            flag='YES'
            #print(flag, j, y, j*n+y, a,b,n,s)
    return flag

if __name__ == '__main__':
    num = int(input())
    for i in range(num):
        in_list = []
        out = raw_input()
        in_list = [int(n) for n in out.split()]
        #print(in_list)
        out = test_line(num, in_list)
        print(out)


# codeforce can not import modules
#import numpy as np
def test_line(num, in_list):
    in_list = in_list
    out_list = []

    flag ='NO'
    a = in_list[0]
    b = in_list[1]
    n = in_list[2]
    s = in_list[3]

    if s/n <= a:
        out_run = s/n
    else:
        out_run=a

    for j in range(out_run+1):
        y = s-j*n
        if y>=0 and y <= b and j*n<=s:
            flag='YES'
            print(j,y,flag, s-j*n)

    return flag

if __name__ == '__main__':
    num = int(input())
    for i in range(num):
        in_list = []
        out = raw_input()
        in_list = [int(n) for n in out.split()]
        #print(in_list)
        out = test_line(num, in_list)
        print(out)


for _ in range(input()):
    a,b,n,S = map(int,raw_input().split())
    max_n = min(S/n,a)*n
    left = S - max_n
    if left <= b:
        print 'YES'
    else:
        print 'NO'


from sys import stdout,stdin
x=int(stdin.readline())
for i in xrange(x):
    a,b,c,d=stdin.readline().split()
    a,b,c,d=int(a),int(b),int(c),int(d)
    if b>=d:
        stdout.write("YES\n")
    else:
        acum=d//c
        if acum<=a:
            if acum*c + b >= d:
                stdout.write("YES\n")

            else:
                stdout.write("NO\n")
        else:
            if a* c + b >= d:
                stdout.write("YES\n")

            else:
                stdout.write("NO\n")


for _ in xrange(int(raw_input())):
    a, b, n, s = map(int, raw_input().split())
    x = min(s / n, a)
    print "YES" if x * n + b >= s and b >= s % n else "NO"


for _ in xrange(int(raw_input())):
    a, b, n, s = map(int, raw_input().split())
    print "YES" if a >= s / n and b >= s % n else "NO"


for _ in xrange(int(raw_input())):
    a, b, n, s = map(int, raw_input().split())
    print "YES" if b >= s or (a >= s / n and b >= s % n) else "NO"


#include<stdio.h>
int main()
{
   long long int i,j,a,b,t,n,s,flag;
    scanf("%d",&t);
    while(t--)
    {
        flag=0;
        scanf("%d %d %d %d",&a,&b,&n,&s);
        j=s%n;
        if(((a*n)+b)<s)
        printf("NO\n");
        else if(j<=b)
        printf("YES\n");
        else
        printf("NO\n");

    }
    return 0;
}


#include<stdio.h>
int main()
{
    int i,j,a,b,t,n,s,flag;
    scanf("%d",&t);
    while(t--)
    {
        flag=0;
        scanf("%d %d %d %d",&a,&b,&n,&s);
        j=s%n;
        if(((a*n)+b)<s)
        printf("NO\n");
        else if(j<=b)
        printf("YES\n");
        else
        printf("NO\n");

    }
    return 0;
}


#include<stdio.h>
int main()
{
    int i,j,a,b,t,n,s,flag;
    scanf("%d",&t);
    while(t--)
    {
        flag=0;
        scanf("%d %d %d %d",&a,&b,&n,&s);
        for(i=0;i<=b;i++)
        {
           if(((a*n)+i)%s==0)
           {
               flag=1;
               break;
           }
        }
        if(flag==1)
        printf("YES\n");
        else
        printf("NO\n");
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int i,j,a,b,t,n,s,flag;
    scanf("%d",&t);
    while(t--)
    {
        flag=0;
        scanf("%d %d %d %d",&a,&b,&n,&s);
        j=s%n;
        if(((a*n)+b)<s)
        printf("NO\n");
        else
        {if(j<=b)
        printf("YES\n");
        else
        printf("NO\n");
        }
    }
    return 0;
}


#include<stdio.h>
int main()
{
   long long int i,j,a,b,t,n,s,flag;
    scanf("%d",&t);
    while(t--)
    {
        flag=0;
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        j=s%n;
        if(((a*n)+b)<s)
        printf("NO\n");
        else if(j<=b)
        printf("YES\n");
        else
        printf("NO\n");

    }
    return 0;
}


#include<stdio.h>
int main()
{
   long long int i,j,a,b,t,n,s,flag;
    scanf("%lld",&t);
    while(t--)
    {
        flag=0;
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        j=s%n;
        if(((a*n)+b)<s)
        printf("NO\n");
        else if(j<=b)
        printf("YES\n");
        else
        printf("NO\n");

    }
    return 0;
}


#include<stdio.h>
int main()
{
    int i, t, a, b, n, s;
    scanf("%d", &t);
    for(i = 0; i < t; i++)
    {
        scanf("%d %d %d %d", &a, &b, &n, &s);
        if(s > a * n + b)
            printf("NO\n");
        else
        {
            if(s <= b)
                printf("YES\n");
            else if(b >= n)
                printf("YES\n");
            else if(s % n <= b)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int i, t, a, b, n, s;
    scanf("%d", &t);
    for(i = 0; i < t; i++)
    {
        scanf("%d %d %d %d", &a, &b, &n, &s);
        if(s > a * n + b)
            printf("NO\n");
        else
        {
            if(s <= b)
                printf("YES\n");
            else if(b >= n - 1)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    long long i, t, a, b, n, s;
    scanf("%lld", &t);
    for(i = 0; i < t; i++)
    {
        scanf("%lld %lld %lld %lld", &a, &b, &n, &s);
        if(s > a * n + b)
            printf("NO\n");
        else
        {
            if(s <= b)
                printf("YES\n");
            else if(b >= n - 1)
                printf("YES\n");
            else if(s % n <= b)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    long long i, t, a, b, n, s;
    scanf("%lld", &t);
    for(i = 0; i < t; i++)
    {
        scanf("%lld %lld %lld %lld", &a, &b, &n, &s);
        if(s > a * n + b || b < s % n)
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int i, t, a, b, n, s;
    scanf("%d", &t);
    for(i = 0; i < t; i++)
    {
        scanf("%d %d %d %d", &a, &b, &n, &s);
        if(s > a * n + b)
            printf("NO\n");
        else
        {
            if(s <= b)
                printf("YES\n");
            else if(s % n == 0)
                printf("YES\n");
            else if(b >= n - 1)
                printf("YES\n");
            else if(s % n <= b)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int i, t, a, b, n, s;
    scanf("%d", &t);
    for(i = 0; i < t; i++)
    {
        scanf("%d %d %d %d", &a, &b, &n, &s);
        if(s > a * n + b)
            printf("NO\n");
        else
        {
            if(s <= b)
                printf("YES\n");
            else if(b >= n - 1)
                printf("YES\n");
            else if(s % n <= b)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}


#include <stdio.h>
int main() {
	int q;
	scanf("%i",q);

	for (int qr = 0; qr < q; ++qr) {
		int a, b, n, s;
		scanf("%i","%i","%i","%i",a,b,n,s);

		if (s%n<= b&& 1ll*a*n+b>= s) {
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}
	}
	return 0;
}


#include <stdio.h>
int main() {
	int q;
	scanf("%i",&q);

	for (int qr = 0; qr < q; ++qr) {
		int a, b, n, s;
		scanf("%i",&a);
		scanf("%i",&b);
		scanf("%i",&n);
		scanf("%i",&s);

		if (s%n<= b&&a*n+b>= s) {
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}
	}
	return 0;
}


#include <stdio.h>
int main(void)
{
	int q;
	scanf("%d",&q);
for (int qr = 0; qr < q; ++qr)
	{
		int a,b,n,s;
		scanf("%d %d %d %d",&a,&b,&n,&s);
		if (s%n<=b&&(long)a*n+b>=s)
		printf("YES\n");
		else
		printf("NO\n");
	}
}


#include <stdio.h>
int main() {
	int q;
	scanf("%i",&q);

	for (int qr = 0; qr < q; ++qr) {
		int a, b, n, s;
		scanf("%i","%i","%i","%i",&a,&b,&n,&s);

		if (s%n<= b&& 111*a*n+b>= s) {
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}
	}
}


#include <stdio.h>
int main(void)
{
	int q;
	scanf("%d",&q);
	for (int qr = 0; qr < q; ++qr) {

	{
		int a,b,n,s;
		scanf("%d %d %d %d",&a,&b,&n,&s);
		if (s%n<=b&&(long long)a*n+b>=s) printf("YES\n");
		else printf("NO\n");
	}
}


#include <stdio.h>
int main() {
	int q;
	scanf("%i",&q);

	for (int qr = 0; qr < q; ++qr) {
		int a, b, n, s;
		scanf("%i",&a);
		scanf("%i",&b);
		scanf("%i",&n);
		scanf("%i",&s);

		if (s%n<= b&& 111*a*n+b>= s) {
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}
	}
	return 0;
}


#include <stdio.h>
int main(void)
{
	int q;
	scanf("%d",&q);
		for (int qr = 0; qr < q; ++qr)

	{
		int a,b,n,s;
		scanf("%d %d %d %d",&a,&b,&n,&s);
		if (s%n<=b&&(long long)a*n+b>=s) printf("YES\n");
		else printf("NO\n");
	}
}


#include<stdio.h>

int main()
{
    int i,q,a,b,n,s,t;

    scanf("%d",&q);

    for(i=0; i<q; i++)
    {
        scanf("%d%d%d%d",&a,&b,&n,&s);

        while(a)
        {
            if(a*n > s)
            {
                a--;
            }
            if(a*n <= s)
            {
                t = a*n;
                break;
            }
        }

        if(t+b >= s)
            printf("YES\n");
        else
            printf("NO\n");
    }

    return 0;
}


#include <stdio.h>

int main()
{
    int Q;
    long long a,b,n,S,i;

    scanf("%d",&Q);

    for(i = 0; i<Q; i++)
    {
        scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&S);

        if(S%n <= b && a*n + b >= S)
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }

    return 0;
}


#include<stdio.h>

int main()
{
    long long int i,q,a,b,n,s,t;

    scanf("%lld",&q);

    for(i=0; i<q; i++)
    {
        scanf("%lld%lld%lld%lld",&a,&b,&n,&s);

        while(a)
        {
            if(a*n > s)
            {
                a--;
            }
            if(a*n <= s)
            {
                t = a*n;
                break;
            }
        }

        if(t+b >= s)
            printf("YES\n");
        else
            printf("NO\n");
    }

    return 0;
}


#include<stdio.h>

int main()
{
    long long i,q,a,b,n,s,t;

    scanf("%lld",&q);

    for(i=0; i<q; i++)
    {
        scanf("%%I64d%I64d%I64d%I64d",&a,&b,&n,&s);

        while(a)
        {
            if(a*n > s)
            {
                a--;
            }
            if(a*n <= s)
            {
                t = a*n;
                break;
            }
        }

        if(t+b >= s)
            printf("YES\n");
        else
            printf("NO\n");
    }

    return 0;
}


#include <stdio.h>

int main(){
    long long i,j,a,b,n,s,nb;
    scanf("%lld",&nb);
    i = 0;
    j = 0;
    while (i < nb)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        int r = s - a * n;
        r = r < 0 ? n + r : r;
       printf(b >= r ? "YES\n" : "NO\n");
        i++;
    }
}


#include <stdio.h>

int main(){
    long long i,j,a,b,n,s,nb;
    scanf("%lld",&nb);
    i = 0;
    j = 0;
    while (i < nb)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        int r = s - a * n;
        r = r < 0 ? ((-1 * r) % n) : r;
       printf(b >= r ? "YES\n" : "NO\n");
        i++;
    }
}


#include <stdio.h>

int main(){
    long long i,j,a,b,n,s,nb;
    scanf("%lld",&nb);
    i = 0;
    j = 0;
    while (i < nb)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        /*int r = s - a * n;
        r = r < 0 ? ((-1 * r) % n) : r;
        printf(b >= r ? "YES\n" : "NO\n");*/
        int r = s /n;
        if (r == a)
        {
            printf("yes\n");
        }
        else if (r > a)
        {
            if (b >= s)
                printf("yes\n");
            else if (s -(a * n) < b)
                printf("yes\n");
            else
                printf("no\n");
        }
        else
        {
            if (s % n <= b)
                printf("yes\n");
            else
                printf("no\n");
        }

        i++;
    }
    return (0);
}


#include <stdio.h>

int main(){
    long long i,j,a,b,n,s,nb;
    scanf("%lld",&nb);
    i = 0;
    j = 0;
    while (i < nb)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        /*int r = s - a * n;
        r = r < 0 ? ((-1 * r) % n) : r;
        printf(b >= r ? "YES\n" : "NO\n");*/
        int r = s /n;
        if (r > a)
        {
            if (b >= s)
                printf("yes\n");
            else
                printf("no\n");
        }
        else
        {
            if (s % n <= b)
                printf("yes\n");
            else
                printf("no\n");
        }

        i++;
    }
    return (0);
}


#include <stdio.h>

int main(){
    long long int i,j,a,b,n,s,nb;
    scanf("%lld",&nb);
    i = 0;
    j = 0;
    while (i < nb)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        /*int r = s - a * n;
        r = r < 0 ? ((-1 * r) % n) : r;
        printf(b >= r ? "YES\n" : "NO\n");*/
        long long int r = s /n;

           if (r > a)
        {
            if (b >= s)
                printf("yes\n");
            else if (s -(a * n) <= b)
                printf("yes\n");
            else
                printf("no\n");
        }
        else
        {
            if (s % n <= b)
                printf("yes\n");
            else
                printf("no\n");
        }

        i++;
    }
    return (0);
}


#include <stdio.h>

int main(){
    long long int i,j,a,b,n,s,nb;
    scanf("%lld",&nb);
    i = 0;
    j = 0;
    while (i < nb)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        /*int r = s - a * n;
        r = r < 0 ? ((-1 * r) % n) : r;
        printf(b >= r ? "YES\n" : "NO\n");*/
        long long int r = s /n;
        if (r == a)
        {
            printf("yes\n");
        }
        else if (b == s)
        {
             printf("yes\n");
        }
        else if (r > a)
        {
            if (b >= s)
                printf("yes\n");
            else if (s -(a * n) <= b)
                printf("yes\n");
            else
                printf("no\n");
        }
        else
        {
            if (s % n <= b)
                printf("yes\n");
            else
                printf("no\n");
        }

        i++;
    }
    return (0);
}


#include <stdio.h>

int main(){
    long long i,j,a,b,n,s,nb;
    scanf("%lld",&nb);
    i = 0;
    j = 0;
    while (i < nb)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        /*int r = s - a * n;
        r = r < 0 ? ((-1 * r) % n) : r;
        printf(b >= r ? "YES\n" : "NO\n");*/
        int r = s /n;
        if (r == a)
        {
            printf("yes\n");
        }
        else if (r > a)
        {
            if (b >= s)
                printf("yes\n");
            else if (s -(a * n) <= b)
                printf("yes\n");
            else
                printf("no\n");
        }
        else
        {
            if (s % n <= b)
                printf("yes\n");
            else
                printf("no\n");
        }

        i++;
    }
    return (0);
}


#include <stdio.h>

int main(){
    long long int i,j,a,b,n,s,nb;
    scanf("%lld",&nb);
    i = 0;
    j = 0;
    while (i < nb)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        /*int r = s - a * n;
        r = r < 0 ? ((-1 * r) % n) : r;
        printf(b >= r ? "YES\n" : "NO\n");*/
        long long int r = s /n;

           if (r > a)
        {
            if (b >= s)
                printf("yes\n");
            else
                printf("no\n");
        }
        else
        {
            if (s % n <= b)
                printf("yes\n");
            else
                printf("no\n");
        }

        i++;
    }
    return (0);
}


#include <stdio.h>

int main(){
    long long i,j,a,b,n,s,nb;
    scanf("%lld",&nb);
    i = 0;
    j = 0;
    while (i < nb)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        /*int r = s - a * n;
        r = r < 0 ? ((-1 * r) % n) : r;
        printf(b >= r ? "YES\n" : "NO\n");*/
        long long r = s /n;
        if (r == a)
        {
            printf("yes\n");
        }
        else if (r > a)
        {
            if (b >= s)
                printf("yes\n");
            else if (s -(a * n) <= b)
                printf("yes\n");
            else
                printf("no\n");
        }
        else
        {
            if (s % n <= b)
                printf("yes\n");
            else
                printf("no\n");
        }

        i++;
    }
    return (0);
}


#include<stdio.h>
#include<math.h>
#include<string.h>
typedef long long int ll;
int main()
{
   ll t,i,j;
   scanf("%lld",&t);
   while(t--)
   {
       ll a,b,n,s,f=0;
       scanf("%lld%lld%lld%lld",&a,&b,&n,&s);
     if(b==s)
     {
         printf("YES\n");
         continue;
     }
  int r=s%n;
  if(r<=b)
  {
  if(a*n+b>=s)
  {
      printf("YES\n");
      continue;
  }
  else
  {
      printf("NO\n");
      continue;
  }

  }

  else
  {
      printf("NO\n");
      continue;

  }

   }
   return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
typedef long long int ll;
int main()
{
   int t,i,j;
   scanf("%d",&t);
   while(t--)
   {
       int a,b,n,s,f=0;
       scanf("%d%d%d%d",&a,&b,&n,&s);
       if(a*n+b<s)
       {
           printf("NO\n");
           continue;
       }
  else  if(b>=s)
    {
        printf("YES\n");
        continue;
    }
   else if(a*n==s)
   {
       printf("YES\n");
       continue;
   }
   int k=s/n;
   int r=s%n;
   if(k<=a&&r<=b)
   {
       printf("YES\n");
       continue;
   }
   else
   {
       printf("NO\n");
       continue;

   }

   }
   return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
typedef long long int ll;
int main()
{
   int t;
   scanf("%d",&t);
   while(t--)
   {
       int a,b,n,s;
       scanf("%d%d%d%d",&a,&b,&n,&s);
       if(a*n+b<s)
       {
           printf("NO\n");
           continue;
       }
       else if(a*n+b==s)
       {
           printf("YES\n");
           continue;
       }
       else if(b==s)
       {
           printf("YES\n");
               continue;

       }
       else if(a*n==s)
       {
           printf("YES\n");
           continue;
       }
       else if(s-a*n<=b&s-a*n>=0)
       {
           printf("YES\n");
       }
       else if(s-b<=a*n&&((s-b)%n==0&&(s-b)/n<=a))
       {
           printf("YES\n");
           continue;
       }
       else
       {
           printf("NO\n");
       }

   }
   return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
typedef long long int ll;
int main()
{
   int t,i,j;
   scanf("%d",&t);
   while(t--)
   {
       int a,b,n,s,f=0;
       scanf("%d%d%d%d",&a,&b,&n,&s);
       if(a*n+b<s)
       {
           printf("NO\n");
           continue;
       }
  int r=s%n;
  if(r<=b&&a*n+b>=s)
  {
      printf("YES\n");
  }
  else
  {
      printf("NO\n");
  }

   }
   return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
typedef long long int ll;
int main()
{
   int t,i,j;
   scanf("%d",&t);
   while(t--)
   {
       int a,b,n,s,f=0;
       scanf("%d%d%d%d",&a,&b,&n,&s);

  int r=s%n;
  if(r<=b&&a*n+b>=s)
  {
      printf("YES\n");
  }
  else
  {
      printf("NO\n");
  }

   }
   return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
typedef long long int ll;
int main()
{
   int t,i,j;
   scanf("%d",&t);
   while(t--)
   {
       int a,b,n,s,f=0;
       scanf("%d%d%d%d",&a,&b,&n,&s);
       if(a*n+b<s)
       {
           printf("NO\n");
           continue;
       }
      for(i=0;i<=a;i++)
      {
          for(j=0;j<=b;j++)
          {
              if(i*n+j==s)
              {
                  printf("YES\n");
                f=1;
                break;
              }

          }
           if(f==1)
              break;
      }
      if(f!=1)
      printf("NO\n");

   }
   return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
typedef long long int ll;
int main()
{
   int t,i,j;
   scanf("%d",&t);
   while(t--)
   {
       int a,b,n,s,f=0;
       scanf("%d%d%d%d",&a,&b,&n,&s);
     if(b==s)
     {
         printf("YES\n");
         continue;
     }
  int r=s%n;
  if(r<=b)
  {
      if(a*n+b>=s)
      printf("YES\n");
      else
          printf("NO\n");

  }
  else
  {
      printf("NO\n");
  }

   }
   return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
typedef long long int ll;
int main()
{
   int t,i,j;
   scanf("%d",&t);
   while(t--)
   {
       int a,b,n,s,f=0;
       scanf("%d%d%d%d",&a,&b,&n,&s);
       if(a*n+b<s)
       {
           printf("NO\n");
           continue;
       }
  else  if(b>=s)
    {
        printf("YES\n");
        continue;
    }
   else if(a*n==s)
   {
       printf("YES\n");
       continue;
   }

 for(i=0;i<=b;i++)
 {
     if((s-i)%n==0&&(s-i)/n<=a)
     {
         printf("YES\n");
         f=1;
         break;
     }
 }
 if(f!=1)
 {
     printf("NO\n");

 }

   }
   return 0;
}


#include<stdio.h>
#include<math.h>
#include<string.h>
typedef long long int ll;
int main()
{
   int t,i,j;
   scanf("%d",&t);
   while(t--)
   {
       int a,b,n,s,f=0;
       scanf("%d%d%d%d",&a,&b,&n,&s);
     if(b==s)
     {
         printf("YES\n");
         continue;
     }
  int r=s%n;
  if(r<=b)
  {
  if(a*n+b>=s)
  {
      printf("YES\n");
      continue;
  }
  else
  {
      printf("NO\n");
      continue;
  }

  }

  else
  {
      printf("NO\n");
      continue;

  }

   }
   return 0;
}


#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        int a,b,c,s,p,t;
        scanf("%d%d%d%d",&a,&b,&c,&s);
        if(s/c<=a)
        {
            if(s-c*(s/c)<=b)
                printf("YES\n");
            else
                printf("NO\n");
        }
        else
            printf("NO\n");
    }
}


#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        int a,b,c,s,p,t;
        scanf("%d%d%d%d",&a,&b,&c,&s);
        if(a*c+b<s)
            printf("NO\n");
        else if(s%c>b)
            printf("NO\n");
        else
            printf("YES\n");
    }
}


#include<stdio.h>
#define I64 long long int
int main()
{
    I64 n;
    scanf("%I64d",&n);
    while(n--)
    {
        I64 a,b,c,s;
        scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&s);
        if(a*c+b<s)
            printf("NO\n");
        else if(s%c>b)
            printf("NO\n");
        else
            printf("YES\n");
    }
}


#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        int a,b,c,s,p,t;
        scanf("%d%d%d%d",&a,&b,&c,&s);
        p=abs(a*c-s);
        if(p>b)
        {
            printf("NO\n");
        }
        else
        {
            printf("YES\n");
        }
    }
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int q,a,b,n,S,sa,sb,i;
    scanf("%d",&q);
    for(i=1;i<=q;i++) {
        sa = 0;
        sb = 0;
        scanf("%d %d %d %d",&a,&b,&n,&S);
        while(S > n) {
            sa = sa + (S/n);
            S = S % n;
        }
        while(S > 0) {
            sb = sb + (S/1);
            S = S % sb;
        }
        if(a >= sa && b >= sb)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;


#include <stdio.h>
#include <stdlib.h>

int main () {
    int q,a,b,n,S;
    scanf("%d",&q);
    for(int i=1;i<=q;i++) {
        int sa = 0;
        int sb = 0;
        scanf("%d %d %d %d",&a,&b,&n,&S);
        while(S > n) {
            sa = sa + (S/n);
            S = S % n;
        }
        while(S > 0) {
            sb = sb + (S/1);
            S = S % sb;
        }
        if(b >= sb) {
            if(a >= sa)
                printf("YES\n");
            else
                printf("NO\n");
        }
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int step;
    int a,b,n,s;
    scanf("%d",&step);
    for(int i=0;i<step;i++) {
        int x=0;
        scanf("%d %d %d %d",&a,&b,&n,&s);
        x = s/n; // 需要的a
        if (a >= x) {
            if ((x*n)+b >= s) {
                printf("YES\n");
            }
            else
                printf("NO\n");
        }
        else {
            if ((a*n)+b >= s) {
                printf("YES\n");
            }
            else
                printf("NO\n");
        }
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int q,a,b,n,ans,S;
    scanf("%d",&q);
    for(int i=0;i<q;i++) {
        ans = 0;
        scanf("%d %d %d %d",&a,&b,&n,&S);
        ans = (a*n)+(b*1);
        if (ans >= S)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int q,a,b,n,S,sa,sb;
    while(scanf("%d",&q)!=EOF) {
        for(int i=1;i<=q;i++) {
            sa = 0;
            sb = 0;
            scanf("%d %d %d %d",&a,&b,&n,&S);
            while(S > n) {
                sa = sa + (S/n);
                S = S % n;
            }
            while(S > 0) {
                sb = sb + (S/1);
                S = S % sb;
            }
            if((n*a) >= sa) {
                if (b >= sb)
                    printf("YES\n");
                else
                    printf("NO\n");
            }
            else
                printf("NO\n");
        }
        return 0;
    }
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int q,a,b,n,S,sa,sb;
    while(scanf("%d",&q)!=EOF) {
        for(int i=1;i<=q;i++) {
            sa = 0;
            sb = 0;
            scanf("%d %d %d %d",&a,&b,&n,&S);
            while(S > n) {
                sa = sa + (S/n);
                S = S % n;
            }
            while(S > 0) {
                sb = sb + (S/1);
                S = S % sb;
            }
            if((n*a) >= sa) {
                if (b >= sb)
                    printf("YES");
                else
                    printf("NO");
            }
            else
                printf("NO");
        }
        return 0;
    }
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int step;
    int a,b,n,s;
    scanf("%d",&step);
    for(int i=0;i<step;i++) {
        int x=0;
        int y=0;
        scanf("%d %d %d %d",&a,&b,&n,&s);
        x = s / n;
        y = s % n;
        if(x <= a && y <= b)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int q,a,b,n,S,sa,sb,i;
    scanf("%d",&q);
    for(i=1;i<=q;i++) {
        sa = 0;
        sb = 0;
        scanf("%d %d %d %d",&a,&b,&n,&S);
        while(S > n) {
            sa = sa + (S/n);
            S = S % n;
        }
        while(S > 0) {
            sb = sb + (S/1);
            S = S % sb;
        }
        if((a*n) >= sa) {
            if (b >= sb)
                printf("YES\n");
            else
                printf("NO\n");
        }
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int q,a,b,n,S,sa,sb,i;
    scanf("%d",&q);
    for(i=1;i<=q;i++) {
        sa = 0;
        sb = 0;
        scanf("%d %d %d %d",&a,&b,&n,&S);
        while(S > n) {
            sa = sa + (S/n);
            S = S % n;
        }
        while(S > 0) {
            sb = sb + (S/1);
            S = S % sb;
        }
        if((a >= sa) && (b >= sb)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int step;
    int a,b,n,s;
    scanf("%d",&step);
    for(int i=0;i<step;i++) {
        int x=0;
        scanf("%d %d %d %d",&a,&b,&n,&s);
        x = s/n; // 需要的a
        if (a >= x) {
            if ((a*n)+b >= s) {
                printf("YES\n");
            }
            else
                printf("NO\n");
        }
        else {
            if ((a*n)+b >= s) {
                printf("YES\n");
            }
            else
                printf("NO\n");
        }
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int q,a,b,n,S,sa,sb;
    scanf("%d",&q);
    for(int i=1;i<=q;i++) {
        sa = 0;
        sb = 0;
        scanf("%d %d %d %d",&a,&b,&n,&S);
        while(S > n) {
            sa = sa + (S/n);
            S = S % n;
        }
        while(S > 0) {
            sb = sb + (S/1);
            S = S % sb;
        }
        if((n*a) >= sa) {
            if (b >= sb)
                printf("YES\n");
            else
                printf("NO\n");
        }
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int q,a,b,n,S,sa,sb,i;
    scanf("%d",&q);
    for(i=1;i<=q;i++) {
        sa = 0;
        sb = 0;
        scanf("%d %d %d %d",&a,&b,&n,&S);
        while(S > n) {
            sa = sa + (S/n);
            S = S % n;
        }
        while(S > 0) {
            sb = sb + (S/1);
            S = S % sb;
        }
        if(a >= sa) {
            if (b >= sb)
                printf("YES\n");
            else
                printf("NO\n");
        }
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

int main () {
    int q,a,b,n,S;
    scanf("%d",&q);
    for(int i=1;i<=q;i++) {
        int sa = 0;
        int sb = 0;
        scanf("%d %d %d %d",&a,&b,&n,&S);
        while(S > n) {
            sa = sa + (S/n);
            S = S % n;
        }
        while(S > 0) {
            sb = sb + (S/1);
            S = S % sb;
        }
        if((a >= sa) && (b >= sb))
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>

struct node
{
    int a,b,n,s;
}lala[10001];
int check[10001];
int main()
{
    int q,i,j;
    scanf("%d",&q);
    for(i=1;i<=q;i++)
    {
        scanf("%d %d %d %d",&lala[i].a,&lala[i].b,&lala[i].n,&lala[i].s);
    }
    for(i=1;i<=q;i++)
    {
        if(lala[i].a*lala[i].n<lala[i].s)
        {
            if(lala[i].b>=lala[i].s-lala[i].a*lala[i].n)  check[i]=1;
        }
        else if(lala[i].a*lala[i].n>lala[i].s)
        {
            for(j=lala[i].a-1;j>=0;j--)
            {
                if(j*lala[i].n<=lala[i].s)
                {
                    if(lala[i].b>=lala[i].s-j*lala[i].n) check[i]=1;
                }
            }

        }
        else
            check[i]=1;
    }
    for(i=1;i<=q;i++)
    {
        if(check[i]==1) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;

struct node
{
    long long a,b,n,s;
}lala[10001];
int check[10001];
int main()
{
    int q,i,j;
    cin >> q;
    for(i=1;i<=q;i++)
    {
        cin >> lala[i].a >> lala[i].b >> lala[i].n >>lala[i].s;
    }
    for(i=1;i<=q;i++)
    {
        if(lala[i].a*lala[i].n<lala[i].s)
        {
            if(lala[i].b>=lala[i].s-lala[i].a*lala[i].n)  check[i]=1;
        }
        else if(lala[i].a*lala[i].n>lala[i].s)
        {
            for(j=lala[i].a-1;j>=0;j--)
            {
                if(j*lala[i].n<=lala[i].s)
                {
                    if(lala[i].b>=lala[i].s-j*lala[i].n) check[i]=1;
                }
            }

        }
        else
            check[i]=1;
    }
    for(i=1;i<=q;i++)
    {
        if(check[i]==1) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


#include <stdio.h>

int ans[10001];
int main()
{
    int q;
    scanf("%d",&q);
    long long a,b,n,s;
    for(int i=1;i<=q;i++)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        if(s/n>a)
        {
            if(a*n+b==s) ans[i]=1;
        }
        else if(s/n==a) ans[i]=1;
        else
        {
            if(s%n<=b)   ans[i]=1;
        }
    }
    for(int i=1;i<=q;i++)
    {
        if(ans[i]==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>

int ans[10001];
int main()
{
    int q;
    scanf("%d",&q);
    long long a,b,n,s;
    for(int i=1;i<=q;i++)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        if(s/n>a)
        {
            if((s/n-a)*n+s%n<=b) ans[i]=1;
        }
        else if(s/n==a) ans[i]=1;
        else
        {
            if(s%n<=b)   ans[i]=1;
        }
    }
    for(int i=1;i<=q;i++)
    {
        if(ans[i]==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


#include <stdio.h>

int ans[10001];
int main()
{
    int q;
    scanf("%d",&q);
    long long a,b,n,s;
    for(int i=1;i<=q;i++)
    {
        scanf("%lld %lld %lld %lld",&a,&b,&n,&s);
        if(s/n>a)
        {
            if((s/n-a)*n<=b) ans[i]=1;
        }
        else if(s/n==a) ans[i]=1;
        else
        {
            if(s%n<=b)   ans[i]=1;
        }
    }
    for(int i=1;i<=q;i++)
    {
        if(ans[i]==1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


input()
a = list(map(int, input().split()))
print(0 if 0 in a else -1)


input()
a = list(map(int, input().split()))
print((5555555550 if  a.count(5) >= 9 else 0) if 0 in a else -1)


import math

def ost(a,b):
    if a%b!=0:
        return 1
    return 0

n=int(input())
data=list(map(int,input().split()))

a=data.count(5)
b=data.count(0)

if a//9==0:
    print(0)
else:
   ans=[]
   k=min(a//9,b)
   ans=[5]*k*9+[0]*k
   print(''.join(map(str,ans)))


import math

def ost(a,b):
    if a%b!=0:
        return 1
    return 0

n=int(input())
data=list(map(int,input().split()))

a=data.count(5)
b=data.count(0)

if a//9==0:
    print(0)
else:
    ans=[]
    if a//18>=1:
        ans=[5]*(a//18*18)
        a%=18
    if a//9==1 and b>=1:
        ans=ans+[5]*9+[0]
    print(''.join(map(str,ans)))


import math

def ost(a,b):
    if a%b!=0:
        return 1
    return 0

n=int(input())
data=list(map(int,input().split()))

a=data.count(5)
b=data.count(0)
print(a,b)

if a//9==0:
    if b>0:
        print(0)
    else:
        print(-1)
else:
   ans=[]
   k=min(a//9,b)
   ans=[5]*k*9+[0]*b
   if k>=1:
        print(''.join(map(str,ans)))
   else:
       print(-1)


import math

def ost(a,b):
    if a%b!=0:
        return 1
    return 0

n=int(input())
data=list(map(int,input().split()))

a=data.count(5)
b=data.count(0)

if a//9==0:
    print(0)
else:
    ans=[5]*(a//9)*9+[0]*b
    print(''.join(map(str,ans)))


import math

def ost(a,b):
    if a%b!=0:
        return 1
    return 0

n=int(input())
data=list(map(int,input().split()))

a=data.count(5)
b=data.count(0)
#print(a,b)

if a//9==0:
    if b>0:
        print(0)
    else:
        print(-1)
else:
        if b>=1:
            ans=[5]*((a//9)*9)+[0]*b
            print(''.join(map(str,ans)))
        else:
            print(-1)


import math

def ost(a,b):
    if a%b!=0:
        return 1
    return 0

n=int(input())
data=list(map(int,input().split()))

a=data.count(5)
b=data.count(0)

if a//9==0 and b==0:
    print(0)
else:
    if a//18>=1:
        ans=[5]*(a//18*18)
        a%=18
    if a//9==1 and b>=1:
        ans=ans+[5]*9+[0]
    print(ans)


import math

def ost(a,b):
    if a%b!=0:
        return 1
    return 0

n=int(input())
data=list(map(int,input().split()))

a=data.count(5)
b=data.count(0)
#print(a,b)

if a//9==0:
    if b>0:
        print(0)
    else:
        print(-1)
else:
   ans=[]
   k=min(a//9,b)
   ans=[5]*k*9+[0]*b
   if k>=1:
        print(''.join(map(str,ans)))
   else:
       print(-1)


import math

def ost(a,b):
    if a%b!=0:
        return 1
    return 0

n=int(input())
data=list(map(int,input().split()))

a=data.count(5)
b=data.count(0)

if a//9==0:
    if b!=0:
        print(0)
    else:
        print(-1)
else:
   ans=[]
   k=min(a//9,b)
   ans=[5]*k*9+[0]*k
   print(''.join(map(str,ans)))


import math

def ost(a,b):
    if a%b!=0:
        return 1
    return 0

n=int(input())
data=list(map(int,input().split()))

a=data.count(5)
b=data.count(0)

if a//9==0:
    if b!=0:
        print(0)
    else:
        print(-1)
else:
   ans=[]
   k=min(a//9,b)
   ans=[5]*k*9+[0]*b
   print(''.join(map(str,ans)))


import math

def ost(a,b):
    if a%b!=0:
        return 1
    return 0

n=int(input())
data=list(map(int,input().split()))

a=data.count(5)
b=data.count(0)

if a//9==0:
    if b!=0:
        print(0)
    else:
        print(-1)
else:
    ans=[]
    if a//18>=1:
        ans=[5]*(a//18*18)
        a%=18
    if a//9==1 and b>=1:
        ans=ans+[5]*9+[0]
    print(''.join(map(str,ans)))


import math

def ost(a,b):
    if a%b!=0:
        return 1
    return 0

n=int(input())
data=list(map(int,input().split()))

a=data.count(5)
b=data.count(0)
#print(a,b)

if a//9==0:
    if b>0:
        print(0)
    else:
        print(-1)
else:
        if b>1:
            ans=[5]*((a//9)*9)+[0]*b
            print(''.join(map(str,ans)))
        else:
            print(-1)


n=int(input())
arr=list(map(int,input().split()))
if(arr.count(0)>0):
    res='5'*(arr.count(5)-arr.count(5)%9)+'0'*arr.count(0)
    print(int(res))
else:
    print("0")


n=int(input())
arr=list(map(int,input().split()))
if(arr.count(0)>0):
    res='5'*(arr.count(5)-arr.count(5)%9)+'0'*arr.count(0)
    print(int(res))
else:
    print("-1")


n=int(input())
arr=list(map(int,input().split()))
if(5*arr.count(5)%9==0 and arr.count(5)>0 and arr.count(0)>0):
    print('5'*arr.count(5)+'0'*arr.count(0))
else:
    print("0")


n=int(input())
arr=list(map(int,input().split()))
res='5'*(arr.count(5)-arr.count(5)%9)+'0'*arr.count(0)
print(int(res))


n = int(input() )
a = list(map(int, input().split() ) )
x, y = a.count(5), a.count(0)
x = x // 9 * 9
print('5'*x + '0'*y if x and y else 0 if y else -1)


n = int(input() )
a = list(map(int, input().split() ) )
x, y = a.count(5), a.count(0)
x = x // 9 * 9
print(-1 if not(x & y) else 0 if not x else '5' * x + '0' * y)


n = int(input() )
a = list(map(int, input().split() ) )
x, y = a.count(5), a.count(0)
x = x // 9 * 9
print('5'*x + '0'*y if x else 0 if y else -1)


n = int(input() )
a = list(map(int, input().split() ) )
x, y = a.count(5), a.count(0)
x = x // 9 * 9
print(-1 if not(x | y) else 0 if not x else '5' * x + '0' * y)


n = int(input())
cards = list(map(int, input().split()))

total = sum(cards)

cards.sort(reverse=True)

if cards[-1] == 0:
    if total % 9 != 0:
        while total != 0 and total % 9 != 0:
            total -= 5
        if total == 0 :
            print(0)
        else:
            result = "5"*(total // 5)
            result = result + "0"
            print(result)
    elif total % 9 == 0:
        result = "5"*(total // 5)
        result = result + "0"
        print(result)
else:
    print(-1)


n = int(input())
cards = list(map(int, input().split()))

total = sum(cards)

cards.sort(reverse=True)

if cards[-1] == 0:
    if total % 9 != 0:
        while total != 0 and total % 9 != 0:
            total -= 5
        if total == 0 :
            print(0)
        else:
            result = "5"*(total // 5)
            result = result + "0"*(cards.count(0))
            print(result)
    elif total % 9 == 0:
        if total == 0:
            print(0)
        else:
            result = "5"*(total // 5)
            result = result + "0"*(cards.count(0))
            print(result)
else:
    print(-1)


n = int(input())
cards = list(map(int, input().split()))

total = sum(cards)

cards.sort(reverse=True)

if cards[-1] == 0:
    if total % 9 != 0:
        while total != 0 and total % 9 != 0:
            total -= 5
        if total == 0 :
            print(0)
        else:
            result = "5"*(total // 5)
            result = result + "0"
            print(result)
    elif total % 9 == 0:
        result = "5"*(total // 5)
        result = result + "0"*(cards.count(0))
        print(result)
else:
    print(-1)


n = int(input())
cards = list(map(int, input().split()))

total = sum(cards)

cards.sort(reverse=True)

if cards[-1] == 0:
    if total % 9 != 0:
        while total != 0 and total % 9 != 0:
            total -= 5
        if total == 0 :
            print(0)
        else:
            result = "5"*(total // 5)
            result = result + "0"*(cards.count(0))
            print(result)
    elif total % 9 == 0:
        result = "5"*(total // 5)
        result = result + "0"*(cards.count(0))
        print(result)
else:
    print(-1)


n = int(input())
fives = 0
zeros = 0

l = list(map(int,input().split()))

for i in l:
    if i == 0:
        zeros += 1
    else:
        fives += 1

prefix = fives//9
if prefix == 0:
    print(0)
else:
    print('5'*fives+'0'*zeros)


n = int(input())
fives = 0
zeros = 0

l = list(map(int,input().split()))

for i in l:
    if i == 0:
        zeros += 1
    else:
        fives += 1

prefix = fives//9
if prefix == 0:
    print(0)
else:
    print('5'*prefix*9+'0'*zeros)


n = int(input())
fives = 0
zeros = 0

l = list(map(int,input().split()))

for i in l:
    if i == 0:
        zeros += 1
    else:
        fives += 1

prefix = fives//9
if prefix == 0 and zeros > 0:
    print(0)
elif prefix == 0:
    print(-1)
elif zeros == 0:
    print(-1)
else:
    print('5'*prefix*9+'0'*zeros)


n = int(input())
fives = 0
zeros = 0

l = list(map(int,input().split()))

for i in l:
    if l == '0':
        zeros += 1
    else:
        fives += 1

prefix = fives//9
if prefix == 0:
    print(0)
else:
    print('5'*prefix+'0'*zeros)


n = int(input())
fives = 0
zeros = 0

l = list(map(int,input().split()))

for i in l:
    if i == 0:
        zeros += 1
    else:
        fives += 1

prefix = fives//9
if prefix == 0 and zeros > 0:
    print(0)
elif prefix == 0:
    print(-1)
else:
    print('5'*prefix*9+'0'*zeros)


n=int(input())
l=sorted(input().split())
if set(l)=='5': print(-1)
elif l.count('5')<9: print(0)
else: print('5'*((l.count('5')//9)*9)+'0'*l.index('5'))


n=int(input())
l=sorted(input().split())
if set(l)=='5': print(-1)
else: print('5'*((l.count('5')//9)*9)+'0'*l.index('5'))


n=int(input())
l=sorted(input().split())
if set(l)=={'5'}: print(-1)
elif l.count('5')<9: print(0)
else: print('5'*((l.count('5')//9)*9)+'0'*l.index('5'))


n=int(input())
l=sorted(input().split())
if set(l)=='5': print(-1)
elif set(l)=='0': print(0)
else: print('5'*((l.count('5')//9)*9)+'0'*l.index('5'))


n=int(input())
l=list(map(int,input().split()))
l.sort()
l=l[::-1]

if l.pop()!=0:
    print(-1)
else:
    i=len(l)
    while(i>=0):
        if sum(l)%9==0:
            l.append(0)
            print(''.join([str(x) for x in l]))
            break
        i-=1
        l.pop()


n=int(input())
l=list(map(int,input().split()))
l.sort()
l=l[::-1]
if l[-1]!=0:
    print(-1)

else:
    zero = l[l.index(0):]
    l = l[:l.index(0)]
    i=len(l)
    if i==0:
        print(0)

    while(i>0):
        if sum(l)%9==0:
            l = l+zero
            print(''.join([str(x) for x in l]))
            break
        i-=1
        l.pop()
        if i==0:
            print(0)


n=int(input())
l=list(map(int,input().split()))
l.sort()
l=l[::-1]

if l[-1]!=0:
    print(-1)
else:
    zero = l[l.index(0):]
    l = l[:l.index(0)]
    i=len(l)
    while(i>=0):
        if sum(l)%9==0:
            l = l+zero
            print(''.join([str(x) for x in l]))
            break
        i-=1
        l.pop()


n=int(input())
l=list(map(int,input().split()))
l.sort()
l=l[::-1]

if l[-1]!=0:
    print(-1)
else:
    zero = l[l.index(0):]
    l = l[:l.index(0)]
    i=len(l)
    while(i>0):
        if sum(l)%9==0:
            l = l+zero
            print(''.join([str(x) for x in l]))
            break
        i-=1
        l.pop()
        if i==0:
            print(0)


n=int(input())
l=list(map(int,input().split()))
l.sort()
l=l[::-1]

if l.pop()!=0:
    print(-1)
else:
    i=len(l)
    while(i>0):
        if sum(l)%9==0:
            l.append(0)
            print(''.join([str(x) for x in l]))
            break
        i-=1
        l.pop()
        if i==0:
            print(0)


l=list(map(int,input().split()))
f=l.count(5)
z=l.count(0)
if z==0 or f<18:
    print(0)
else:
    while((f*5)%9!=0 && f>=18):
        f-=1
    print('5'*f+'0'*z)


l=list(map(int,input().split()))
f=l.count(5)
z=l.count(0)
if z==0 or f<18:
    print(0)
else:
    while((f*5)%9!=0 and f>=18):
        f-=1
    print(('5'*f)+('0'*z))


n=int(input())
l=list(map(int,input().split()))
f=l.count(5)
z=l.count(0)
if z==0 :
    print(0)
else:
    while((f*5)%9!=0):
        f-=1
    print(int(('5'*f)+('0'*z)))


n=int(input())
l=list(map(int,input().split()))
f=l.count(5)
z=l.count(0)
if z==0:
    print(-1)
else:
    while((f*5)%9!=0 and f>0):
        f-=1
    s=int(('5'*f)+('0'*z))
    print(s)


n=int(input())
l=list(map(int,input().split()))
f=l.count(5)
z=l.count(0)
if z==0 or f<18:
    print(0)
else:
    while((f*5)%9!=0 and f>=18):
        f-=1
    print(('5'*f)+('0'*z))


x=int(input())
a=list(str(input().replace(" ","")))
a.sort(reverse=True)
y=a.count("0")
flag=0
b=''.join(a)
for i in range(x):
    if int(b)%90==0:
        print(b)
        flag+=1
        break
    else:
        a.pop(len(a)-2)
        b = ''.join(a)

if flag==0 :
    if y!=0:
        print(0)
elif flag==0:
    print(-1)


x=int(input())
a=list(str(input().replace(" ","")))
a.sort(reverse=True)
y=a.count("0")
flag=0
if y==0:
    print(-1)
else:
    while a.count("5") > 0:
        if a.count("5")*5%9==0:
            print(''.join(a))
            flag+=1
            break
        else:
            a.pop(a.count("5")-1)

    if flag==0:
        print(0)


x=int(input())
a=list(str(input().replace(" ","")))
a.sort(reverse=True)
y=a.count("0")
flag=0
if y==0:
    print(-1)
else:
    for i in range(x):
        if a.count("5")*5%9==0:
            print(''.join(a))
            flag+=1
            break
        else:
            a.pop(len(a)-2)

    if flag==0:
        print(0)


x=int(input())
a=list(str(input().replace(" ","")))
a.sort(reverse=True)
y=a.count("0")
flag=0
if y==0:
    print(-1)
else:
    for i in range(x):
        if a.count("5")*5%9==0:
            print(''.join(a))
            flag+=1
            break
        else:
            a.pop(len(a)-2)

if flag==0:
    print(0)


def jeffAndDigits():
    n=int(input())
    s=[int(x) for x in input().split()]
    fivec=0
    zeroc=0
    fiven=0
    number=''
    for i in s:
        if i==5:
            fivec=fivec+1
        if i==0:
            zeroc=zeroc+1
    if fivec<3 and zeroc==0:
        print(-1)
        return
    elif fivec<3:
        print(0)
        return
    for i in range(fivec,2,-1):
        if i%3==0:
            fiven=i
            break
    for i in range(fiven):
        number=number+'5'
    for i in range(zeroc):
        number=number+'0'
    print(number)

jeffAndDigits()


def jeffAndDigits():
    n=int(input())
    s=[int(x) for x in input().split()]
    fivec=0
    zeroc=0
    fiven=0
    number=''
    for i in s:
        if i==5:
            fivec=fivec+1
        if i==0:
            zeroc=zeroc+1
    if fivec<9 and zeroc>0:
        print(0)
        return
    elif zeroc==0:
        print(-1)
        return
    for i in range(fivec,8,-1):
        if i%9==0:
            fiven=i
            break
    for i in range(fiven):
        number=number+'5'
    for i in range(zeroc):
        number=number+'0'
    print(number)

jeffAndDigits()


def jeffAndDigits():
    n=int(input())
    s=[int(x) for x in input().split()]
    fivec=0
    zeroc=0
    fiven=0
    number=''
    for i in s:
        if i==5:
            fivec=fivec+1
        if i==0:
            zeroc=zeroc+1
    if fivec<3 and zeroc>0:
        print(0)
        return
    elif fivec<3 or zeroc==0:
        print(-1)
        return
    for i in range(fivec,2,-1):
        if i%3==0:
            fiven=i
            break
    for i in range(fiven):
        number=number+'5'
    for i in range(zeroc):
        number=number+'0'
    print(number)

jeffAndDigits()


n = int(input())
temp = ""
li2 = []
li = list(map(int, input().split()))
if li.count(5) < 9:
    if li.count(0) > 0:
        print(0)
    else:
        print(-1)
else:
    li1 = [li[x] for x in range(n) if li[x] == 5]
    # print(li1)
    for i in range(len(li1)):
        temp += str(li1[i])
        if int(temp) % 9 == 0:
            li2.append(len(temp))
    for i in range(max(li2)):
        print(5, end='')
    for i in range(li.count(0)):
        print(0, end="")


n = int(input())
temp = ""
li2 = []
li = list(map(int, input().split()))
if li.count(5) < 9:
    if li.count(0) > 0:
        print(0)
    else:
        print(-1)
else:
    li1 = [li[x] for x in range(n) if li[x] == 5]
    # print(li1)
    for i in range(len(li1)):
        temp += str(li1[i])
        if int(temp) % 9 == 0:
            li2.append(len(temp))
    temp1 = ""
    for i in range(max(li2)):
        temp1 += "5"
    for i in range(li.count(0)):
        temp1 += "0"
    if int(temp1) % 90 == 0:
        for i in range(max(li2)):
            print(5, end='')
        for i in range(li.count(0)):
            print(0, end="")
    else:
        print(-1)


n = int(input())
li = list(map(int, input().split()))
temp = [x for x in li if x == 5]
# print(temp)
if sum(li) == 0:
    print(0)
else:
    if sum(temp) < 45:
        if li.count(0) == 0:
            print("-1")
        else:
            print(0)
    else:
        print("5555555550")


n = int(input())
temp = ""
li2 = []
li = list(map(int, input().split()))
if li.count(5) < 9:
    if li.count(0) > 0:
        print(0)
    else:
        print(-1)
else:
    li1 = [li[x] for x in range(n) if li[x] == 5]
    # print(li1)
    for i in range(len(li1)):
        temp += str(li1[i])
        if int(temp) % 9 == 0:
            li2.append(len(temp))
    temp1 = ""
    for i in range(max(li2)):
        temp1 += "5"
    if int(temp1) % 90 == 0:
        for i in range(max(li2)):
            print(5, end='')
        for i in range(li.count(0)):
            print(0, end="")
    else:
        print(-1)


n=int(input())
a=list(map(int,input().split()))
nbr0=0
nbr5=0
for x in a:
    if x==0:
        nbr0+=1
    else:
        nbr5+=1
if nbr0==0:
    print(-1)
else:
    b=nbr5//9
    s=0
    for i in range(b*9):
        s=s*10+5
    for i in range(nbr0):
        s=s*10
    #print([5]*b*9+[0]*nbr0)
    print(s)


n=int(input())
a=list(map(int,input().split()))
nbr0=0
nbr5=0
for x in a:
    if x==0:
        nbr0+=1
    else:
        nbr5+=1
if nbr0==0:
    print(-1)
else:
    s='5'*(nbr5//9)*9+'0'*nbr0
    print(s)


n=int(input())
a=list(map(int,input().split()))
nbr0=0
nbr5=0
for x in a:
    if x==0:
        nbr0+=1
    else:
        nbr5+=1
if nbr0==0:
    print(-1)
else:
    b=nbr5//9
    if b>=1:
        s='5'*(nbr5//9)*9+'0'*nbr0
    else:
        s='0'
    print(s)


n=int(input())
a=list(map(int,input().split()))
nbr0=0
nbr5=0
for x in a:
    if x==0:
        nbr0+=1
    else:
        nbr5+=1
if nbr0==0:
    print(-1)
else:
    b=nbr5//9
    s=int('5'*b*9+'0'*nbr0)
    print(s)


n = int(input())
a = list(map(int, input().split()))

count0 = 0
count5 = 0
for i in range(n):
  if a[i] == 5:
    count5 += 1
  else:
    count0 += 1

if count0 == 0:
  print(-1)
elif count5 < 9:
  print(0)
else:
  print('5' * (count5 - count5 % 9) + '0' * count0)


n = int(input())
a = list(map(int, input().split()))

count0 = 0
count5 = 0
for i in range(n):
  if a[i] == 5:
    count5 += 1
  else:
    count0 += 1

if count0 == 0 or count5 < 9:
  print(0)
else:
  print('5' * (count5 - count5 % 9) + '0' * count0)


n = int(input())
a = list(map(int, input().split()))

count0 = 0
count5 = 0
for i in range(n):
  if a[i] == 5:
    count5 += 1
  else:
    count0 += 1

for i in range(count5, -1, -1):
  if (i * 5) % 9 == 0:
    break

if i == 0 or count0 == 0:
  print(0)
else:
  print('5' * i + '0' * count0)


n = int(input())
a = list(map(int, input().split()))

count0 = 0
count5 = 0
for i in range(n):
  if a[i] == 5:
    count5 += 1
  else:
    count0 += 1

if count0 < (count5 // 9) or count5 < 9:
  print(0)
else:
  print('5' * (count5 - count5 % 9) + '0' * (count5 // 9))


n=int(input())
a=list(map(int,input().split()))
z1=a.count(5)
z2=a.count(0)
l=[]
if(z1>=9 and z2>=1):
	x=z1//9
	for i in range(9*x):
		l.append('5')
	for i in range(z2):
		l.append('0')
	print("".join(l))
else:
	if(z2>=1):
		print(0)
	if(z2==0):
		print(-1)
	else:
		print(-1)


n=int(input())
a=list(map(int,input().split()))
z1=a.count(5)
z2=a.count(0)
l=[]
if(z1>=9):
	x=z1//9
	for i in range(9*x):
		l.append('5')
	for i in range(z2):
		l.append('0')
	print("".join(l))
else:
	if(z2>=1):
		print(0)
	if(z2==0):
		print(-1)


n=int(input())
a=list(map(int,input().split()))
z1=a.count(5)
z2=a.count(0)
l=[]
if(z1>=9 and z2>=1):
	x=z1//9
	for i in range(9*x):
		l.append('5')
	for i in range(z2):
		l.append('0')
	print("".join(l))
else:
	if(z2>=1):
		print(0)
	elif(z2==0):
		print(-1)
	else:
		print(-1)


n=int(input())
a=list(map(int,input().split()))
z1=a.count(5)
z2=a.count(0)
l=[]
if(z1>=9):
	x=z1//9
	for i in range(9*x):
		l.append('5')
	for i in range(z2):
		l.append('0')
	print("".join(l))
else:
	if(z2>1):
		print(0)
	if(z2==0):
		print(-1)


n = int(input())
nums = [int(x) for x in input().split(' ')]

count = nums.count(5)
countZeros = len(nums) - count
if count//3 >= 1:
    count = count//3*3
    ans = '5'*count + '0'*countZeros
else:
    ans = 0
print(ans)


n = int(input())
nums = [int(x) for x in input().split(' ')]

count = nums.count(5)
countZeros = len(nums) - count
if countZeros == 0:
    print(-1)
elif count//3 >= 1:
    count = count//3*3
    ans = '5'*count + '0'*countZeros
    print(ans)
else:
    print(0)


n = int(input())
nums = [int(x) for x in input().split(' ')]

count = nums.count(5)
countZeros = len(nums) - count
if countZeros == 0:
    print(-1)
elif count//9 >= 1:
    count = count//9*9
    ans = '5'*count + '0'*countZeros
    print(ans)
else:
    print(0)


n = int(input())
a = list(map(int, input().split()))

if a.count(5) < 9 or a.count(0) == 0:
    out = -1
else:
    out = "5"*((a.count(5)//9)*9)
    out += "0" * a.count(0)

print(int(out))


n = int(input())
a = list(map(int, input().split()))

if a.count(5) < 9 and a.count(0) == 0:
    out = -1
else:
    out = "5"*((a.count(5)//9)*9)
    out += "0" * a.count(0)

print(int(out))


n = int(input())
a = list(map(int, input().split()))

out = "5"(*a.count(5)//9)
out += "0" * a.count(0)

print(int(out))


n = int(input())
a = list(map(int, input().split()))

if a.count(5) < 9 and a.count(0) == 0:
    out = -1
elif a.count(5) >=9 and a.count(0) == 0:
    out = -1
else:
    out = "5"*((a.count(5)//9)*9)
    out += "0" * a.count(0)

print(int(out))


n = int(input())
a = list(map(int, input().split()))

out = "5"*(a.count(5)//9)
out += "0" * a.count(0)

print(int(out))


n = int(input())
a = list(map(int, input().split()))

out = "5"*((a.count(5)//9)*9)
out += "0" * a.count(0)

print(int(out))


n = int(input())
a = list(map(int, input().split()))

if a.count(i) < 9 and a.count(0)==0:
    out = -1
else:
    out = "5"*((a.count(5)//9)*9)
    out += "0" * a.count(0)

print(int(out))


n = int(input())
a = list(map(int, input().split()))

if a.count(5) >= 9 and a.count(0) != 0:
    out = "5"*((a.count(5)//9)*9)
    out += "0" * a.count(0)

else:
    out = -1

print(int(out))


n = int(input())
a = list(map(int, input().split()))

if a.count(5) % 9 == 0:
    out = "5"*a.count(5)
    out += "0" * a.count(0)
else:
    out = "0"

print(int(out))


# coding=utf-8

if __name__ == '__main__':
	n = int(input())
	line = str(input()).split()
	line = [int(it) for it in line]
	num_5 = sum(line) // 5
	num_0 = n - num_5
	num_5 //= 3
	num_5 *= 3
	if num_5 > 0:
		if num_0 == 0:
			print(-1)
		else:
			print('5' * num_5 + '0' * num_0)
	else:
		print(0)


# coding=utf-8

if __name__ == '__main__':
	n = int(input())
	line = str(input()).split()
	line = [int(it) for it in line]
	num_5 = sum(line) // 5
	num_0 = n - num_5
	num_5 //= 9
	num_5 *= 9
	if num_0 == 0:
		print(-1)
	elif num_5 > 0:
		print('5' * num_5 + '0' * num_0)
	else:
		print(0)


# coding=utf-8

if __name__ == '__main__':
	n = int(input())
	line = str(input()).split()
	line = [int(it) for it in line]
	num_5 = sum(line) // 5
	num_0 = n - num_5
	num_5 //= 3
	num_5 *= 3
	if num_0 == 0:
		print(-1)
	elif num_5 > 0:
		print('5' * num_5 + '0' * num_0)
	else:
		print(0)


# coding=utf-8

if __name__ == '__main__':
	n = int(input())
	line = str(input()).split()
	line = [int(it) for it in line]
	num_5 = sum(line) // 5
	num_0 = n - num_5
	num_5 //= 3
	num_5 *= 3
	if num_5 > 0:
		print('5' * num_5 + '0' * num_0)
	else:
		print(0)


n = int(input())
l = list(map(int,input().split()))

count1 = 0
count2 = 0

for i in range(n):
    if(l[i] == 5):
        count1+=1
    else:
        count2+=1

if(count1<9):
    print("0")
else:
    t = count1//9
    z = t*9
    s = ""
    while(z):
        s+='5'
        z-=1
    while(count2):
        s+='0'
        count2-=1
    print(s)


n = int(input())
l = list(map(int,input().split()))

count1 = 0
count2 = 0

for i in range(n):
    if(l[i] == 5):
        count1+=1
    else:
        count2+=1

if(count1<9 and count2>0):
    print("0")
elif(count1<9 and count2==0):
    print("-1")
else:
    t = count1//9
    z = t*9
    s = ""
    while(z):
        s+='5'
        z-=1
    while(count2):
        s+='0'
        count2-=1
    print(s)


n = int(input())
l = list(map(int,input().split()))

count1 = 0
count2 = 0

for i in range(n):
    if(l[i] == 5):
        count1+=1
    else:
        count2+=1

if(count2==0):
    print("-1")
elif(count1<9):
    print("0")
else:
    t = count1//9
    z = t*9
    s = ""
    while(z):
        s+='5'
        z-=1
    while(count2):
        s+='0'
        count2-=1
    print(s)


n=int(input())
l=input()
l=l.split()
if(l.count('5')<9):
	print(0)
else:
	a=l.count('5')//9
	b=l.count('0')
	s='5'*(a*9)
	s1='0'*b
	print(s+s1)


n=int(input())
l=input()
l=l.split()
if(l.count('5')<9 and l.count('0')!=0):
	print(0)
elif(l.count('5')<9 and l.count('0')==0):
	print(-1)
else:
	a=l.count('5')//9
	b=l.count('0')
	s='5'*(a*9)
	s1='0'*b
	print(s+s1)


n=int(input())
l=input()
l=l.split()
if(l.count('5')<9 and l.count('0')!=0):
	print(0)
elif(l.count('5')<9 and l.count('0')==0):
	print(-1)
elif:
	a=l.count('5')//9
	b=l.count('0')
	s='5'*(a*9)
	s1='0'*b
	print(s+s1)


n=int(input())
l=input()
l=l.split()
if(l.count('5')<9 and l.count('0')!=0):
	print(0)
elif(l.count('5')<9 and l.count('0')==0):
	print(-1)
elif(l.count('5')>=9 and l.count('0')!=0):
	a=l.count('5')//9
	b=l.count('0')
	s='5'*(a*9)
	s1='0'*b
	print(s+s1)
else:
	print(-1)


n = int(input())
arr = list(map(int,input().split()))
five_count = arr.count(5)
zero_count = n-five_count
x = five_count//9
if x==0:
    print(0)
else:
    print('5'*9*x+'0'*zero_count)


n = int(input())
arr = list(map(int,input().split()))
five_count = arr.count(5)
zero_count = n-five_count
x = five_count//9
if x==0:
    if zero_count != 0:
        print(0)
    else:
        print(-1)
else:
    if zero_count == 0:
        print(-1)
    else:
        print('5'*9*x+'0'*zero_count)


n = int(input())
arr = list(map(int,input().split()))
five_count = arr.count(5)
zero_count = n-five_count
x = five_count//9
if x==0:
    if zero_count != 0:
        print(0)
    else:
        print(-1)
else:
    print('5'*9*x+'0'*zero_count)


n=int(input())
a=list(map(int,input().split()))
x=a.count(5)
y=a.count(0)
if x//9==0 or y==0:
    print("0")
else:
    for i in range(x//9):
        print("999999999",end='')
    for i in range(y):
        print("0",end="")


n=int(input())
a=list(map(int,input().split()))
x=a.count(5)
y=a.count(0)
if x//9==0 or y==0:
    print("0")
else:
    for i in range(x//9):
        print("555555555",end='')
    for i in range(y):
        print("0",end="")


n=int(input())
a=list(map(int,input().split()))
x=a.count(5)
y=a.count(0)
if x//9==0 and y!=0:
    print("0")
elif y==0 and x//9==0:
    print("-1")
elif x//9>0 and y!=0:
    for i in range(x//9):
        print("555555555",end='')
    for i in range(y):
        print("0",end="")


n=int(input())
a=list(map(int,input().split()))
x=a.count(5)
y=a.count(0)
if x//9==0 and y!=0:
    print("0")
elif y==0 and x//9==0:
    print("-1")
else:
    for i in range(x//9):
        print("555555555",end='')
    for i in range(y):
        print("0",end="")


n=int(input())
a=list(map(int,input().split()))
x=a.count(5)
y=a.count(0)
if x//9==0 and y!=0:
    print("0")
elif y==0:
    print("-1")
else:
    for i in range(x//9):
        print("555555555",end='')
    for i in range(y):
        print("0",end="")


n = int(input())
seq = list(map(int, input().split()))

five = 0
zero = 0

for i in seq:
    if i == 5:
        five += 1
    elif i == 0:
        zero += 1

if five >= 9:
    mod = five % 9
    if zero >= 1:
        print((five - mod) * '5' + zero * '0')
    else:
        print(-1)
else:
    if zero > 0:
        print(0)
    else:
        print(-1)


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0;
    double ans = 0 ;
    double  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

    if (three > 0 && one < 0) {
        while (three > 0){
            three -= 3;
            ans++;}
    }

    //
   // if (three % 4 == 0)
 //       ans += three / 4;
//    else {

    while (one > 0 && three > 0) {
        u3 -= 3;
        one--;
        ans++;
    }

        if (three > 0)

        ans += ceil(u3 / 4);

   // }
        //
        if (two % 4 == 0)
            ans += two / 4;
        else {
            ans += ceil(u2 / 4);
            if (one > 0)

            //    ans = ceil(u)
                one--;

            if (one > 1)
                one -= 2;
        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0, k1 = 0;
    long long ans = 0 ;
    long double  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (one == 0 && three > 0) {
            three -= 3;
            ans++;
    }

    u3 = three;

    while (one > 0 && three > 0) {
        u3 -= 3;
        one--;
        ans++;
    }

    three = u3;
    u1 = one;

            while (three > 0) {
                three -= 3;
                ans++;
            }

        if (two % 4 == 0)
            ans += two / 4;

        else {
            ans += ceil(u2 / 4);

            if (one > 1)
                one -= 2;
            else {

            if (one > 0)
                one--;
            }

        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0;
    double ans = 0 ;
    double  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (u3 > 0) {
            u3 -= 3;

            ans++;
        }
    //
   // if (three % 4 == 0)
 //       ans += three / 4;
//    else {

    while (one > 0 && three > 0) {
        u3 -= 3;
        one--;
        ans++;
    }

        if (three > 0)

        ans += ceil(u3 / 4);

   // }
        //
        if (two % 4 == 0)
            ans += two / 4;
        else {
            ans += ceil(u2 / 4);
            if (one > 0)

            //    ans = ceil(u)
                one--;

            if (one > 1)
                one -= 2;
        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0;
    long long ans = 0 ;
    long double  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (three > 0 && one == 0 && two == 0) {
            three -= 3;
            ans++;
    }

    while (one > 0 && three > 0) {
        u3 -= 3;
        one--;
        ans++;
    }

        if (three > 0)

        ans += ceil(u3 / 4);

        if (two % 4 == 0)
            ans += two / 4;

        else {
            ans += ceil(u2 / 4);

            if (one > 1)
                one -= 2;
            else{

            if (one > 0)
                one--;}

        }
    //
    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0, ans = 0 ;
    float  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (one > 0 && three > 0) {
            u3 -= 3;
            three -= 3;
            one--;
            ans++;
        }
        if (three > 0)

        ans += ceil(u3 / 4);

        if (two % 4 == 0)
            ans += two / 4;
        else {
            ans += ceil(u2 / 4);
            if (one > 0)
                one--;

            if (one > 1)
                one -= 2;
        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0;
    double ans = 0 ;
    double  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (one > 0 && three > 0) {
            u3 -= 3;
            one--;
            ans++;
        }
    //
   // if (three % 4 == 0)
 //       ans += three / 4;
//    else {
        if (three > 0)

        ans += ceil(u3 / 4);

   // }

        if (two % 4 == 0)
            ans += two / 4;
        else {
            ans += ceil(u2 / 4);
            if (one > 0)

            //    ans = ceil(u)
                one--;

            if (one > 1)
                one -= 2;
        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0;
    long long ans = 0 ;
    long double  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (three > 0 && one == 0 && two == 0) {
            three -= 3;
            ans++;
    }

    while (one > 0 && three > 0) {
        u3 -= 3;
        one--;
        ans++;
    }

        if (three > 0)

        ans += ceil(u3 / 4);

        if (two % 4 == 0)
            ans += two / 4;

        else {
            ans += ceil(u2 / 4);

            if (one > 1)
                one -= 2;
            else {

            if (one > 0)
                one--;
            }

        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0;
    double ans = 0 ;
    double  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (one > 0 && three > 0) {
            three -= 3;
            one--;
            ans++;
        }
    //
   // if (three % 4 == 0)
 //       ans += three / 4;
//    else {
        if (three > 0)

        ans += ceil(u3 / 4);

   // }

        if (two % 4 == 0)
            ans += two / 4;
        else {
            ans += ceil(u2 / 4);
            if (one > 0)

            //    ans = ceil(u)
                one--;

            if (one > 1)
                one -= 2;
        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0, ans = 0 ;
    float  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (one > 0 && three > 0) {
            u3 -= 3;
            one--;
            ans++;
        }
        if (three > 0)

        ans += ceil(u3 / 4);

        if (two % 4 == 0)
            ans += two / 4;
        else {
            ans += ceil(u2 / 4);
            if (one > 0)
                one--;

            if (one > 1)
                one -= 2;
        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    //

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0, ans = 0 ;
    float  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (one > 0 && three > 0) {
            u3 -= 3;
            one--;
            ans++;
        }
        if (three > 0)

        ans += ceil(u3 / 4);

        if (two % 4 == 0)
            ans += two / 4;
        else {
            ans += ceil(u2 / 4);
            if (one > 0)
                one--;

            if (one > 1)
                one -= 2;
        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;
//
int main() {

    double one=0, two=0, three=0, out=0, four=0;
    int n=0;
    cin >> n;
    int a[n];
    for (int i=0;i<n;i++) {
        cin >> a[i];
        if (a[i]==1) one++;
        if (a[i]==2) two++;
        if (a[i]==3) three++;
        if (a[i]==4) four++;
    }
    out=four;
    if (three != 0 && one == 0) {
        out=out + three +ceil((two*2)/4);
        cout << out;
        return 0;
    }
    if (three == 0 && one != 0) {
        out=out + ceil((one)/4) +floor(((two*2))/4);
        cout << out;
        return 0;
    }
    if (three == 0 && one == 0) {
        out=out + ceil((two*2)/4);
        cout << out;
        return 0;
    }
    if (three >= one) {
        out = out+one+(three-one)+ceil((two*2)/4);
        cout << out;
        return 0;
    }
    if (three <= one) {
        out = out+three+(one-three)+ceil((two*2)/4);
        cout << out;
        return 0;
    }

}


#include <iostream>
#include <cmath>
using namespace std;

int main() {

    double one=0, two=0, three=0, out=0, four=0;
    int n=0;
    cin >> n;
    int a[n];
    for (int i=0;i<n;i++) {
        cin >> a[i];
        if (a[i]==1) one++;
        if (a[i]==2) two++;
        if (a[i]==3) three++;
        if (a[i]==4) four++;
    }
    out=four;
    if (three != 0 && one == 0) {
        out=out + three +ceil((two*2)/4);
        cout << out;
        return 0;
    }
    if (three == 0 && one != 0) {
        out=out + ceil((one)/4) +floor(((two*2))/4);
        cout << out;
        return 0;
    }
    if (three == 0 && one == 0) {
        out=out + ceil((two*2)/4);
        cout << out;
        return 0;
    }
    if (three >= one) {
        out = out+one+(three-one)+ceil((two*2)/4);
        cout << out;
        return 0;
    }
    if (three <= one) {
        out = out+three+(one-three)+ceil((two*2)/4);
        cout << out;
        return 0;
    }

}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0, ans = 0 ;
    float  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (one > 0 && three > 0) {
            u3 -= 3;
            one--;
            ans++;
        }
        if (three > 0)
            u3 = three;

        ans += ceil(u3 / 4);

        if (two % 4 == 0)
            ans += two / 4;
        else {
            u2 = two;
            ans += ceil(u2 / 4);
            if (one > 0)
                one--;

            if (one > 1)
                one -= 2;
        }

    u1 = one;

        if (one > 0) {
            u1 = one;
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0;
    long long ans = 0 ;
    long double  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (three > 0 && one == 0) {
            three -= 3;
            ans++;
    }

    u3 = three;

    while (one > 0 && three > 0) {
        u3 -= 3;
        three--;
        one--;
        ans++;
    }

    three = u3;

        if (three > 0)
            ans += ceil(u3 / 4);

        if (two % 4 == 0)
            ans += two / 4;

        else {
            ans += ceil(u2 / 4);

            if (one > 1)
                one -= 2;
            else {

            if (one > 0)
                one--;
            }

        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {

    double one=0, two=0, three=0, out=0, four=0;
    int n=0, a[n];
    cin >> n;
    for (int i=0;i<n;i++) {
        cin >> a[i];
        if (a[i]==1) one++;
        if (a[i]==2) two++;
        if (a[i]==3) three++;
        if (a[i]==4) four++;
    }
    out=four;
    if (three == 0 && one == 0) {
        out=out + ceil((two*2)/4);
        cout << out;
        return 0;
    }
    if (three >= one) {
        out = out+one+(three-one)+ceil((two*2)/4);
        cout << out;
        return 0;
    }
    if (three <= one) {
        out = out+three+(one-three)+ceil((two*2)/4);
        cout << out;
        return 0;
    }

}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0;
    long long ans = 0 ;
    long double  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (three > 0 && one == 0 && two == 0) {
            three -= 3;
            ans++;
    }

    while (one > 0 && three > 0) {
        u3 -= 3;
        one--;
        ans++;
    }

        if (three > 0)
            ans += ceil(u3 / 4);

        if (two > 0 && two % 4 == 0)
            ans += two / 4;

        else {
            ans += ceil(u2 / 4);

            if (one > 1)
                one -= 2;
            else {

            if (one > 0)
                one--;
            }

        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0;
    long long ans = 0 ;
    long double  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (three > 0 && one == 0 && two == 0) {
            three -= 3;
            ans++;
    }

    while (one > 0 && three > 0) {
        u3 -= 3;
        one--;
        ans++;
    }

        if (three > 0){
If(three % 4 == 0)  ans += ceil(u3 / 4) + 1;
        ans += ceil(u3 / 4);
    }
        if (two % 4 == 0)
            ans += two / 4;

        else {
            ans += ceil(u2 / 4);

            if (one > 1)
                one -= 2;
            else {

            if (one > 0)
                one--;
            }

        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0, ans = 0 ;
    float  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (one > 0 && three > 0) {
            three -= 3;
            one--;
            ans++;
        }
        if (three > 0)

        ans += ceil(u3 / 4);

        if (two % 4 == 0)
            ans += two / 4;
        else {
            ans += ceil(two/4);
            if (one > 0)
                one--;

            if (one > 1)
                one -= 2;
        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0, k1 = 0;
    long long ans = 0 ;
    long double  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (one == 0 && three > 0) {
            three -= 3;
            ans++;
    }

    u3 = three;

    while (one > 0 && three > 0) {
        u3 -= 3;
        one--;
        ans++;
    }

    three = u3;
    u1 = one;

            while (three > 0) {
                three -= 3;
                ans++;
            }

        if (two % 4 == 0)
            ans += two / 4;

        else {
           ans += ceil(u2 / 4);

            if (one > 1)
                one -= 2;
            else {

            if (one > 0)
                one--;
            }

        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


158B#include <iostream>
#include <cmath>
using namespace std;

int main() {

double one=0, two=0, three=0, out=0, four=0;
int n=0;
cin >> n;
int a[100000];
for (int i=0;i<n;i++) {
cin >> a[i];
if (a[i]==1) one++;
if (a[i]==2) two++;
if (a[i]==3) three++;
if (a[i]==4) four++;
}
out=four;
if (three != 0 && one == 0) {
out=out + three +ceil((two*2)/4);
cout << out;
return 0;
}
if (three == 0 && one != 0) {
out=out + ceil((one)/4) +floor(((two*2))/4);
cout << out;
return 0;
}
if (three == 0 && one == 0) {
out=out + ceil((two*2)/4);
cout << out;
return 0;
}
if (three >= one) {
out = out+one+(three-one)+ceil((two*2)/4);
cout << out;
return 0;
}
if (three <= one) {
out = out+three+(one-three)+ceil((two*2)/4);
cout << out;
return 0;
}


#include <iostream>
#include <cmath>
using namespace std;

int main() {

    double one=0, two=0, three=0, out=0, four=0;
    int n=0;
    cin >> n;
    int a[n];
    for (int i=0;i<n;i++) {
        cin >> a[i];
        if (a[i]==1) one++;
        if (a[i]==2) two++;
        if (a[i]==3) three++;
        if (a[i]==4) four++;
    }
    out=four;
    if (three != 0 && one == 0) {
        out=out + three +ceil((two*2)/4);
        cout << out;
        return 0;
    }
    if (three == 0 && one != 0) {
        out=out + ceil((one)/4) +floor(((two*2))/4);
        cout << out;
        return 0;
    }
    if (three == 0 && one == 0) {
        out=out + ceil((two*2)/4);
        cout << out;
        return 0;
    }
    if (three >= one) {
        out = out+one+(three-one)+floor((two*2)/4);
        cout << out;
        return 0;
    }
    if (three <= one) {
        out = out+three+(one-three)+ceil((two*2)/4);
        cout << out;
        return 0;
    }

}


#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main() {
    int n, one = 0, two = 0, three = 0, temp = 0, ans = 0 ;
    float  u1, u2, u3;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> temp;
        if (temp == 4){
            ans++; continue;
        }
        switch (temp) {
                case 1: one++; break;
                case 2: two += 2; break;
            case 3: three += 3; break;
        }

    }

    u1 = one;
    u2 = two;
    u3 = three;

        while (one > 0 && three > 0) {
            three -= 3;
            one--;
            ans++;
        }
        if (three > 0)

        ans += ceil(u3 / 4);

        if (two % 4 == 0)
            ans += two / 4;
        else {
            ans += ceil(u2/4);
            if (one > 0)
                one--;

            if (one > 1)
                one -= 2;
        }

    u1 = one;

        if (one > 0) {
            ans += ceil(u1 / 4);
        }

    cout << ans;
    return 0;
}


#include <iostream>
using namespace std;

int main(){
	int n;
	cin>>n;
	int c=0;
	int p[n];
	int s=0;
	int t=1,l=0;
	while(c<n && cin >>p[c]){
		s = s + p[c];
		if(s>=4){
			l=s/4;
			t+=l;
			s = s - 4*l;
		}
		c++;
	}
	t= (n==3 && s==8 || s%4!=0) ? t++: t;
	cout<<t;
}