You are given two sets of integers: A and B. You need to output the sum of elements in the set C = \\{x | x = a ⊕ b, a ∈ A, b ∈ B\} modulo 998244353, where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). Each number should be counted only once.

For example, if A = \{2, 3\} and B = \{2, 3\} you should count integer 1 only once, despite the fact that you can get it as 3 ⊕ 2 and as 2 ⊕ 3. So the answer for this case is equal to 1 + 0 = 1.

Let's call a segment [l; r] a set of integers \\{l, l+1, ..., r\}.

The set A is given as a union of n_A segments, the set B is given as a union of n_B segments.

Input

The first line contains a single integer n_A (1 ≤ n_A ≤ 100).

The i-th of the next n_A lines contains two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ 10^{18}), describing a segment of values of set A.

The next line contains a single integer n_B (1 ≤ n_B ≤ 100).

The i-th of the next n_B lines contains two integers l_j and r_j (1 ≤ l_j ≤ r_j ≤ 10^{18}), describing a segment of values of set B.

Note that segments in both sets may intersect.

Output

Print one integer — the sum of all elements in set C = \\{x | x = a ⊕ b, a ∈ A, b ∈ B\} modulo 998244353.

Examples

Input


2
3 5
5 8
3
1 2
1 9
2 9


Output


112


Input


1
1 9
2
2 4
2 10


Output


120

Note

In the second example, we can discover that the set C = \{0,1,...,15\}, which means that all numbers between 0 and 15 can be represented as a ⊕ b.
#include <bits/stdc++.h>
using namespace std;
const int N = 105, M = 998244353;
pair<long long, long long> a[N], b[N], q[N * N * N * 5];
int n, m, tot, ans;
int S(long long l, long long r) {
  int v1 = (l + r) % M, v2 = (r - l + 1) % M;
  return 1ll * v1 * v2 % M * (M + 1) / 2 % M;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%lld%lld", &a[i].first, &a[i].second);
  sort(a + 1, a + n + 1);
  int top = 1;
  for (int i = 2; i <= n; i++)
    if (a[i].first <= a[top].second + 1)
      a[top].second = max(a[top].second, a[i].second);
    else
      a[++top] = a[i];
  n = top;
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) scanf("%lld%lld", &b[i].first, &b[i].second);
  sort(b + 1, b + m + 1);
  top = 1;
  for (int i = 2; i <= m; i++)
    if (b[i].first <= b[top].second + 1)
      b[top].second = max(b[top].second, b[i].second);
    else
      b[++top] = b[i];
  m = top;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      for (int k = 60; k >= 0; k--) {
        long long bl1 = (a[i].first - 1) >> k, br1 = (a[i].second + 1) >> k;
        long long bl2 = (b[j].first - 1) >> k, br2 = (b[j].second + 1) >> k;
        int flag1 = !(bl1 & 1), flag2 = (br1 & 1), flag3 = !(bl2 & 1),
            flag4 = (br2 & 1);
        if (bl1 + 1 >= br1) flag1 = flag2 = 0;
        if (bl2 + 1 >= br2) flag3 = flag4 = 0;
        if ((flag1 && b[j].first != ((bl2 + 1) << k)) ||
            (flag3 && a[i].first != ((bl1 + 1) << k)))
          q[++tot] = make_pair((bl1 ^ bl2 ^ 1) << k, k);
        if ((flag2 && b[j].first != ((bl2 + 1) << k)) ||
            (flag3 && a[i].second + 1 != (br1 << k)))
          q[++tot] = make_pair((br1 ^ bl2 ^ 1) << k, k);
        if ((flag1 && b[j].second + 1 != (br2 << k)) ||
            (flag4 && a[i].first != ((bl1 + 1) << k)))
          q[++tot] = make_pair((bl1 ^ br2 ^ 1) << k, k);
        if ((flag2 && b[j].second + 1 != (br2 << k)) ||
            (flag4 && a[i].second + 1 != (br1 << k)))
          q[++tot] = make_pair((br1 ^ br2 ^ 1) << k, k);
        if (flag1 && flag3) q[++tot] = make_pair((bl1 ^ bl2) << k, k);
        if (flag2 && flag3) q[++tot] = make_pair((br1 ^ bl2) << k, k);
        if (flag1 && flag4) q[++tot] = make_pair((bl1 ^ br2) << k, k);
        if (flag2 && flag4) q[++tot] = make_pair((br1 ^ br2) << k, k);
      }
  sort(q + 1, q + tot + 1);
  for (int i = 1; i <= tot; i++)
    q[i].second = q[i].first + (1ll << q[i].second) - 1;
  top = 1;
  for (int i = 2; i <= tot; i++)
    if (q[i].first <= q[top].second + 1)
      q[top].second = max(q[top].second, q[i].second);
    else
      q[++top] = q[i];
  for (int i = 1; i <= top; i++) (ans += S(q[i].first, q[i].second)) %= M;
  printf("%d\n", ans);
}
