      program x
      implicit none
      integer          NDIM
      parameter(NDIM=1000)
      double precision u(NDIM,3),p(3),p2,m(NDIM),q(NDIM),sumqi
      integer          natoms,def(NDIM),ni(NDIM),i,j,n,k,ntypes,tot
      integer          ith
      character*10     dump_str
      character        dump_char
      integer          dump_int

      read(*,*) natoms
      read(*,*) ntypes
      read(*,*) (ni(i),i=1,ntypes)
      read(*,*) (q(i),i=1,ntypes)
      read(*,*) (m(i),i=1,ntypes)
      tot=0
      sumqi=0.0d0
      do i=1,ntypes
        sumqi=sumqi+dble(ni(i))*q(i)
        tot=tot+ni(i)
      end do
      read(*,*) (def(i),i=1,tot)

      do i=1,ntypes
        q(i)=q(i)-sumqi/dble(tot)
      end do

      write(*,*) "i,p2,(p(i),i=1,3)"
      do i=1,3*natoms
        p(1)=0.0d0
        p(2)=0.0d0
        p(3)=0.0d0
        p2=0.0d0
        read(*,*) dump_int,dump_char,dump_str
        do n=1,natoms
          read(*,*) (u(n,k),k=1,3)
        end do
        ith=0
        do j=1,ntypes
          do n=1,ni(j)
            do k=1,3
              p(k)=p(k)+q(j)*u(def(ith+n),k)/
     &                  sqrt(m(j))

            end do
          end do
          ith=ith+ni(n)
          p2=p(1)**2 + p(2)**2 + p(3)**2
        end do
        write(*,10) i,p2,p(1),p(2),p(3)
      end do

10    format(i5,f10.6,"     ",3f10.6)
20    format("     ",14f16.10)
      stop
      end
