PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#QHD0000071. Đổi tiền với giới hạn số đồng (Coin Changing Again)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đổi tiền với giới hạn số đồng (Coin Changing Again)

Coin Changing Again

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

There are four distinct coin denominations c1<c2<c3<c4c_1<c_2<c_3<c_4. Each query specifies upper bounds d1,d2,d3,d4d_1,d_2,d_3,d_4 on how many coins of each type can be used, and a target value vv.

Count the number of integer quadruples (x1,x2,x3,x4)(x_1,x_2,x_3,x_4) satisfying 0≤xi≤di0\le x_i\le d_i and c1x1+c2x2+c3x3+c4x4=vc_1x_1+c_2x_2+c_3x_3+c_4x_4=v. Coin order does not matter, only multiplicities. Print zero if no such combination exists, without reducing the answer modulo anything.

All qq queries share the same four denominations and belong to one input instance. Print one answer per query in the original order. For denominations 1,2,5,101,2,5,10, bounds 3,2,3,13,2,3,1, and target 1010, exactly four combinations are possible.

Input

The first line contains c1,c2,c3,c4,qc_1,c_2,c_3,c_4,q. Each of the following qq lines contains d1,d2,d3,d4,vd_1,d_2,d_3,d_4,v.

Output

Print qq lines containing the exact number of valid combinations for each respective query.

Subtasks

  • Subtask 1 (20 points): 1≤q≤101\le q\le 10; 1≤c1<c2<c3<c4≤501\le c_1<c_2<c_3<c_4\le 50; 1≤di≤301\le d_i\le 30; 1≤v≤2001\le v\le 200 for every query.
  • Subtask 2 (30 points): 1≤q≤401\le q\le 40; 1≤c1<c2<c3<c4≤2001\le c_1<c_2<c_3<c_4\le 200; 1≤di≤10001\le d_i\le 1000; 1≤v≤100001\le v\le 10000 for every query.
  • Subtask 3 (50 points): 1≤q≤1001\le q\le 100; 1≤c1<c2<c3<c4≤10001\le c_1<c_2<c_3<c_4\le 1000; 1≤di≤1000001\le d_i\le 100000; 1≤v≤1000001\le v\le 100000 for every query.

Examples

Input

1 2 5 10 2
3 2 3 1 10
1000 2 2 2 900

Output

4
27

Explanation

The first query has four valid multiplicity vectors: (0,0,0,1)(0,0,0,1), (0,0,2,0)(0,0,2,0), (1,2,1,0)(1,2,1,0) and (3,1,1,0)(3,1,1,0). The second query produces 2727 valid combinations of total 900900.