This page is still under construction.

Parts of this page are still being built. What you see may change.

Modified LCS

Time limit1sMemory limit128 MB

Summary
Count the terms shared by two strictly increasing arithmetic progressions, which equals their longest common subsequence.
Level

Medium5 of 10

Topics
Number theory, Math
Solved
No attempts yet

Problem

LCS stands for longest common subsequence, a well known problem. A sequence in this problem is a list of integers, and a sequence XX is a subsequence of a sequence YY when you can obtain XX by deleting zero or more elements from YY and leaving the remaining elements in their original order.

You are given two sequences. Find the length of the longest sequence that is a subsequence of both of them.

The sequences themselves are not given. Each sequence is described by three integers NN, FF and DD, where NN is the length of the sequence and FF is its first element. Every element except the first is greater than the element before it by DD.

For example, N=5N = 5, F=3F = 3 and D=4D = 4 describes the sequence [3,7,11,15,19][3, 7, 11, 15, 19].

At least one integer belongs to both sequences and is not greater than 1,000,000.

Input

The first line contains a single integer TT, the number of test cases (1≤T≤1001 \le T \le 100). Each of the next TT lines describes one test case and contains six integers separated by a single space, N1N_1, F1F_1, D1D_1, N2N_2, F2F_2, D2D_2 (1≤N1,N2≤10181 \le N_1, N_2 \le 10^{18}, 1≤F1,D1,F2,D2≤1091 \le F_1, D_1, F_2, D_2 \le 10^9). They are the length of the first sequence, the first element of the first sequence, the increment of the first sequence, the length of the second sequence, the first element of the second sequence, and the increment of the second sequence, in that order.

Output

For each test case, print one line with a single integer, the length of the longest common subsequence of the two sequences.

Examples1

  1. Example 1

    Input
    3
    5 3 4 15 3 1
    10 2 2 7 3 3
    100 1 1 100 1 2
    
    Expected output
    4
    3
    50