#include <bits/stdc++.h>
const long long inf = 1ll << 60;
const int maxn = 30010;
using namespace std;
struct node {
  long long v;
  int id;
} a[maxn], b[maxn];
int n, q, posa[maxn], posb[maxn], ban[maxn];
long long f[maxn], w1[maxn], w2[maxn], w3[maxn];
bool cmp(node a, node b) { return a.v < b.v; }
void calc(int i) {
  w1[i] = w2[i] = w3[i] = -inf;
  if (i >= 1 && ban[i] != i) w1[i] = a[i].v * b[i].v;
  if (i >= 2 && ban[i] != i - 1 && ban[i - 1] != i)
    w2[i] = a[i].v * b[i - 1].v + a[i - 1].v * b[i].v;
  if (i >= 3) {
    if (ban[i] != i - 2 && ban[i - 1] != i && ban[i - 2] != i - 1)
      w3[i] =
          a[i].v * b[i - 2].v + a[i - 1].v * b[i].v + a[i - 2].v * b[i - 1].v;
    if (ban[i] != i - 1 && ban[i - 1] != i - 2 && ban[i - 2] != i)
      w3[i] = max(w3[i], a[i].v * b[i - 1].v + a[i - 1].v * b[i - 2].v +
                             a[i - 2].v * b[i].v);
  }
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) scanf("%I64d", &a[i].v), a[i].id = i;
  for (int i = 1; i <= n; i++) scanf("%I64d", &b[i].v), b[i].id = i;
  sort(a + 1, a + 1 + n, cmp), sort(b + 1, b + 1 + n, cmp);
  for (int i = 1; i <= n; i++) posa[a[i].id] = i, posb[b[i].id] = i;
  for (int i = 1; i <= n; i++) ban[i] = posb[a[i].id];
  for (int i = 1; i <= n; i++) calc(i);
  for (int j = 1, x, y; j <= q; j++) {
    scanf("%d%d", &x, &y);
    x = posa[x], y = posa[y], swap(ban[x], ban[y]);
    for (int i = max(1, x - 2); i <= min(n, x + 2); i++) calc(i);
    for (int i = max(1, y - 2); i <= min(n, y + 2); i++) calc(i);
    f[0] = 0;
    for (int i = 1; i <= n; i++) {
      if (i >= 1) f[i] = f[i - 1] + w1[i];
      if (i >= 2) f[i] = max(f[i], f[i - 2] + w2[i]);
      if (i >= 3) f[i] = max(f[i], f[i - 3] + w3[i]);
    }
    printf("%I64d\n", f[n]);
  }
  return 0;
}
