This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define file ""
#define all(x) x.begin(), x.end()
#define sc second
#define fr first
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const ll inf = 1e18 + 5;
const ll mod = 1e9 + 7;
const int N = 1e6 + 5;
int dx[] = {+1, 0, -1, 0};
int dy[] = {0, +1, 0, -1};
int n, m;
void solve1() {
 	cout << max(n, m) - 1 << endl;
 	for (int i = 1; i < max(n, m); i++) 
 		cout << (min(n, m) == n ? -1 : 1) << " ";
	cout << endl;
}
void solve2() {
	cout << max(n, m) << endl;
	cout << (n == 2 ? 1 : -1) * (max(n, m) / 2 + 1) << " ";
 	for (int i = 1; i <= max(n, m) / 2; i++) 
 		if (n == 2) cout << "-1000000 999999 ";
 		else cout << "1000000 -999999 ";
	cout << endl;
}
vector<int> a;
bool solve3() {
	if (a.size() == max(n, m)) {
	 	for (int i = 0; i < a.size(); i++) {
	 		ll sum = 0;
	 		if (i + n - 1 >= a.size()) continue;
	 		int cnt = n;
	 		for (int j = i; cnt > 0; j++, cnt--) sum += a[j];
	 		if (sum >= 0) return 0;
	 	}
	 	for (int i = 0; i < a.size(); i++) {
	 		ll sum = 0;
	 		if (i + m - 1 >= a.size()) continue;
	 		int cnt = m;
	 		for (int j = i; cnt > 0; j++, cnt--) sum += a[j];
	 		if (sum <= 0) return 0;
	 	}
	 	for (int to : a) cout << to << " ";
	 	cout << endl;
		return 1;
	}
	for (int i = -3; i <= 3; i++) {
		if (i == 0) continue;
	 	a.pb(i);
	 	if (solve3()) return 1;
	 	a.pop_back();
	}
	return 0;
}
bool check(vector<int>& a) {
	int mn = min(n, m);
	int mx = max(n, m);
	for (int i = 0; i < a.size(); i++) {
		ll sum = 0;
		if (i + mn - 1 >= a.size()) continue;
		int cnt = mn;
		for (int j = i; cnt > 0; j++, cnt--) sum += a[j];
		if (sum <= 0) {
			return 0;
		}
	}
	for (int i = 0; i < a.size(); i++) {
		ll sum = 0;
		if (i + mx - 1 >= a.size()) continue;
		int cnt = mx;
		for (int j = i; cnt > 0; j++, cnt--) sum += a[j];
		if (sum >= 0) return 0;
	}
	return 1; 	 	
}
void solve4() {
	int x = min(n, m);
	int y = max(n, m);
	vector<int> ans(x);
	for (int i = 0; i < x / 2; i++) {
		ans[i] = -1e6;
		ans[x - i - 1] = +1e6;
	}
	if (x % 2 == 0) ans[x / 2 - 1] = -1e6 + 1, ans[x / 2] = 1e6;
	if (x % 2 == 1) ans[(x + 1) / 2 - 1] = 1; 
	vector<int> all;
	int last = 0;
	while (all.size() < y) {
	 	all.pb(ans[last++]);
	 	if (last == x) last = 0;
	}
	while (check(all)) {
	 	all.pb(ans[last++]);
	 	if (last == x) last = 0;
	}
	cout << all.size() - 1 << endl;
	for (int i = 0; i < all.size() - 1; i++) cout << all[i] * (x == n ? -1 : 1) << " ";
}
void solve() {
 	cin >> n >> m;
 	a.clear();
 	if (max(n, m) % min(n, m) == 0) {solve1(); return;}
 	if (min(n, m) == 2) {solve2(); return;}
 	solve4();
}
int main() { 
	ios_base :: sync_with_stdio(false);               
	cin.tie(nullptr);
	srand(time(nullptr));
	int t;
	cin >> t;
	while (t--) solve();
  return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'bool solve3()':
sequence.cpp:46:15: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'const int' [-Wsign-compare]
   46 |  if (a.size() == max(n, m)) {
      |      ~~~~~~~~~^~~~~~~~~~~~
sequence.cpp:47:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |    for (int i = 0; i < a.size(); i++) {
      |                    ~~^~~~~~~~~~
sequence.cpp:49:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |     if (i + n - 1 >= a.size()) continue;
      |         ~~~~~~~~~~^~~~~~~~~~~
sequence.cpp:54:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |    for (int i = 0; i < a.size(); i++) {
      |                    ~~^~~~~~~~~~
sequence.cpp:56:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |     if (i + m - 1 >= a.size()) continue;
      |         ~~~~~~~~~~^~~~~~~~~~~
sequence.cpp: In function 'bool check(std::vector<int>&)':
sequence.cpp:77:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |  for (int i = 0; i < a.size(); i++) {
      |                  ~~^~~~~~~~~~
sequence.cpp:79:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |   if (i + mn - 1 >= a.size()) continue;
      |       ~~~~~~~~~~~^~~~~~~~~~~
sequence.cpp:86:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |  for (int i = 0; i < a.size(); i++) {
      |                  ~~^~~~~~~~~~
sequence.cpp:88:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |   if (i + mx - 1 >= a.size()) continue;
      |       ~~~~~~~~~~~^~~~~~~~~~~
sequence.cpp: In function 'void solve4()':
sequence.cpp:108:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  108 |  while (all.size() < y) {
      |         ~~~~~~~~~~~^~~
sequence.cpp:117:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |  for (int i = 0; i < all.size() - 1; i++) cout << all[i] * (x == n ? -1 : 1) << " ";
      |                  ~~^~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |