#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <ctime>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cassert>
#include <bitset>
#include <numeric>

using namespace std;

const int MAXN = (int)1e6 + 11;
long long dp[MAXN];

int main() {
	int task;
	scanf("%d", &task);
	while (task -- > 0) {
		long long n;
		int a, b, c;
		scanf("%lld %d %d %d", &n, &a, &b, &c);
		int m = 0;
		dp[0] = 1;
		while (true) {
			if (dp[m] >= n) {
				break;
			}
			++ m;
			long long pa = m >= a ? dp[m - a] : 0;
			long long pb = m >= b ? dp[m - b] : 0;
			long long pc = m >= c ? dp[m - c] : 0;
			dp[m] = max(1ll, pa + pb + pc);
		}
		printf("%d\n", m);
	}
	return 0;
}